Linux Notes - Collated version

Tags:

[2023-07-16 Sun] Other notes are collected in smaller, atomic pages or articles. View a list of pages at Linux Notes.

These is an eclectic collection of notes on interesting topics and troubleshooting references that I come across related to using Linux. Expect many headings to be incomplete or even incorrect as they are often written in my own words based on my understanding at that moment, however - the notes should prove useful as an overview and general reference.

The source of this content is the markdown export of an org file and then using Oddmuse to display the page.

Count files in a directory and subdirectory

[disk usage - How do I count all the files recursively through directories - Unix & Linux Stack Exchange](https://unix.stackexchange.com/questions/4105/how-do-i-count-all-the-files-recursively-through-directories) provides several options to do this.

On a Mac, the . after find is necessary to specify the starting directory.

find . -type f | wc -l 

Logging cron jobs

The article How to check if a cron job ran | Benjamin Cane has detailed information on the logging of cron and grepping through the results.

New information for 2020: cron is now a systemctl service, so you can have the logs of cron / anacron with:
journalctl -u cron.service
if you did install anacron, it is the same with:
journalctl -u anacron.service
(anacron is for desktop, when your computer is not garanteed to be always on. It enforce that even if your computer is down at 5'00, daily jobs are run when the computer has started up).
- How to check cron logs in Ubuntu - Server Fault

If there is a unified log which may the default in the OS being used, then the syslog has to be grepped through for cron entries. This behavior can be changed:

By default on Ubuntu your cron output should be logged to varlog/syslog If you would like cron to log to it’s own file you can edit etcrsyslog.d/50-default.conf and uncomment the line
#cron.*
Then run
sudo service rsyslog restart
and your cron jobs will be logged to /var/log/cron.log
- Where are Cron Job errors logged? | DigitalOcean

Use imagemagick convert image formats

For example converting jpg to png:

mogrify -format png abcd.jpg

The above also works on files like abcd.ico.

Software to make Windows more Unix-y

Some words of wisdom from #emacs

<homerj> msys2, displayfusion, fences, and autohotkey are must-haves

The relevance of C with respect to Linux

Most of Linux is written in C, though newer developments often involve using Python as well. A user or a system admin need not know C to efficiently use Linux. A kernel developer must know C to be able to develop with the core of Linux.

initramfs

Initial RAM filesystem. Generally created after each installation, and usually automatically updated after each system or kernel upgrade. These are the files and libraries and configurations that are loaded into the RAM during the system startup.

User land and Kernel Land <code>[0/1]</code>

The Linux Kernel is a bunch of libraries which directly interact with the hardware. The user does not directly interact with the Kernel. The user, or any command can access the kernel using a combination of 'intermediaries' like system calls, [Capabilities and permissions](linux-notes.md). System calls form the glue between the user land and the kernel land which are essentially separate.

Kernel modules are essentially drivers. Each hardware component generally comes with a driver.

Windows OS Admins are not equivalent of a root user. Windows admins are essentially users with special capabilities, but is not a kernel level user.

DLL's in Windows OS are the equivalent of kernel modules.

Extensions like .d

The extension .d could refer to a daemon or more commonly to a directory.

PAM - Pluggable Authentication modules

Core security in Linux is handled by SE Linux. Apparently the SE Linux library was open sourced by the NSA and is a significant contribution to Linux.

Make or create a Directory and Change into it

Reference: <https://unix.stackexchange.com/questions/125385/combined-mkdir-and-cd>

$_ is a special parameter that holds the last argument of the previous command. The quote around $\_ make sure it works even if the folder name contains spaces.
mkdir foo && cd "$_"

Send a currently running process to the background in the terminal

dtrx: intelligent archive extraction tool

See link to ubuntu manpages. On debian / ubuntu installing this is as simple as

sudo apt-get install dtrx

This actually seems useful for quick extractions and meta data exploration of deb packages, compared to struggling with the knobs of other tools like gzip, gunzip and so on.

Creating a new user on a server and note on ssh keys

This is based on an ubuntu droplet

Logged in as root: adding a user and adding the user to the sudo group:

adduser shrysr
usermod -aG sudo shrysr

After this the password and other details have to be entered which is self explanatory.

Note on SSH keys and access: Using digital ocean - it is easy to create SSH keys for the root user. However, this has to be manually scp'd or added for any lesser users on the server. If the folder .ssh is copied from the root user to the lesser user's home folder then the folder permission has to be changed appropriately.

The local ssh key can be accessed via cat:

cat ~/.ssh/id_rsa.pub

The authorised keys are stored in .ssh/authorised_keys. The ownership of the folder can be changed with:

sudo chown -R $USER ~/.ssh

Copy SSH id

ssh-copy-id  -i ~/.ssh/id_rsa shrysr@s.ragavan.co:~/.ssh/

Installing node js and npm on debian

curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

Installing Go on debian

Instructions followed of Linode worked well. The latest version has to be noted and replaced below from Go's download page.

cd ~/temp
curl -O https://storage.googleapis.com/golang/go1.12.7.linux-amd64.tar.gz

Verifying the sign

sha256sum ~/temp/go1.12.7.linux-amd64.tar.zg

Change ownership of the file

sudo chown -R ~/temp/go
sudo mv go /usr/local

Change the .profile file

cat >> ~/.profile << EOF
# Adding go to the path
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
EOF

Re-read the profile

source ~/.profile

Setting hostname on debian

hostnamectl set-hostname example_hostname

Setting 256 colors in the terminal while using tmux

Add this to the ~/.tmux.conf file. This may have to be created if unavailable.

Reference link

touch ~/.tmux.conf
cat >> ~/.tmux.conf  << EOF
set -g default-terminal "tmux-256color"
EOF

tmux notes and resources

<span class="timestamp-wrapper"><span class="timestamp">[2019-08-03 Sat] </span></span> I have recently started using tmux to manage my VPS, which hosts my website and hopefully many more convenient things down the line.

A good prefix could be C-/. Some of the commands are not totally intuitive like in Emacs. However, there are a number of plugins that can make life easier.

This blog post outlines a good good approach, that starts with installing the tmux plugin manager, which makes it easy to install plugins. I will reproduce the steps here for convenient reference. This is a part of converting my entire configuration into org source blocks, which can be tangled into configuration files. In this way, it also becomes easier to maintain.

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# Append the following configuration to the .tmux.conf file

cat >> ~/.tmux.conf << EOF
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'tmux-plugins/tmux-pain-control'
# .... maybe more plugins here

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'
EOF

Detach the tmux process and return to the server's shell. The configuration of the server will have to be refreshed. It does not appear that the server has to be shut down, atleast for the changes above.

tmux source ~/.tmux.conf
  1. Close a tmux window : Prefix + &

References

Process control: background and foreground

Installing the Iosevka font

On Arch / Antergos

Use AUR to install iosevka in Antergos / Arch as the package is already available.

yay -S ttf-iosevka

Setting the font in Emacs. This should be added to the init. The font height could vary based on the monitor size.

(set-face-attribute 'default nil :family "ttf-iosevka" :height 120)

Setup gpg-agent to be running whenever gpg is called

For some reason, it appears though the gpg-agent is shown to be running, this configuration is required to make sure that the entered keys are stored in the keyring.

cat >> ~/.gnupg/gpg.conf <<EOF
no-greeting
no-permission-warning
lock-never
keyserver-options timeout=10
use-agent
EOF

Install tk especially for being able to select the CRAN mirrors in R

This is pertinent to Arch based distros.

sudo pacman -S tk

Git global config

git config --global user.email "abcs@gmail.com"
git config --global user.name "Mad Max"

scp : tranfer a folder to the server

The command will look like:

scp -r <from> user@45.56.88.290:<location>

Transferring a single file does not require the -r switch.

Upgrading a debian distro

sudo apt-get update
sudo apt-get dist-upgrade

App installation in debian using flatpak

Some apps are not available in the so called stable debian software archives. Therefore alternative sources have to be established for the same.

Installing flatpak on debian and adding the flatpak repository:

sudo apt-get install flatpak
sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

Reboot after the above commands.

Note: Calling a flatpak based app is rather verbose, and is better served by defining appropriate aliases.

Franz: Multi-network messenger

This app covers Slack and Whatsap and other networks. It still takes up about 1GB of RAM and the app itself is about 500MB, but it atleast covers all the platforms in one go and should be useful in the office.

In addition, the org.freedesktop.Platform package has to be installed. The latter gets installed automatically, when executed in the terminal.

Pre-requisites for debian/ubuntu

sudo apt install libx11-dev libxext-dev libxss-dev libxkbfile-dev
flatpak install flathub com.meetfranz.Franz

Slack:

The Slack app takes up a lot of memory.

flatpak install flathub com.slack.Slack

Swapping control and Capslock

Creating xmodmap script : Swapping control and capslock keys Source: link

cat > ~/.xmodmap <<EOF
!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L
EOF

Executing xmodmap on the configuration above

xmodmap ~/.xmodmap

Installing Firefox developer edition

The developer edition of Firefox contains interesting features, and it appears to perform better. The developer edition is available as a package on Arch Linux (AUR). For Debian, the procedure is a little round-about.

The following procedure using flatpak is picked up from the Debian wiki page.

Using flatpak

Unofficial builds are provided by Fedora at <https://firefox-flatpak.mojefedora.Cz/>

sudo apt install flatpak
sudo flatpak remote-add --from gnome https://sdk.gnome.org/gnome.flatpakrepo
sudo flatpak remote-add --from org.mozilla.FirefoxRepo https://firefox-flatpak.mojefedora.cz/org.mozilla.FirefoxRepo.flatpakrepo

Then for "developer edition" (aka "Beta"):

flatpak install org.mozilla.FirefoxRepo org.mozilla.FirefoxDevEdition

and Running:

flatpak run org.mozilla.FirefoxRepo org.mozilla.FirefoxNightly

The Debian wiki also describes a method to add the flatpak installations to the Path. However, this is a newer feature and is unavailable at the moment on my machine.

echo 'export PATH=$PATH:/var/lib/flatpak/exports/bin' >> ~/.zshrc

Emacs can be installed via Conda

The advantage of using conda is being able to quickly install reasonably recent versions of Emacs quickly on Debian type OS's which often reference older (stable) versions of software packages by default. Using conda would avoid adding PPA's or hunting for binaries or even compiling from source. Another advantage is that this approach can be used cross platform.

One disadvantage of this method is that the package is installed into the miniconda / anaconda package installation path. Though the instillation script of miniconda adds the path for bash, it has to be manually set for any other shell like zsh. However, once this is done - there appear to be no issues in using Emacs.

conda install -c conda-forge emacs

Virtualbox: resizing virtual disk image - vdi

Reference: <http://derekmolloy.ie/resize-a-virtualbox-disk>

It does not appear to be possible to expand the size of a fixed format vdi. The floating format has a disadvantage of a read-write overhead for expanding the disk image as it is utilised.

However, as per the documentation, after the hard disk size reaches a stable stage, this overhead becomes negligible on an average.

Therefore the vdi has to be copied (or cloned), and the floating format has to be selected. This is done using the copy option in the virtualbox media manager. Once copied, the expanded vdi image has to be attached to the guest OS.

When the attachment is complete, the hardisk will show up in the virtualbox media manager app. Now the vdi size can be adjusted to the desired value.

The next step is to download the live iso of gparted. This should be loaded as a storage device with the live CD option selected. With this loaded, the existing partitions have to be changed appropriately<sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup>. This step has to be done to enable Linux to recognise the expanded harddisk.

Once this has been, the gparted iso can be removed and the guest OS can be booted as usual. However, the UUID of the paritions have to be changed appropriately. If not changed, there will be delay during boot, especially if the swap partition has been modified.

The actual partition setup and the UUIDs can be viewed with:

lsblk -f

The appropriate UUID has to be replaced in the file /etc/fstab. Technically, the fstab file is generated by the command mkinitcpio, but sometimes a manual change is necessary.

Downgrading a single package in Arch linux

From the Arch linux wiki : archive : downgrading via downloading the Package from URL. Find the package you want under packages and let pacman fetch it for installation. For example:

pacman -U https://archive.archlinux.org/packages/ ... packagename.pkg.tar.xz

Downgrading via local cache

pacman -U /var/cache/pacman/pkg/<package-name>

It seems to be a very good idea to maintain a few older versions of packages in the cache, even at the expense of Harddisk space.

Further options are provided at this Unix stack exchange discussion.

Footnotes

sup
<a id="fn.1" href="#fnr.1">1</a></sup> It is likely that the swap partition is the last partition, and the previous partition is the root which has to be extended. In this case, the swap has to be deleted and the root partiion should be expanded to the desired size, leaving behind room for the swap partition. The final unallocated space has to be used for a new extension partition and then a logical partition to create the linux-swap. For some reason, there is a space of 1MB preceding the swap partition.