General Terminal Commands
Login via SSH
ssh root@88.191.361.182 # Login with root
Add User
adduser ubuntu # Add new user
adduser ubuntu sudo # Add user to super users
exit # Logout
clear # Clear terminal screen
ssh user@88.191.361.182 # Login with new user
Software Management
sudo apt-get update # Update package lists for Linux that uses APT
sudo apt-get install apache2 # Install apache2
sudo apt-get remove certbot # Remove old pre-installed Certbot
sudo apt-get install python3-pip python3-dev nginx # Install multiple packages
File Color
white: files
blue: folders
cyan: protected folder
red: zip and backup files
File Listing
ls # List current directory
ls /var/www/html/smg/static # List target directory
ll # List current directory in long format
ll /var/www/html/smg/static # List target directory in long format
Directory Management
pwd # Print working directory
mkdir smg/backup # Make directory
cd ~ # Enter home
cd / # Enter root (top-level)
cd .. # Enter parent directory
cd /var/www/html/ # Enter html directory
Edit with Nano
sudo nano /smg/project/settings.py # Edit target file
Delete/Remove
rm wsgi.py # Remove file
rm -r images/ # Remove folder and its files (recursively)
rm -rf foldername/* # Remove files inside folder except hidden files (recursively forcefully)
Copy
cp index.html /smg/templates/ # Copy file to target directory
cp -Rfv /root/.ssh /home/ubuntu/ # Copy file to target directory (recursively)
cp fox.txt fox-backup.txt # Duplicate file
sudo cp 000-default.conf django.conf # Sudo copy/duplicate a file
Secure Copy
# Copy securely file(s) from local machineto remote home directory
C:\> scp /Users/Me/Desktop/files.zip ubuntu@261.71.367.99:~
Move & Rename
mv ~/db.sqlite3 /var/www/html/smg/ # Move
mv *.txt docs/ # Move all text files to docs/ folder
mv settings.py _settings.py # Rename file
mv files/ media/ # Rename folder
Change File Ownership
chown -Rfv ubuntu:ubuntu /home/ubuntu/.ssh # Root user gives to user:group ownership of .ssh (recursively)
sudo chown www-data:www-data smg/ # Superuser gives to server ownership of smg/ directory
sudo chown -Rfv www-data:www-data smg/ # Superuser gives to server ownership of smg/ and all its files (recursively)
sudo chown www-data:www-data db.sqlite3 # Superuser gives to server ownership of the database file
sudo chown ubuntu:ubuntu ../ # Superuser gives to user `ubuntu` ownership of the parent directory
Compression
zip image.zip image.png # Zip a file
zip squash.zip file1 file2 file3 # Zip multiple files
zip -r squash.zip dir1 # Zip a directory (recursively)
unzip image.zip # Unzip a file
unzip squash.zip -d /home/ubuntu/ # Unzip file into a target folder (testing if its directory)
Third-Party-Specific Commands
Apache Commands
sudo apt-get install apache2 # Install apache2
sudo apt-get install libapache2-mod-wsgi-py3 # Allow Apache to serve Python 3 apps via WSGI interface
sudo a2enmod rewrite # Enable the modification of Apache
sudo nano /etc/apache2/apache2.conf # Configure apache2
sudo nano /etc/apache2/sites-available/django.conf # Edit a Django site's apache2 config file
sudo a2ensite django.conf # Enable the django.conf site
sudo a2dissite 000-default.conf # Disable the 000-default.conf site
sudo nano /var/log/apache2/error.log # View apache2 error log
sudo bash -c 'echo > /var/log/apache2/error.log' # Empty error log
sudo service apache2 restart # Restart apache2
Firewall Commands
sudo apt-get install ufw # Install firewall
sudo ufw allow 'Apache Full' # Allow Apache full access (e.g. HTTP and HTTPS)
sudo ufw allow ssh # Allow SSH
sudo ufw default allow outgoing # Default outgoing policy allowed
sudo ufw default deny incoming # Default incoming policy denied
sudo ufw enable # Enable firewall
sudo ufw status # Check firewall status
Git Access
ssh-keygen -t rsa # Create an SSH key – just follow the prompts
sudo cp -Rfv /home/ubuntu/.ssh /root/ # Copy the SSH keys from `ubuntu` user to `root`
cat ~/.ssh/id_rsa.pub # Output the public key, copy its code and paste it in GitHub
Virtual Environment
sudo apt-get install python3.10-venv # Version number depends on your Python version
cd myproj/ # Enter project folder
python3 -m venv venv # Create a virtual environment
source venv/bin/activate # Activate virtual environment
pip install django==4.2.8 # Install framework(s)
pip install -r requirements.txt # Install project dependencies
pip install gunicorn psycopg2-binary # Install multiple packages
pip freeze > requirements.txt # Write all installed packages into a `requirements.txt` file
deactivate # Deactivate virtual environment
Certbot SSL Certificate
sudo apt-get remove certbot # Remove old pre-installed Certbot
sudo snap install --classic certbot # Install Certbot
sudo ln -s /snap/bin/cerbot /usr/bin/certbot # Prepare the Certbot command by creating a symbolic link
sudo certbot --apache -d domain.com -d www.domain.com # Add Certbot certificates to your domains
sudo certbot renew --dry-run # Test automatic renewal of certificates