Linux and various terminal stuff

26 Apr 2024

These are my notes for Linux related stuff that didn't quite deserve their own post as of yet. Hopefully you'll also find something in here useful. Enjoy.

Alias

This command can be used for creating your own CLI commands. I e.g. used it for shorthanding my composer install like this:
alias composer='php /usr/local/bin/composer'
This means that whenever I write “composer” in the console, what I'm really writing is “php /usr/local/bin/composer”. You can see all your aliases with the
alias
command, and remove specific aliaseses again with this command:
unalias composer

Persisting aliases

The aliases won't stick. For that you have to add them into your ~/.bash_aliases-file.

You simply add the alias commands as stated above to the ~/.bash_aliases-file, e.g.:
alias composer='php /usr/local/bin/composer'
The ~/.bash_aliases-file is loaded via the ~/.bashrc-file. So when you make changes to the ~/.bash_aliases-file make sure to reload the ~/.bashrc-file. This is done with the following command:
source ~/.bashrc

Cronjobs

Use the following command to see your cronjobs:
crontab -e
Each line has 5 time-and-date fields followed by a command. The field are: minutes, hours, day of month, month and day of week. I usually use cron.help to define new running frequencies.

Running cron from a specified directory

Some commands have to be run from a certain directory. To do this with cron, you need to change the directory and run your command in one command. This can be achieved by chaining the commands with && as shown in this StackOverflow answer.
cd /path/to/directory && mycommand
Change it to your directory and mycommand to your desired command.

Cron log

You can display the tail-end of the cron log with the following command:
tail /var/log/cron
This is very useful when adding new commands to your crontab and ensuring that they work. Alternatively use grep to search through the log.

Distros

CentOS 8

Since CentOS 8 had an end-of-life 2021-12-31 there is really only one thing to do in CentOS 8.

How to convert CentOS 8 to RockyLinux

Below is a scrpt for rather seamlessly migrating CentOS 8 to RockyLinux. Read about the script here: [migrate2rocky.sh](https://github.com/rocky-linux/rocky-tools/tree/main/migrate2rocky). Remember to use tmux if you are running a remote session.
sudo dnf update -y
sudo dnf upgrade -y

sudo reboot

wget https://raw.githubusercontent.com/rocky-linux/rocky-tools/main/migrate2rocky/migrate2rocky.sh
chmod +x migrate2rocky.sh
sudo ./migrate2rocky.sh -r

sudo reboot

RockyLinux

Moved to RockyLinux notes.

Files

Finding a files MIME type in Linux

A media type also known as MIME type indicates the nature and format of a file. Finding a given files MIME type is rather easy in Linux with the following command:
file --mime-type certificate.pdf
Replace certificate.pdf with whatever file, you are examining. The command will give output similar to this:
certificate.pdf: application/pdf

Finding stuff in files

grep -sIir TEXT *
Replace TEXT with whatever you are searching for and * with your desired folder.

Samba

Samba allow network sharing of files and printers between Windows and Linux systems.

Mount a samba share

Install the following package and create the folder /media/share.
sudo apt install cifs-utils
sudo mkdir /media/share
Now create a credentials file.
nano /etc/samba/credentials
Set the samba username and password in the file like this:
username=smb_username
password=smb_password
Manually mounting a samba share
To manually connect to a samba share, you can run the following command:
sudo mount -t cifs -o rw,credentials=/etc/samba/credentials //192.168.1.10/share /media/share
Replace the ip address with your server. This will mount the share until system boot.

The samba share can then be unmounted again with:
umount /media/share
Auto mounting a samba share on system boot
To auto mount the remote share on system boot you need to make an addition to /etc/fstab.
sudo nano /etc/fstab
Where you add the following to the end of the file:
//192.168.1.10/share /media/share cifs credentials=/etc/samba/credentials
Replace the ip address with your server.

You can test the /etc/fstab file without rebooting with the command: sudo mount -a.

Harddisk

Display connected drives.
sudo fdisk -l
Displays how much space is left.
df -H

Harddisk health

Use the tool smartctl to monitor the health of drives. If you prefer a GUI, you can also install Gsmartcontrol, which is a GUI for smartctl.

NGINX

Certbot

Useful commands:

PDF

Converting a PDF to black and white (reducing filesize)

I found a command that utilizes Ghostscript to turn the PDF into black and white while also reducing the filesize remmarkably.
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFsettings=/display -dNOPAUSE -dQUit -dBATCH -r150 -sOutputFile=outputfile.pdf -dCompressFonts=true -sProcessColorModel=DeviceGray -sColorConversionStrategy=Gray inputfile.pdf 
Simply replace inputfile.pdf and outputfile.pdf with your input and output filenames.

Resizing windows in Linux

You can use the CLI tool wmctrl to resize your windows in Linux, which can be useful when recording your desktop. The following command resizes my Firefox browser to 1200 pixels wide and 1600 pixels wide.
wmctrl -r Firefox -e 0,0,0,1600,1200

SSH

Copying files

Use the following syntax for a single file:
scp filename user@ip:location
Which would like like this for a simple textfile:
scp stuff.txt root@192.168.0.23:/home/root/Documents
You can also copy entire directories like so:
scp -r .\Desktop\MyFolder\* root@192.168.0.23:/home/root/Desktop/MyFolder/
The above examples show you how to copy from your own computer to another computer, however you can also reverse the direction, so you are downloading from the remote computer unto your own. Simply reverse the direction as shown here:
scp -r user@ip:location destination
Simply adjust the above to your situation, e.g. replace destination with /home/user/Downloads. Note: rerunning the command just overwrites the existing files, so you always have the latest version.

I'm doing the above when doing backups of my home server. I have written a bit about backups here: How to backup your data.

Setting up SSH aliases

You can setup aliases for your ssh commands, so you don't need to remember the IP addresses of your servers. This can be configured in this file: ~/.ssh/config by adding something similar to this:
# mydatabase
Host mydatabase
User databaseuser
Hostname 123.123.123.123
Change mydatabase with whatever name you want to use, databaseuser with your username, and 123.123.123.123 with the actual IP address.

SSH Tunnelling

ssh -i ~/.ssh/id_rsa -N -L 13306:localhost:3306 root@ip
-i is for identity file. N is unknown. L is location. 13306 is the local port. localhost:3306 is the location and port from the viewpoint of the remote.

Tmux

Tmux is a terminal multiplexer, which means that you can start a Tmux session and then open multiple windows inside that session. Each window occupies the entire screen and can be split into rectangular panes. With Tmux you can easily switch between multiple programs in one terminal, detach them and reattach them to a different terminal. Most importantly (in my opinion) the Tmux sessions are persistent, meaning that programs running in Tmux will continue to run even if you get disconnect, which is very nifty for remote sessions.

You might also enjoy

RockyLinux notes

RockyLinux notes

Published 2024-05-05

DevOps

Linux

Notes

Various notes gathered after converting to RockyLinux from CentOS.

Read the post →
How to easily web scrape any website with Python

How to easily web scrape any website with Python

Published 2024-05-03

Datahoarding

Notes

Python

Web development

Learn how to easily web scrape any website using Python. I go through the various techniques I use.

Read the post →
Python notes

Python notes

Published 2024-05-03 — Updated 2024-05-14

Notes

Python

Different tips, tricks and how-to's while developing various scripts in Python.

Read the post →