Last active
December 2, 2025 11:54
-
-
Save eplt/0ddea14011ec35fbbcac8a193f2e3e02 to your computer and use it in GitHub Desktop.
Ubuntu Cheatsheet - Commonly used commands for Ubuntu (18.04 and 20.04) on DigitalOcean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I use Ubuntu 18.04 and 20.04 Droplets on DigitalOcean often. This is a cheatlist of commands used frequently. | |
| Sign up with https://m.do.co/t/b298d6966c0c (Discount code for newbies, I get referral credits too) | |
| -- System Information -- | |
| Check OS version: | |
| cat /etc/os-release | |
| Check kernel version: | |
| cat /proc/version | |
| uname -a | |
| List all users: | |
| grep bash /etc/passwd | cut -d: -f1 | |
| Check component versions: | |
| apache2 -v | |
| mysql -V | |
| php -v | |
| -- Initial Server Setup -- | |
| Set locales: | |
| sudo dpkg-reconfigure locales | |
| Update system to latest: | |
| sudo apt-get update | |
| sudo apt-get upgrade | |
| Add SSH key to server (assumes SSH key already setup): | |
| ssh-copy-id root@<server_ip> | |
| or | |
| cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> .ssh/authorized_keys && echo "Key copied"' | |
| Clear known_hosts record before connecting: | |
| nano ~/.ssh/known_hosts | |
| -- DigitalOcean Monitoring Agent -- | |
| Remove old do-agent: | |
| sudo apt-get purge do-agent | |
| Install monitoring agent: | |
| curl -sSL https://insights.nyc3.cdn.digitaloceanspaces.com/install.sh | sudo bash | |
| Check installation status: | |
| systemctl status do-agent | |
| -- Service Management -- | |
| Apache2 Commands: | |
| systemctl start apache2 | |
| systemctl stop apache2 | |
| systemctl restart apache2 | |
| systemctl status apache2 | |
| Nginx Commands: | |
| systemctl start nginx | |
| systemctl stop nginx | |
| systemctl restart nginx | |
| systemctl status nginx | |
| MySQL Commands: | |
| systemctl start mysql | |
| systemctl stop mysql | |
| systemctl restart mysql | |
| systemctl status mysql | |
| PostgreSQL Commands: | |
| systemctl start postgresql | |
| systemctl stop postgresql | |
| systemctl restart postgresql | |
| systemctl status postgresql | |
| -- Let's Encrypt SSL Certificates -- | |
| Setup Let's Encrypt with certbot: | |
| cd /usr/local/sbin | |
| sudo wget https://dl.eff.org/certbot-auto | |
| sudo chmod a+x /usr/local/sbin/certbot-auto | |
| Setup for Nginx: | |
| certbot-auto --nginx -d <domainname> -d www.<domainname> | |
| Setup for Apache: | |
| certbot-auto --apache -d <domainname> -d www.<domainname> | |
| Auto-renew certificates (add to crontab): | |
| sudo crontab -e | |
| 30 2 * * 1 /usr/local/sbin/certbot-auto renew >> /var/log/le-renew.log | |
| -- Firewall Management -- | |
| Important: Don't enable ufw unless certain you won't block SSH access. | |
| Check firewall status: | |
| ufw status verbose | |
| ufw show added | |
| ufw status numbered | |
| Allow HTTP/HTTPS: | |
| sudo ufw allow http | |
| sudo ufw allow 80 | |
| sudo ufw allow https | |
| sudo ufw allow 443 | |
| Allow port ranges: | |
| sudo ufw allow 6000:6007/tcp | |
| sudo ufw allow 6000:6007/udp | |
| Allow from specific IP: | |
| sudo ufw allow from 203.0.113.4 | |
| sudo ufw allow from 203.0.113.4 to any port 22 | |
| Deny/reset: | |
| sudo ufw deny http | |
| sudo ufw disable | |
| sudo ufw reset | |
| Enable firewall: | |
| sudo ufw enable | |
| Delete rules by number: | |
| ufw status numbered | |
| ufw delete <NUM> | |
| -- Scheduled Tasks & Startup -- | |
| Cron scheduling: | |
| sudo crontab -e | |
| Use @reboot keyword for startup tasks: | |
| @reboot /path/to/script | |
| Alternatively, use rc.local: | |
| sudo nano /etc/rc.local | |
| If rc.local doesn't exist, start with: | |
| #!/bin/bash | |
| Then make executable: | |
| sudo chmod a+x /etc/rc.local | |
| List all cron jobs for all users (run as root): | |
| for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done | |
| -- Configuration Files -- | |
| PHP configuration (7.2 example): | |
| nano /etc/php/7.2/apache2/php.ini | |
| systemctl restart apache2 | |
| Nginx configuration: | |
| nano /etc/nginx/nginx.conf | |
| systemctl restart nginx | |
| Apache2 configuration: | |
| nano /etc/apache2/apache2.conf | |
| systemctl restart apache2 | |
| -- Database Management -- | |
| Backup MySQL to file: | |
| mysqldump -u root -p --all-databases > alldb.sql | |
| gzip -9 alldb.sql | |
| Restore MySQL from backup: | |
| gzip -d alldb.sql.gz | |
| mysql -u root -p < alldb.sql | |
| Backup specific database: | |
| mysqldump -u root -p database_name > database_name.sql | |
| Restore specific database: | |
| mysql -u root -p database_name < database_name.sql | |
| Check MySQL version: | |
| mysql --version | |
| Connect to MySQL: | |
| mysql -u root -p | |
| -- Package Management -- | |
| Check package version before install (simulation): | |
| apt-get -s install <package> | |
| or | |
| apt-cache policy <package> | |
| or | |
| aptitude versions <package> | |
| Install package from specific version: | |
| apt-get install <package>=<version> | |
| List installed packages: | |
| apt list --installed | |
| Update single package: | |
| sudo apt-get install --only-upgrade <package> | |
| Remove package: | |
| sudo apt-get remove <package> | |
| Clean package cache: | |
| sudo apt-get clean | |
| sudo apt-get autoclean | |
| -- Compression & Archiving -- | |
| Maximum compress folder with tar and gzip: | |
| env GZIP=-9 tar -zcvf archive.tar.gz folder/ | |
| Compress with bzip2 (better compression): | |
| tar -cjvf archive.tar.bz2 folder/ | |
| Extract tar.gz: | |
| tar -xzvf archive.tar.gz | |
| Extract tar.bz2: | |
| tar -xjvf archive.tar.bz2 | |
| List contents without extracting: | |
| tar -tzf archive.tar.gz | |
| Tar/zip each subfolder separately: | |
| find . -maxdepth 1 -type d ! -path . | while read a_dir; do tar -zcvf ${a_dir}.tgz ${a_dir}; done | |
| -- Disk Usage & File Management -- | |
| Check folder sizes: | |
| sudo du -shc /var/* | |
| sudo du -sh /home/* | |
| Check disk usage summary: | |
| df -h | |
| Check inode usage: | |
| df -i | |
| Interactive disk usage analysis: | |
| sudo apt-get install ncdu | |
| sudo ncdu / | |
| Move all files from subfolders to current folder: | |
| find . -mindepth 2 -type f -print -exec mv {} . \; | |
| Count files in current folder (no subfolders): | |
| find . -type f | wc -l | |
| Count files recursively: | |
| find . -type f | wc -l | |
| Count subfolders at current level: | |
| ls -lR | grep ^d | wc -l | |
| Find all folders total (including subdirectories): | |
| find . -type d | wc -l | |
| Find folders in root only (no subdirectories): | |
| find . -maxdepth 1 -type d | wc -l | |
| Delete empty folders: | |
| find . -empty -type d -delete | |
| Efficiently delete large folders with many files: | |
| mkdir empty_dir | |
| rsync -a --delete empty_dir/ yourdirectory/ | |
| rm -R empty_dir | |
| -- File Operations -- | |
| Find files by name: | |
| find . -name "*.log" | |
| Find files by size: | |
| find . -size +100M | |
| find . -size -1M | |
| Find files modified in last day: | |
| find . -mtime -1 | |
| Find and delete files: | |
| find . -name "*.tmp" -delete | |
| Find and compress files: | |
| find . -name "*.log" -exec gzip {} \; | |
| Recursive copy preserving structure: | |
| rsync -a --include '*/' --include '*.pdf' --exclude '*' source/ destination/ | |
| Search for text in files: | |
| grep -r "search_term" ./ | |
| grep -r "search_term" ./ --include="*.php" | |
| Search and replace in files: | |
| sed -i 's/old_text/new_text/g' filename | |
| Replace in all files of a type: | |
| find . -name "*.php" -exec sed -i 's/old/new/g' {} \; | |
| Count lines in file: | |
| wc -l filename | |
| Count total lines in directory: | |
| find . -name "*.php" -exec wc -l {} + | tail -1 | |
| -- Monitoring & Logs -- | |
| Real-time system monitoring: | |
| top | |
| htop (install if needed: sudo apt-get install htop) | |
| Monitor specific process: | |
| ps aux | grep <process_name> | |
| Monitor disk I/O: | |
| iostat | |
| Memory usage: | |
| free -h | |
| View system logs: | |
| sudo tail -f /var/log/syslog | |
| View Apache error logs: | |
| sudo tail -f /var/log/apache2/error.log | |
| View Nginx error logs: | |
| sudo tail -f /var/log/nginx/error.log | |
| View application logs (example): | |
| sudo tail -f /var/log/application.log | |
| -- Network & SSH -- | |
| Check open ports: | |
| sudo netstat -tulpn | |
| ss -tulpn | |
| SSH with specific key: | |
| ssh -i ~/.ssh/custom_key user@hostname | |
| SSH tunnel (local port forwarding): | |
| ssh -NTf -L localhost:3306:remoteserver.com:3306 [email protected] | |
| SSH config file for persistent settings: | |
| nano ~/.ssh/config | |
| Host myserver | |
| HostName api.example.com | |
| User myuser | |
| IdentityFile ~/.ssh/id_rsa_custom | |
| LocalForward 3306 localhost:3306 | |
| ServerAliveInterval 60 | |
| Connect using config: | |
| ssh myserver | |
| Check SSH key fingerprint: | |
| ssh-keygen -lf ~/.ssh/id_rsa.pub | |
| View SSH connection history: | |
| grep "Accepted publickey" /var/log/auth.log | tail -10 | |
| -- Development & Python -- | |
| Keep pip updated (fixes many dependency issues): | |
| pip install --upgrade pip | |
| pip3 install --upgrade pip | |
| Install Python package: | |
| pip install package_name | |
| Create virtual environment: | |
| python3 -m venv myenv | |
| source myenv/bin/activate | |
| Deactivate virtual environment: | |
| deactivate | |
| List installed packages: | |
| pip list | |
| pip freeze | |
| Export requirements: | |
| pip freeze > requirements.txt | |
| Install from requirements: | |
| pip install -r requirements.txt | |
| -- Git Operations -- | |
| Initialize git repo: | |
| git init | |
| Clone repo: | |
| git clone https://github.com/username/repo.git | |
| Add files: | |
| git add . | |
| git add filename | |
| Commit: | |
| git commit -m "Commit message" | |
| Push to remote: | |
| git push origin main | |
| Pull from remote: | |
| git pull | |
| Check status: | |
| git status | |
| View log: | |
| git log --oneline | |
| Create branch: | |
| git checkout -b new_branch | |
| Switch branch: | |
| git checkout branch_name | |
| Merge branch: | |
| git merge branch_name | |
| -- Performance & Optimization -- | |
| Learn about ./configure && make && make install: | |
| https://thoughtbot.com/blog/the-magic-behind-configure-make-make-install | |
| Check file count in large directories: | |
| find /var/www -type f | wc -l | |
| Find largest files: | |
| find / -type f -size +100M | sort -k5 -hr | head -10 | |
| Process count by user: | |
| ps -u username | wc -l | |
| Find zombie processes: | |
| ps aux | grep zombie | |
| Kill process by name: | |
| pkill -f process_name | |
| Kill process by PID: | |
| kill -9 <PID> | |
| -- User & Permission Management -- | |
| Add new user: | |
| sudo useradd -m -s /bin/bash username | |
| sudo passwd username | |
| Add user to sudo group: | |
| sudo usermod -aG sudo username | |
| Add user to group: | |
| sudo usermod -aG groupname username | |
| List user groups: | |
| groups username | |
| id username | |
| Change file ownership: | |
| sudo chown user:group filename | |
| sudo chown -R user:group directory/ | |
| Change file permissions: | |
| chmod 755 filename | |
| chmod -R 755 directory/ | |
| Set default permissions for new files: | |
| umask 0022 | |
| -- Useful Commands -- | |
| System uptime: | |
| uptime | |
| Current date/time: | |
| date | |
| Check hostname: | |
| hostname | |
| Set hostname: | |
| sudo hostnamectl set-hostname newhostname | |
| Measure command execution time: | |
| time command | |
| Redirect output to file: | |
| command > output.txt | |
| Append to file: | |
| command >> output.txt | |
| Redirect errors to file: | |
| command 2> errors.txt | |
| Redirect both output and errors: | |
| command > output.txt 2>&1 | |
| Pipe output to another command: | |
| command1 | command2 | |
| Execute command in background: | |
| command & | |
| List background jobs: | |
| jobs | |
| Bring job to foreground: | |
| fg %1 | |
| Continue stopped job in background: | |
| bg %1 | |
| Create symbolic link: | |
| ln -s /path/to/file /path/to/link | |
| Remove symbolic link: | |
| rm /path/to/link | |
| Create file with content: | |
| echo "content" > filename.txt | |
| Append to file: | |
| echo "content" >> filename.txt | |
| View file: | |
| cat filename | |
| less filename (pagination) | |
| more filename (pagination) | |
| Edit file: | |
| nano filename | |
| vi filename | |
| vim filename | |
| Download file: | |
| wget https://example.com/file.zip | |
| curl -O https://example.com/file.zip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment