First hour on a fresh VPS
These are the notes I run through every time I get a new small server. Nothing exotic: a safe login, a firewall, some swap, a better TCP congestion algorithm and automatic security patches. Commands assume a root shell on a fresh install.
Update the system
Start with a full upgrade, then reboot if the kernel was updated.
apt update && apt full-upgrade -y
apt install -y curl ca-certificates sudo ufw
reboot
Create a regular user
Working as root all the time is asking for trouble. Create a user and give it sudo.
adduser deploy
usermod -aG sudo deploy
On your own computer, copy your public key to the server:
ssh-copy-id deploy@your-server-ip
Open a second terminal and confirm that ssh deploy@your-server-ip works before you continue.
Lock down SSH
Once key login works, disable passwords and direct root login. A drop-in file keeps the main config untouched.
cat > /etc/ssh/sshd_config.d/10-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3
EOF
sshd -t && systemctl reload ssh
Keep your current session open until you have tested a new login in another window. If you made a typo, you will still have a way back in.
Changing the port is optional. It cuts log noise but does not add real security.
Firewall
UFW is a simple front end for nftables. Allow SSH first, otherwise you will lock yourself out.
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enable
ufw status verbose
If you changed the SSH port, allow that port instead of OpenSSH.
Add a swap file
Many small VPS plans ship without swap. A few gigabytes on disk will keep the OOM killer away during package upgrades and traffic spikes. On a 1 GB machine, 1 to 2 GB is plenty.
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
Lower the swappiness so the kernel prefers RAM and only swaps when it has to:
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
sysctl --system
free -h
If fallocate fails or the filesystem does not support it (some Btrfs and ZFS setups), create the file with dd if=/dev/zero of=/swapfile bs=1M count=2048 instead.
Enable BBR
BBR is a congestion control algorithm from Google. On links with some packet loss or high latency, it often gives noticeably better throughput than the default CUBIC. It needs kernel 4.9 or newer, which every current distribution has.
cat > /etc/sysctl.d/99-bbr.conf <<'EOF'
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
EOF
sysctl --system
Verify that it is active:
sysctl net.ipv4.tcp_congestion_control
lsmod | grep bbr
The first command should print bbr. Some containers (OpenVZ, LXC) share the host kernel and cannot change this setting. In that case ask your provider.
Time zone and NTP
Correct time matters for logs, TLS and cron. Most images already sync, but it takes five seconds to check.
timedatectl set-timezone Europe/Bucharest
timedatectl set-ntp true
timedatectl
Automatic security updates
Unattended upgrades install security patches by themselves. Fail2ban bans addresses that keep failing to log in.
apt install -y unattended-upgrades fail2ban
dpkg-reconfigure -plow unattended-upgrades
systemctl enable --now fail2ban
The defaults are fine for most servers. Check /var/log/unattended-upgrades/ now and then to see what was installed.
Final check
Reboot once and confirm that everything comes back on its own.
reboot
# after reconnecting:
swapon --show
sysctl net.ipv4.tcp_congestion_control
ufw status
systemctl is-active fail2ban
If all four look right, the base system is ready for whatever you want to run on it.