Ubuntu Server First Steps
Ubuntu Server First Steps
Section titled “Ubuntu Server First Steps”After spinning up a fresh Ubuntu server, there are a few critical steps to take before running any workloads. This guide covers system updates, SSH hardening, firewall setup, and basic security checks.
Prerequisites
Section titled “Prerequisites”- A fresh Ubuntu 22.04 LTS or 24.04 server
- SSH access to the server
- Root or sudo privileges
- A local machine for generating SSH keys (if needed)
Step 1: Update System Packages
Section titled “Step 1: Update System Packages”Connect to your server and update all packages to the latest versions.
sudo apt updatesudo apt upgrade -ysudo apt autoremove -yVerify the update completed successfully:
apt list --upgradableIf the output is empty, all packages are current.
Step 2: Set Hostname and Timezone
Section titled “Step 2: Set Hostname and Timezone”Set a descriptive hostname for your server:
sudo hostnamectl set-hostname my-serverVerify the change:
hostnamectlSet the timezone to match your location:
sudo timedatectl set-timezone America/New_YorktimedatectlStep 3: Configure SSH Access
Section titled “Step 3: Configure SSH Access”Generate SSH Keys (on your local machine)
Section titled “Generate SSH Keys (on your local machine)”If you don’t have an SSH key pair, generate one:
ssh-keygen -t ed25519 -C "your-email@example.com"This creates ~/.ssh/id_ed25519 (private key) and ~/.ssh/id_ed25519.pub (public key).
Copy Public Key to Server
Section titled “Copy Public Key to Server”Copy your public key to the server’s authorized_keys file:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ipOr manually:
mkdir -p ~/.sshecho "your-public-key-content" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keyschmod 700 ~/.sshHarden SSH Configuration
Section titled “Harden SSH Configuration”Edit the SSH daemon configuration:
sudo nano /etc/ssh/sshd_configApply these changes:
#Port 22Port 22
#PermitRootLogin prohibit-passwordPermitRootLogin no
#PubkeyAuthentication yesPubkeyAuthentication yes
#PasswordAuthentication yesPasswordAuthentication no
#PermitEmptyPasswords noPermitEmptyPasswords no
#X11Forwarding yesX11Forwarding noRestart SSH to apply changes:
sudo systemctl restart sshStep 4: Configure Firewall
Section titled “Step 4: Configure Firewall”Enable and configure UFW (Uncomplicated Firewall):
sudo ufw default deny incomingsudo ufw default allow outgoingsudo ufw allow 22/tcpsudo ufw enableVerify firewall status:
sudo ufw status verboseStep 5: Create a Non-Root User
Section titled “Step 5: Create a Non-Root User”Create a dedicated user account for daily work:
sudo useradd -m -s /bin/bash -G sudo myusersudo passwd myuserTest login with the new user:
ssh myuser@server-ipStep 6: Disable Root Login
Section titled “Step 6: Disable Root Login”Prevent direct root login by locking the root account:
sudo passwd -l rootVerify the lock:
sudo passwd -S rootYou should see “root L” (locked) in the output.
Verification Checklist
Section titled “Verification Checklist”Run these commands to verify your setup:
| Check | Command | Expected Result |
|---|---|---|
| System updated | apt list --upgradable | Empty output |
| Hostname set | hostnamectl | Shows your hostname |
| SSH keys working | ssh user@server-ip | Logs in without password |
| Firewall active | sudo ufw status | Status: active |
| Root locked | sudo passwd -S root | Shows “root L” |
Troubleshooting
Section titled “Troubleshooting”SSH Connection Refused
Section titled “SSH Connection Refused”Check that SSH is running:
sudo systemctl status sshRestart if needed:
sudo systemctl restart sshLocked Out of SSH
Section titled “Locked Out of SSH”If you changed SSH settings and can’t connect, use console access or recovery mode to fix /etc/ssh/sshd_config and restart SSH.
Firewall Blocking SSH
Section titled “Firewall Blocking SSH”If you can’t connect after enabling UFW, add the SSH rule:
sudo ufw allow 22/tcpsudo ufw reloadNext Steps
Section titled “Next Steps”Once your server is hardened, you can:
- Install Docker for running containerized services
- Set up monitoring and log aggregation
- Configure automated backups
- Deploy your first application
See the Docker Compose Notes for the next step.