From 52bfdaea11ec4ff5a8bc9b97fb86a2777b8b4511 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sun, 1 Aug 2021 20:25:28 +0200 Subject: new post --- posts/2021-08-01-linux-cheatsheet.md | 364 +++++++++++++++++++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 posts/2021-08-01-linux-cheatsheet.md (limited to 'posts/2021-08-01-linux-cheatsheet.md') diff --git a/posts/2021-08-01-linux-cheatsheet.md b/posts/2021-08-01-linux-cheatsheet.md new file mode 100644 index 0000000..6e28016 --- /dev/null +++ b/posts/2021-08-01-linux-cheatsheet.md @@ -0,0 +1,364 @@ +--- +Title: List of essential Linux commands for server management +Description: List of essential Linux commands for server management +Slug: linux-cheatsheet +Listing: true +Created: 2021-08-01 +Tags: [] +--- + +***For Debian and Ubuntu servers*** + + + + + +##### Generate SSH key + +```bash +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//.ssh/` folder. + + + +##### Login to host via SSH + +```bash +# connect to host as your local username +ssh host + +# connect to host as user +ssh @ + +# connect to host using port +ssh -p @ +``` + + + +##### Execute command on a server through SSH + +```bash +# 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 + +```bash +w +``` + + + +##### Displays Linux system information + +```bash +uname +``` + + + +##### Displays kernel release information + +```bash +uname -r +``` + + + +##### Shows the system hostname + +```bash +hostname +``` + + + +##### Shows system reboot history + +```bash +last reboot +``` + + + +##### Displays information about the user + +```bash +sudo apt install finger +finger +``` + + + +##### Displays IP addresses and all the network interfaces + +```bash +ip addr show +``` + + + +##### Downloads a file from an online source + +```bash +wget https://example.com/example.tgz +``` + +Note: If URL contains ?, & enclose the URL in double quotes. + + + +##### Compress a file with gzip + +```bash +# will not keep the original file +gzip file.txt + +# will keep the original file +gzip --keep file.txt +``` + + + +##### Interactive disk usage analyzer + +```bash +sudo apt install ncdu + +ncdu +ncdu +``` + + + +##### Install Node.js using the Node Version Manager + +```bash +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 + +```bash +npm install -g tldr + +tldr tar +``` + + + +##### Combine all Nginx access logs to one big log file + +```bash +zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log +``` + + + +##### Set up Redis server + +```bash +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 + +```bash +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 + +```bash +grep -r ‘pattern’ files +``` + + + +##### Find proccess ID for a specific program + +```bash +pgrep nginx +``` + + + +##### Print name of current/working directory + +```bash +pwd +``` + + + +##### Creates a blank new file + +```bash +touch newfile.txt +``` + + + +##### Displays first lines in a file + +```bash +# -n presents the number of lines (10 by default) +head -n 20 somefile.txt +``` + + + +##### Displays last lines in a file + +```bash +# -n 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 + +```bash +wc -l somefile.txt +``` + + + +##### Find all instances of the file + +```bash +sudo apt install mlocate + +locate somefile.txt +``` + + + +##### Find file names that begin with ‘index’ in /home folder + +```bash +find /home/ -name "index" +``` + + + +##### Find files larger than 100MB in the home folder + +```bash +find /home -size +10000M +``` + + + +##### Displays block devices related information + +```bash +lsblk +``` + + + +##### Displays free space on mounted systems + +```bash +df -h +``` + + + +##### Displays free and used memory in the system + +```bash +free -h +``` + + + +##### Displays all active listening ports + +```bash +sudo apt install net-tools + +netstat -pnltu +``` + + + +##### Kill a process violently + +```bash +kill -9 +``` + + + +##### List files opened by user + +```bash +lsof -u +``` + + + +##### Execute "df -h", showing periodic updates + +```bash +# -n 1 means every second +watch -n 1 df -h +``` + + + -- cgit v1.2.3