List of essential Linux commands for server management

post, Aug 1, 2021, on Mitja Felicijan's blog

Generate SSH key

ssh-keygen -t ed25519 -C "[email protected]"

# when no support for Ed25519 present
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Note: By default SSH keys get stored to /home/<username>/.ssh/ folder.

Login to host via SSH

# connect to host as your local username
ssh host

# connect to host as user
ssh <user>@<host>

# connect to host using port
ssh -p <port> <user>@<host>

Execute command on a server through SSH

# execute one command
ssh [email protected] "ls /root"

# execute many commands
ssh [email protected] "cd /root;touch file.txt"

Displays currently logged in users in the system

w

Displays Linux system information

uname

Displays kernel release information

uname -r

Shows the system hostname

hostname

Shows system reboot history

last reboot

Displays information about the user

sudo apt install finger
finger <username>

Displays IP addresses and all the network interfaces

ip addr show

Downloads a file from an online source

wget https://example.com/example.tgz

Note: If URL contains ?, & enclose the URL in double quotes.

Compress a file with gzip

# will not keep the original file
gzip file.txt

# will keep the original file
gzip --keep file.txt

Interactive disk usage analyzer

sudo apt install ncdu

ncdu
ncdu <path/to/directory>

Install Node.js using the Node Version Manager

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
source ~/.bashrc

nvm install v13

Too long; didn't read

npm install -g tldr

tldr tar

Combine all Nginx access logs to one big log file

zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log

Set up Redis server

sudo apt install redis-server redis-tools

# check if server is running
sudo service redis status

# set and get a key value
redis-cli set mykey myvalue
redis-cli get mykey

# interactive shell
redis-cli

Generate statistics of your webserver

sudo apt install goaccess

# check if installed
goaccess -v

# combine logs
zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log

# export to single html
goaccess \
  --log-file=/var/log/nginx/access-all.log \
  --log-format=COMBINED \
  --exclude-ip=0.0.0.0 \
  --ignore-crawlers \
  --real-os \
  --output=/var/www/html/stats.html

# cleanup afterwards
rm /var/log/nginx/access-all.log

Search for a given pattern in files

grep -r ‘pattern’ files

Find proccess ID for a specific program

pgrep nginx

Print name of current/working directory

pwd

Creates a blank new file

touch newfile.txt

Displays first lines in a file

# -n <x> presents the number of lines (10 by default)
head -n 20 somefile.txt

Displays last lines in a file

# -n <x> presents the number of lines (10 by default)
tail -n 20 somefile.txt

# -f follows the changes in file (doesn't closes)
tail -f somefile.txt

Count lines in a file

wc -l somefile.txt

Find all instances of the file

sudo apt install mlocate

locate somefile.txt

Find file names that begin with ‘index’ in /home folder

find /home/ -name "index"

Find files larger than 100MB in the home folder

find /home -size +100M

Displays block devices related information

lsblk

Displays free space on mounted systems

df -h

Displays free and used memory in the system

free -h

Displays all active listening ports

sudo apt install net-tools

netstat -pnltu

Kill a process violently

kill -9 <pid>

List files opened by user

lsof -u <user>

Execute "df -h", showing periodic updates

# -n 1 means every second
watch -n 1 df -h


Other posts