Skip to content

VPS: Linux Security Tips

Learn how to improve the security of your Linux server with essential tips to prevent unauthorized access and protect your services → Find out more

10 November 202611 min read0 Views0 helpful

Introduction

This guide includes tips and advice on how to make your Linux server more secure. Since VPSs need to be accessible from outside, basic protection against unwanted access is definitely recommended and should not be neglected.

Info

Please note that these instructions are not exhaustive. You can find more detailed information in other areas of the ZAP documentation (e.g., 2FA (SSH))

Tip

The easiest way to protect your server is always the same, regardless of the server: use secure passwords, update your services regularly, and generally be careful about which services you install and which instructions you follow.

Configuring service ports

ServicePort
SSH22
FTP21
Mail25
DNS53
HTTP80
HTTPS443

Services such as SSH or FTP always use the same default ports (some of these are listed in the table above). If an external attacker tries to brute-force your SSH service, they first need to know which port SSH uses. If you don't configure these ports differently, ports 22 and 21 are usually the first ones an attacker will try to execute commands directly on the server or access files via FTP.

To prevent this, we recommend assigning custom ports to standard services. Find out how in the next section:

Warning

Your chosen port must be between 1024 and 65535 and must not already be in use!

Use cat /etc/services to view some standard ports so you don't choose one that's already occupied.

SSH port

To change the SSH port, you need to edit the configuration file. It is usually located at /etc/ssh/sshd_config. If it isn't, you can find it with the following command:

Code
find / -name "sshd_config" 2>/dev/null

Now open the file with nano (as root or using sudo).

Code
sudo nano /etc/ssh/sshd_config

