Skip to content

Instantly share code, notes, and snippets.

@eplt
Last active February 16, 2024 07:51
Show Gist options
  • Save eplt/0ddea14011ec35fbbcac8a193f2e3e02 to your computer and use it in GitHub Desktop.
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
I use Ubuntu 18.04 Droplet on DigitalOcean often, this is a cheatlist of commands that I use often.
Use my link https://m.do.co/t/b298d6966c0c (Discount code for newbies, I get some free referral credits too) to sign up and start playing.
--- Check your OS version
cat /etc/os-release
--- DigitalOcean Ubuntu 14.04 do-agent (monitoring) install
curl -L -o ./install.sh https://insights.nyc3.cdn.digitaloceanspaces.com/install.sh
sed -i '17s/https/http/' install.sh
chmod 775 install.sh
./install.sh
--- If needed, clear the known_hosts file/record before connecting
pico ~/.ssh/known_hosts
--- Digital Ocean onitoring agent, you should do it now. Remove old one first.
sudo apt-get purge do-agent
curl -sSL https://insights.nyc3.cdn.digitaloceanspaces.com/install.sh | sudo bash
---
On starting a new server
-- Set the locales
sudo dpkg-reconfigure locales
-- Update system to latest
sudo apt-get update
sudo apt-get upgrade
-- Adding SSH key to your server, assuming you have setup your SSH key already
ssh-copy-id root@<server ip>
or
cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> .ssh/authorized_keys && echo "Key copied"'
-- 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
-- Setup Let's Encrypt
cd /usr/local/sbin
sudo wget https://dl.eff.org/certbot-auto
sudo chmod a+x /usr/local/sbin/certbot-auto
certbot-auto --nginx -d <domainname> -d www.<domainname>
certbot-auto --apache -d <domainname> -d www.<domainname>
sudo crontab -e
30 2 * * 1 /usr/local/sbin/certbot-auto renew >> /var/log/le-renew.log
-- Also, a nice trick with crontab, the @reboot keyword for startup tasks
@reboot /path/to/script
-- Alternatively, it's common to use rc.local
sudo nano /etc/rc.local
-- Note that if /etc/rc.local did not exist, it needs to start with:
#!/bin/bash
-- Then make sure the file is executable
sudo chmod a+x /etc/rc.local
-- Know your firewall, these are the few commands you need. Check the status, list all rules even when ufw is inactive. don't activate ufw unless you are sure you wont' kick yourself out of SSH. :)
ufw status verbose
ufw show added
ufw status numbered
ufw delete NUM
sudo ufw allow http
or
sudo ufw allow 80
ufw allow https
or
sudo ufw allow 443
sudo ufw allow 6000:6007/tcp
sudo ufw allow 6000:6007/udp
sudo ufw allow from 203.0.113.4
sudo ufw allow from 203.0.113.4 to any port 22
sudo ufw deny http
sudo ufw disable
sudo ufw reset
sudo ufw enable
-- Backup mysql server to a file
mysqldump -u root -p --all-databases > alldb.sql
gzip -9 alldb.sql
-- Restore mysql server from a file
gzip -d alldb.sql
mysql -u root -p < alldb.sql
-- Maximum compress a folder with tar and gzip
env GZIP=-9 tar -zcvf archive.tar.gz folder/
-- Checking Version of LAMP Components
cat /proc/version
apache2 -v
mysql -V
php -v
-- Checking a package version before install, using simulation mode
apt-get -s install <package>
-- Or use aptitude
aptitude versions <package>
--
Counting and Moving Files around (When handling folders with 1000s of files)
-- Check folder sizes
sudo du -shc /var/*
-- Move all files in all 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 current level subfolders
ls -lR | grep ^d | wc -l
-- Find all folders in total, including subdirectories
find . -type d | wc -l
-- Find all folders in the root directory (not including subdirectories)
find . -maxdepth 1 -type d | wc -l
-- Delete all empty folders
find . -empty -type d -delete
-- If you have a folder with many many files (say 10k+ files inside), use rsync to "delete" it
mkdir empty_dir
rsync -a --delete empty_dir/ yourdirectory/
rm -R empty_dir
-- Typical php.ini location
pico /etc/php/7.2/apache2/php.ini
systemctl restart apache2
-- To learn about sudo make install
https://thoughtbot.com/blog/the-magic-behind-configure-make-make-install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment