Ravindra BagaleCourses & study guides

Appendix B: Command Cheat Sheet

B.1 Package and service management

Task Ubuntu Amazon Linux 2023 / CentOS Stream 9
Update package lists / upgrade sudo apt update && sudo apt upgrade -​y sudo yum update -​y
Install sudo apt install -​y <pkg> sudo yum install -​y <pkg>
Remove sudo apt remove <pkg> sudo yum remove <pkg>
Search apt search <word> yum search <word>
Start now + enable at boot sudo service <svc> start + sudo systemctl enable <svc> same
Status / logs sudo service <svc> status, journalctl -​u <svc> -​f same
Apache service apache2 httpd
MySQL service mysql mariadb
Web user www-data nginx / apache
Firewall sudo ufw allow 'Nginx Full' sudo firewall-​cmd --​permanent --​add-​service=​http && sudo firewall-​cmd --​reload

B.2 Files, permissions, users

ls -la            cd /path          pwd              mkdir -p a/b
cp -r src dst     mv old new        rm -r dir        touch file
cat f   less f    head -n 20 f      tail -f f        wc -l f
chmod 755 dir     chmod 644 file    chmod 400 key.pem
sudo chown -R user:group /path
sudo useradd -m -s /bin/bash u      sudo passwd u    sudo usermod -aG wheel u  (Ubuntu: sudo)

B.3 Search and text

grep -rin "text" /path              find / -name "*.conf" 2>/dev/null
awk '{print $1}' file               sed -i 's/old/new/g' file
cmd | sort | uniq -c | sort -rn     cmd > out.txt 2>&1      echo x | sudo tee -a /etc/file

B.4 Networking

ip a            ip r              hostname -I          curl -s https://checkip.amazonaws.com
sudo ss -tlnp   ping -c 4 host    curl -I http://host  dig +short domain
nc -zv host 3306                  traceroute host      sudo hostnamectl set-hostname web01

B.5 Disk and EBS

df -hT          du -sh /path       lsblk -f            sudo blkid
sudo file -s /dev/nvme1n1          sudo mkfs -t xfs /dev/nvme1n1
sudo mkdir /data && sudo mount /dev/nvme1n1 /data     sudo umount /data
sudo growpart /dev/nvme0n1 1       sudo xfs_growfs -d /     sudo resize2fs /dev/nvme0n1p1
sudo mount -a   # test /etc/fstab  (UUID=... /data xfs defaults,nofail 0 2)

B.6 Web servers

sudo nginx -t && sudo service nginx reload
sudo apachectl configtest && sudo service httpd reload          # AL2023 / CentOS
sudo apache2ctl configtest && sudo service apache2 reload       # Ubuntu
sudo a2ensite site.conf ; sudo a2dissite 000-default.conf ; sudo a2enmod rewrite proxy proxy_http
sudo restorecon -Rv /var/www ; sudo setsebool -P httpd_can_network_connect 1   # CentOS SELinux
sudo nginx -T | grep -E "listen|server_name|root"               # what Nginx really loaded
sudo apachectl -S                                               # Apache vhosts + default (Ubuntu: apache2ctl -S)
sudo ln -s /etc/nginx/sites-available/site /etc/nginx/sites-enabled/   # Ubuntu Nginx enable site
sudo semanage fcontext -a -t httpd_sys_content_t "/srv/site(/.*)?" ; sudo restorecon -Rv /srv/site
sudo semanage port -a -t http_port_t -p tcp 8081                # CentOS: allow a new web port
curl -H "Host: site1.example.com" http://localhost              # test a vhost without DNS
Setting Nginx (server { }) Apache (<VirtualHost>)
Port listen 80; <VirtualHost *:​80> + Listen 80
Site name server_​name example.​com www.​example.​com; ServerName example.​com + ServerAlias www.​example.​com
Folder root /​var/​www/​site; DocumentRoot /​var/​www/​site + <Directory>
Index file index index.​html; DirectoryIndex index.​html
No folder listing autoindex off; Options -Indexes
Redirect return 301 https://$host$request_​uri; Redirect permanent / https://​example.​com/
Hide version server_​tokens off; ServerTokens Prod + ServerSignature Off

B.7 DNS and domains

dig example.com +short           dig www.example.com +short       dig NS example.com +short
dig @8.8.8.8 example.com +short  dig MX example.com +short        dig +trace example.com
nslookup example.com             host example.com                 curl -I http://example.com
sudo yum install -y bind-utils   # dig on AL2023/CentOS  (Ubuntu: sudo apt install -y dnsutils)
ipconfig /flushdns               # Windows     resolvectl flush-caches   # Linux laptop
sudo certbot --nginx -d example.com -d www.example.com -d blog.example.com

Typical records: A @ → Elastic IP · CNAME www → @ · A blog → Elastic IP · TTL 300–3600.

B.8 Databases

sudo mysql                                   # admin shell (socket auth)
sudo mysql_secure_installation
mysql -u appuser -p -h 127.0.0.1 appdb
mysqldump -u appuser -p appdb > backup.sql   mysql -u appuser -p appdb < backup.sql
mongosh         # MongoDB shell: show dbs ; use db ; db.col.find()

B.9 App process managers

pm2 start app.js --name api ; pm2 status ; pm2 logs api ; pm2 reload api ; pm2 startup ; pm2 save
sudo systemctl daemon-reload ; sudo service flaskapp start ; sudo systemctl enable flaskapp ; journalctl -u flaskapp -f
gunicorn --workers 3 --bind 127.0.0.1:8000 wsgi:app

B.10 EC2 operations (AWS CLI, optional)

aws ec2 allocate-address --domain vpc
aws ec2 associate-address --instance-id i-xxx --allocation-id eipalloc-xxx --allow-reassociation
aws ec2 release-address --allocation-id eipalloc-xxx
aws ec2 create-image --instance-id i-xxx --name web-v1            # add --no-reboot only if you accept the risk
aws ec2 copy-image --source-region ap-south-1 --source-image-id ami-xxx --region ap-south-2 --name web-v1-dr
aws ec2 deregister-image --image-id ami-xxx ; aws ec2 delete-snapshot --snapshot-id snap-xxx
aws ec2 create-snapshot --volume-id vol-xxx --description "before-upgrade"
aws ec2 modify-instance-attribute --instance-id i-xxx --instance-type "{\"Value\": \"t3.small\"}"   # instance stopped
nproc ; free -h ; lscpu                                            # verify after resizing

B.11 SSH and file transfer

ssh -i key.pem ec2-user@IP                   ssh -i key.pem ubuntu@IP
scp -i key.pem file ec2-user@IP:~/           scp -i key.pem -r dir ubuntu@IP:~/
scp -i key.pem ec2-user@IP:/path/file .      rsync -avz -e "ssh -i key.pem" dir/ ubuntu@IP:~/dir/