Useful Linux commands
by Alex Arica

User

Add a new user:

adduser [username]
                    

Add a user to a new group:

sudo usermod -aG [groupname] [username]
                    

Add user to sudo group:

sudo usermod -aG sudo alex
                    

Display groups that a user belongs to:

groups [username]
                    

Bash history

The bash history is kept in the file:

~/.bash_history
                    

Display full history:

history
                    

Delete full history:

history -c
                    

Disable current user's bash history:

echo "HISTFILESIZE=0" >> ~/.bashrc
source ~/.bashrc
history -c
                    

Package manager "apt"

Update packages:

apt-get update
                    

Upgrade distro:

apt-get dist-upgrade
                    

Uninstall packages which were installed by other packages which were since removed:

apt autoremove
                    

Uninstall a package with all its files:

sudo apt --purge remove snapd
                    

Debian repository configuration file:

cat /etc/apt/sources.list
                    

SystemD

Check the logs of a process run by systemd

# for example for kubelet:
systemctl status kubelet
journalctl -xeu kubelet
                    

By default when we start a service in SystemD, it does not persist on reboot:

sudo systemctl start nginx
                    

We need to enable it so that it is automatically started on reboot:

sudo systemctl enable nginx
                    

The systemd programs are located in:

/etc/systemd/system/[program]
                    

Tar gz

Compress:

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
                    

Uncompress:

tar -xzvf archive.tar.gz -C /tmp
                    

Find files

Find a file by name:

find -L . -name '*.java'
                    

Find Files with text:

grep -Ril "POSTGRES_MYAPP_PASSWORD" /
                    

The option -L forces through symlinks.

Stream

Stream file:

tail -f [file path]
                    

Tail the output of the command "ls":

ls -t /etc | tail -n 5
                    

Append contents to a file

Write multi-lines contents to a file:

cat <<EOF | sudo tee myfile.yaml
a configuration
with
many
lines
EOF
                    

Above "sudo" is only required if the access to file is restricted for current user.

Write a single line content to a file:

echo "some contents" | sudo tee -a /etc/file.conf
                    

Another way to write a single line content to a file:

echo "some contents" >> /etc/file.conf
                    

Note that you cannot use "sudo" with the command above. To do so, run:

sudo -- bash -c 'echo "some contents" >> /etc/file.conf'
                    

Write the list of files' names located in a folder to a file:

ls -al ~ | sudo tee mylist.txt
                    
ls -al ~ >> files.txt
                    

Good articles on this topic:

https://www.cyberciti.biz/faq/linux-append-text-to-end-of-file/
https://linuxize.com/post/bash-append-to-file/

Disk details

List the partitions and the space that they occupy on disk:

df -h
                    

List the biggest directories & files:

sudo du -a / | sort -n -r | head -n 10
                    

List the biggest files:

sudo find / -type f -exec du -Sh {} + | sort -rh | head -n 5
                    

Kernel modules

Linux's kernel has a list of modules that can be enabled and disabled.

List all enabled modules:

lsmod
                    

Search if a module is enabled (e.g. "overlay"):

lsmod | grep overlay
                    

Enable a module:

sudo modprobe overlay
                    

The above command does not ensure that a module would be enabled on reboot. To do so, you can create a configuration file in the folder "/etc/modules-load.d/". For example, let's enable the module "overlay" for "kubernetes":

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
EOF
                    

You can also achieve the above from the common module file "/etc/modules":

sudo -- bash -c 'echo "overlay" >> /etc/modules'
                    

System configuration

Linux's kernel has a list of system configurations that can be enabled and disabled.

List all system conf:

sysctl -a
                    

Search if a system conf is enabled (e.g. "net.ipv4.ip_forward"):

sudo sysctl net.ipv4.ip_forward
                    

The output "net.ipv4.ip_forward = 0" means disabled. And "net.ipv4.ip_forward = 1" means enabled.

To enable a system configuration and persist it on reboot, you can create a configuration file in the folder "/etc/sysctl.d/". For example, let's enable the conf "net.ipv4.ip_forward" for "kubernetes":

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF
                    

Apply the changes without having to reboot:

sudo sysctl --system