Cheatsheet - Setup Linux VPS

October 3, 2024

Update Ubuntu

sudo apt update && sudo apt upgrade -y

Add a New User (You will be prompted for a password)

sudo adduser yourusername

Grant Root Access to the New User

sudo usermod -aG sudo yourusername

Switch to the New User

su - yourusername

Configure UFW (Firewall)

Allow SSH on port 22 and enable UFW:

sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status

Set Up SSH Access

  1. Create the .ssh directory:

    mkdir ~/.ssh
    chmod 700 ~/.ssh
  2. Add your SSH public key:

    nano ~/.ssh/authorized_keys
  3. Set appropriate permissions:

    chmod 600 ~/.ssh/authorized_keys

Configure SSH for Keep-Alive and Security

Edit the SSH server config:

sudo nano /etc/ssh/sshd_config

At the end of the file, add or modify the following lines:

  • Keep SSH Connections Alive:

    ClientAliveInterval 120
    ClientAliveCountMax 720
  • Allow or Deny Root Login (set to yes if you need root login, otherwise no for security):

    PermitRootLogin yes
  • Disable Password Authentication (if using SSH keys only):

    PasswordAuthentication no

After editing, save the file and restart the SSH service:

sudo systemctl restart ssh

Enable and Configure Swap

1. Check Existing Swap Information

sudo swapon --show
free -h
df -h

2. Create a Swap File (1 GB example)

sudo fallocate -l 1G /swapfile
ls -lh /swapfile

3. Enable the Swap File

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show

4. Verify Swap Usage

free -h

5. Make the Swap File Permanent

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This ensures that the swap file persists across reboots.