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.
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))
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.
| Service | Port |
|---|---|
| SSH | 22 |
| FTP | 21 |
| 25 | |
| DNS | 53 |
| HTTP | 80 |
| HTTPS | 443 |
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:
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.
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:
find / -name "sshd_config" 2>/dev/null
Now open the file with nano (as root or using sudo).
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.
# Unter Ubuntu/Debian/CentOS z.B.
sudo systemctl restart sshd
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
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.
Use Ctrl + W to search in nano.
nano /etc/proftpd/proftpd.conf
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.
/var/log/auth.log. This file contains all SSH login attempts, whether successful or failed.
Here's the entry:
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.
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.
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.)
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
Fail2Ban is now installed, but it isn't active or configured yet. Check /etc/fail2ban; the following files should be there:
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:
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:
| Attribute | Description | Value |
|---|---|---|
| ignoreip | IP addresses that should not be blocked | 127.0.0.1/8 |
| ignorecommand | Not relevant. | |
| bantime | How long an IP address remains blocked | 1h |
| findtime | Period during which failed logins are counted | 10m |
| maxretry | Number of failed attempts that result in a ban | 5 |
# 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:
[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 4
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.
sudo systemctl restart fail2ban.service
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.
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:
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.
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}.
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.
[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