Enter your chosen port after Port. If Port is commented out (that is, #Port 22), remove the "#" and replace 22 with your chosen port. Save the file (in Nano, press Ctrl + o) and close it (in Nano, press Ctrl + x).

You now need to restart the SSH service for the changes to take effect.

Code
# Unter Ubuntu/Debian/CentOS z.B.
sudo systemctl restart sshd

FTP port

If you have installed an FTP service such as proFTPd, you can also easily change its port by editing the configuration file. The process is similar to changing the SSH service's port.

Find the configuration file proftpd.conf

Code
find / -name "proftpd.conf" 2>/dev/null

The file is usually located at /etc/proftpd.conf (CentOS) or /etc/proftpd/proftpd.conf (Ubuntu, Debian). Open the file with nano, remove the "#" before port, and enter your chosen port after it. Follow the notes above to make sure you don't enter an invalid port.

Tip

Use Ctrl + W to search in nano.

Code
nano /etc/proftpd/proftpd.conf

Additional protection with Fail2Ban

Fail2Ban is a service that blocks IP addresses that try to log in to your server with likely malicious intent. Fail2Ban monitors log files for suspicious activity, making it a relatively easy and effective way to secure your system.

After installation, Fail2Ban includes preconfigured settings for some commonly used services, including:

  • apache
  • lighttpd
  • sshd
  • qmail
  • postfix
  • Courier Mail Server You can easily add other services by specifying a regular expression (RegEx) and the log file you want to monitor.
As an example, let's look at an entry in /var/log/auth.log. This file contains all SSH login attempts, whether successful or failed.

Here's the entry:

Code
Dec 2 12:59:19 vps-zap515723-2 sshd[364126]: Failed password for root from 92.117.xxx.xxx port 52504 ssh2

Fail2Ban uses this log file and monitors it for failed authentication attempts. Since the log file includes the attacker's IP address, Fail2Ban can block that IP address after a number of failed attempts.

Installing Fail2Ban

First, log in to your Linux server. If you need help, follow our First access (SSH) guide. You should run the commands as root or using sudo.

Code
sudo apt update && sudo apt upgrade -y
sudo apt install fail2ban

After installation, you can check the status directly with systemctl. (Press Ctrl+C to exit systemctl.)

Code
systemctl status fail2ban.service
* fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor pres>
     Active: active (running) since Sat 2023-12-02 13:10:33 UTC; 24s ago
       Docs: man:fail2ban(1)
    Process: 23988 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, statu>
   Main PID: 23989 (fail2ban-server)
        CPU: 409ms
     CGroup: /system.slice/fail2ban.service
             `-23989 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

Dec 02 13:10:33 vps-zap515723-1 systemd[1]: Starting Fail2Ban Service...
Dec 02 13:10:33 vps-zap515723-1 systemd[1]: Started Fail2Ban Service.
Dec 02 13:10:34 vps-zap515723-1 fail2ban-server[23989]: Server ready

Configuring Fail2Ban

Fail2Ban is now installed, but it isn't active or configured yet. Check /etc/fail2ban; the following files should be there:

Code
action.d fail2ban.d jail.conf paths-arch.conf paths-debian.conf
fail2ban.conf filter.d jail.d paths-common.conf paths-opensuse.conf

To create an active "jail", you need to create a file named jail.local. Simply copy the contents of jail.conf into the new file and open it:

Code
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local

You can now configure all settings in the jail.local file, including which services to monitor. You should focus only on the section after [Default]. The Default section contains general basic settings.

Scroll down until you find this section, and adjust it, for example, as follows:

AttributeDescriptionValue
ignoreipIP addresses that should not be blocked127.0.0.1/8
ignorecommandNot relevant.
bantimeHow long an IP address remains blocked1h
findtimePeriod during which failed logins are counted10m
maxretryNumber of failed attempts that result in a ban5
Code
# kann mit Leerzeichen (und/oder Komma) getrennt definiert werden.
#ignoreip = 127.0.0.1/8 ::1

# ignorecommand = /path/to/command <ip>
ignorecommand =

# "bantime" ist die Anzahl der Sekunden, die ein Host gesperrt ist.
bantime  = 1h

# Ein Host wird gesperrt, wenn er "maxretry" Fehler innerhalb von "findtime"
# Sekunden erzeugt hat.
findtime  = 10m

# "maxretry" ist die Anzahl der Fehler, bevor ein Host gesperrt wird.
maxretry = 5

You've now completed the basic settings. To monitor the SSH service, for example, scroll down to the [sshd] tag. If you changed your port, enter it under port.

The [sshd] section should then look like this:

Code
[sshd]

enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 4
Tip

As you can see, you can also set individual options for specific services (for example, maxretry is lower than the default setting here). Even though you've set general options earlier, you can configure most settings separately for each service. If you don't, the general setting will be used.

Now you only need to restart Fail2Ban to start monitoring.

Code
sudo systemctl restart fail2ban.service

Checking that Fail2Ban works

If you have access to a VPN or a second server, you can test whether the service works by trying to get Fail2Ban to block you. Using a VPN or your phone's hotspot should give you a different IP address to test Fail2Ban with.

Warning

Don't test this on your regular network, as your own IP address could be blocked and you could lock yourself out.

Try connecting to your server via SSH (using a different IP address!) and enter the wrong password each time. The result should look something like this:

Code
root@185.223.29.xxx's password:
Permission denied, please try again.
root@185.223.29.xxx's password:
Permission denied, please try again.
root@185.223.29.xxx's password:
root@185.223.29.xxx: Permission denied (publickey,password).
root@vps-zap515723-2:/var/log# ssh root@185.223.29.179
root@185.223.29.xxx's password:
Permission denied, please try again.
root@185.223.29.xxx's password:
Permission denied, please try again.
root@185.223.29.xxx's password:
^C
root@vps-zap515723-2:/var/log# ssh root@185.223.29.xxx
ssh: connect to host 185.223.29.xxx port 22: Connection refused

As you can see, the connection to your Fail2Ban-protected server is now refused (Connection refused instead of Permission denied).

Now display the Fail2Ban status. You'll see that an IP address has been blocked.

Code
fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 4
|  |- Total failed:     8
|  `- File list:        /var/log/auth.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   xxx

If you want to unblock the IP again, you can do so with the following command: fail2ban-client set sshd unbanip {deine IP}.

Info

If you have an unusually high number of blocked IPs, we recommend increasing the ban time with each failed attempt to reduce the number of possible login attempts.

Code
[sshd]

enabled = true
port    = 22
filter  = sshd
logpath = /var/log/auth.log
maxretry = 4

bantime = 1h
# Bantime sollte mit jeder Sperre dieser IP steigen
bantime.increment = true
# Skalierungsfaktor von 24 Stunden (1h,24h,48h,3d,4d....)
bantime.factor = 24
# Maximale Sperrzeit = 5 Wochen
bantime.maxtime = 5w

FAQ

Frequently asked questions

Use secure passwords, update services regularly, and be careful about which services you install and which instructions you follow.
Enable authentication exclusively via SSH keys and disable password login. See the SSH key guide for details.
Port 22. Attackers trying to brute-force SSH first need to know which port it uses.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 Users found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP support team knows your product — briefly describe your issue and we'll help you personally.