A default Linux installation is built for convenience, not security. The first hour after spinning up a fresh VPS or dedicated server is when attackers scan for weak SSH passwords, open services, and default configurations. I've seen brute-force login attempts land within minutes of a server going live.
This server security checklist walks through twelve concrete steps to lock down a new Linux box before you deploy anything. Most take under five minutes. A few require planning. All of them reduce your attack surface.
1. Disable root SSH login and password authentication
Root login over SSH is the single biggest target. Disable it entirely and force key-based authentication.
First, create a non-root user with sudo privileges:
adduser deploy
usermod -aG sudo deploy
Copy your SSH public key into /home/deploy/.ssh/authorized_keys, set ownership, and test that you can log in as deploy before continuing.
Then edit /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
Restart SSH:
systemctl restart sshd
Verify you can still connect using your key, then close your root session. From now on you'll log in as deploy and escalate with sudo when needed.
2. Change the default SSH port
Port 22 gets hammered by automated scanners. Moving SSH to a non-standard port won't stop a determined attacker, but it will drop the noise in your auth logs by ninety percent.
Pick a port above 1024 and below 65535 that isn't already in use. I typically use something in the 2000–9000 range. Edit /etc/ssh/sshd_config:
Port 2222
If you're running a firewall (and you should be), open the new port before restarting SSH:
ufw allow 2222/tcp
systemctl restart sshd
Connect on the new port to confirm it works:
ssh -p 2222 deploy@your-server-ip
Only after verifying the new port do you close port 22.
3. Install and configure fail2ban
Fail2ban watches your logs for repeated authentication failures and temporarily blocks the offending IP addresses. It's a simple intrusion-prevention layer.
Install it:
apt install fail2ban
Create a local config file at /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
[sshd]
enabled = true
port = 2222
Restart the service:
systemctl enable fail2ban
systemctl restart fail2ban
Check banned IPs with fail2ban-client status sshd. You'll see bans accumulate within hours on any public-facing server.
4. Enable a firewall and restrict inbound traffic
Only open the ports you actually need. Everything else should be blocked by default.
UFW (Uncomplicated Firewall) is the simplest option on Debian and Ubuntu systems:
apt install ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow 2222/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
On RHEL or CentOS systems, use firewalld:
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
After enabling the firewall, verify you can still SSH in and that your web services respond. If you lock yourself out, most hosting providers offer an emergency console.
5. Set up automatic security updates
Manual patching is good. Automated patching for security fixes is better. Critical vulnerabilities get published and exploited within days. Staying current matters.
On Debian and Ubuntu, install unattended-upgrades:
apt install unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades
Edit /etc/apt/apt.conf.d/50unattended-upgrades to enable security updates only:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
On RHEL-based systems, use dnf-automatic:
dnf install dnf-automatic
systemctl enable --now dnf-automatic.timer
Configure /etc/dnf/automatic.conf to apply security updates automatically:
apply_updates = yes
Check logs weekly to confirm updates are running and that no package conflicts broke the automation.
6. Restrict user privileges with sudo
Never give users blanket sudo access unless they genuinely need it. Most application accounts should have zero sudo privileges.
For users who do need elevated access, grant only the specific commands they require. Edit /etc/sudoers using visudo:
deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx, /usr/bin/systemctl reload nginx
That line lets the deploy user restart or reload nginx without a password prompt, but nothing else. Avoid wildcards and avoid NOPASSWD for destructive commands.
Application service accounts (like www-data or mysql) should never appear in the sudoers file.
7. Disable unused services and remove unnecessary packages
Every installed package is potential attack surface. If you're not using a service, remove it.
List running services:
systemctl list-units --type=service --state=running
Disable anything you don't recognize or need:
systemctl stop cups
systemctl disable cups
Remove packages you'll never use:
apt autoremove --purge
apt purge telnet rsh-client
In support tickets I handled, bluetooth services and printer daemons running on headless VPS instances were common surprises. Check what's installed.
8. Harden shared memory and prevent privilege escalation
Shared memory at /run/shm can be exploited to bypass permission checks. Mount it with noexec, nosuid, and nodev.
Edit /etc/fstab and add:
tmpfs /run/shm tmpfs defaults,noexec,nosuid,nodev 0 0
Remount immediately:
mount -o remount /run/shm
This blocks execution of binaries from shared memory, which stops a common class of privilege-escalation attacks.
9. Configure file permissions and umask
Default file creation permissions are often too permissive. Set a sensible umask for all users.
Edit /etc/profile and /etc/bash.bashrc to include:
umask 027
That ensures new files are created with rw-r----- (640) and directories with rwxr-x--- (750). Group members can read, but the world cannot.
For sensitive directories like /etc/ssh, verify permissions manually:
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
chmod 644 /etc/ssh/ssh_host_*_key.pub
chmod 600 /etc/ssh/ssh_host_*_key
10. Enable and configure auditd for logging
The audit daemon tracks security-relevant events at the kernel level. It logs privilege escalations, file access, and authentication attempts that don't appear in standard syslog.
Install auditd:
apt install auditd audispd-plugins
Add rules to /etc/audit/rules.d/audit.rules:
-w /etc/passwd -p wa -k user_modification
-w /etc/shadow -p wa -k user_modification
-w /etc/sudoers -p wa -k sudoers_changes
-w /var/log/auth.log -p wa -k auth_log_changes
Restart the service:
systemctl restart auditd
Query the audit log:
ausearch -k user_modification
Audit logs are verbose. Pair them with a log-management tool or at minimum rotate them regularly.
11. Install and configure a host-based intrusion detection system
RKHunter (Rootkit Hunter) scans for known rootkits, backdoors, and suspicious file modifications.
Install it:
apt install rkhunter
Update the database and run an initial scan:
rkhunter --update
rkhunter --propupd
rkhunter --check --skip-keypress
Configure daily automated scans by editing /etc/default/rkhunter:
CRON_DAILY_RUN="yes"
REPORT_EMAIL="[email protected]"
Check /var/log/rkhunter.log after the first run to baseline what's normal for your system. False positives happen, especially after package updates, so review warnings before panicking.
12. Set up kernel hardening via sysctl
The Linux kernel exposes dozens of tunable parameters that affect security. A few minutes of sysctl tweaks add meaningful protection.
Edit /etc/sysctl.conf or create /etc/sysctl.d/99-security.conf:
# IP forwarding (disable unless this is a router)
net.ipv4.ip_forward = 0
# Disable source packet routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# Ignore ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
# Ignore ping requests (optional)
net.ipv4.icmp_echo_ignore_all = 1
# Enable TCP SYN cookies
net.ipv4.tcp_syncookies = 1
# Log martian packets
net.ipv4.conf.all.log_martians = 1
Apply the changes:
sysctl -p
These settings protect against IP spoofing, ICMP-based attacks, and SYN floods. They won't stop every attack, but they raise the bar.
What to check first after hardening
Once you've worked through the checklist, verify that SSH still works, your firewall rules allow the services you need, and automatic updates are running. Check /var/log/auth.log for brute-force attempts and /var/log/fail2ban.log for bans. Run rkhunter and ausearch to confirm logging is capturing events.
Hardening isn't a one-time task. Review your configuration quarterly, subscribe to security mailing lists for your distribution, and monitor CVE databases for software you run. Keep your server patched, your logs readable, and your attack surface small.
Frequently asked questions
Do I need all twelve steps on every server?
Most of them, yes. If you're running a single-purpose box with no sensitive data and no public services, you can skip intrusion detection and audit logging. SSH hardening, firewall, and automatic updates are non-negotiable.
Will changing the SSH port actually help?
It won't stop a targeted attack, but it will reduce log noise and wasted bandwidth from bots hammering port 22. Combined with key-based auth and fail2ban, it's a small win.
Should I disable ping entirely?
Only if you're paranoid or dealing with persistent ICMP-based attacks. Blocking ping makes basic network troubleshooting harder and doesn't add much security. I leave it enabled unless there's a specific reason not to.
How often should I review audit logs?
Weekly at minimum. Daily if the server handles sensitive data. Automate alerts for specific events like sudoers modifications or failed root login attempts so you're not reading gigabytes of logs manually.
Hardening is maintenance, not a milestone
Security is an ongoing process. This checklist gets a fresh server locked down in under an hour, but staying secure means monitoring logs, applying patches, and revisiting your configuration as your server's role evolves. The attacks landing on port 22 today are different from the ones you'll see next year. Adapt accordingly.
