List of essential Linux commands for server management

Table of contents

  1. Generate SSH key
  2. Login to host via SSH
  3. Execute command on a server through SSH
  4. Displays currently logged in users in the system
  5. Displays Linux system information
  6. Displays kernel release information
  7. Shows the system hostname
  8. Shows system reboot history
  9. Displays information about the user
  10. Displays IP addresses and all the network interfaces
  11. Downloads a file from an online source
  12. Compress a file with gzip
  13. Interactive disk usage analyzer
  14. Install Node.js using the Node Version Manager
  15. Too long; didn't read
  16. Combine all Nginx access logs to one big log file
  17. Set up Redis server
  18. Generate statistics of your webserver
  19. Search for a given pattern in files
  20. Find proccess ID for a specific program
  21. Print name of current/working directory
  22. Creates a blank new file
  23. Displays first lines in a file
  24. Displays last lines in a file
  25. Count lines in a file
  26. Find all instances of the file
  27. Find file names that begin with ‘index’ in /home folder
  28. Find files larger than 100MB in the home folder
  29. Displays block devices related information
  30. Displays free space on mounted systems
  31. Displays free and used memory in the system
  32. Displays all active listening ports
  33. Kill a process violently
  34. List files opened by user
  35. Execute "df -h", showing periodic updates
Generate SSH key
ssh-keygen -t ed25519 -C "your_email@example.com"

# when no support for Ed25519 present
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

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 root@100.100.100.100 "ls /root"

# execute many commands
ssh root@100.100.100.100 "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
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
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
➝ Get in contact with me via email.


Articles from blogs I follow around the net

Willingness to look stupid

People frequently1 think that I'm very stupid. I don't find this surprising, since I don't mind if other people think I'm stupid, which means that I…

via Dan Luu October 21, 2021

Generated by openring