This guide provides some tips and advice on how to make your Linux server more secure. Since servers must be accessible from outside, a basic protection against unauthorized access is definitely recommended and should not be neglected.
Please note that these instructions are not exhaustive and more detailed information can be found in other areas of the ZAP documentation. (e.g., 2FA (SSH))
The easiest way to protect your server is always the same, no matter which server: Use strong passwords, regularly update your services, and generally pay attention to which services you install and which instructions you follow.
| Service | Port |
|---|---|
| SSH | 22 |
| FTP | 21 |
| 25 | |
| DNS | 53 |
| HTTP | 80 |
| HTTPS | 443 |
Services like SSH or FTP always use the same default ports (some of which 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 do not change these ports, ports 22 and 21 are usually the targets to directly execute commands on the server or access files via FTP.
To prevent this, we recommend configuring the ports of standard services to custom values. The next section explains how to do this:
Your desired port must be between 1024 and 65535 and must not be already in use!
You can view some standard ports with cat /etc/services to avoid choosing an occupied port.
To change the SSH port, you need to edit the configuration file. This is usually located at /etc/ssh/sshd_config. If not, you can find it with the following command:
find / -name "sshd_config" 2>/dev/null
Open the file with nano (as root or using sudo).
sudo nano /etc/ssh/sshd_config
Insert your desired port after Port. If Port is commented out (e.g., #Port 22), remove the "#" and replace 22 with your chosen port. Save the file (in Nano with Ctrl + o) and close it (Nano: Ctrl + x).
You must now restart the SSH service for the changes to take effect.
# On Ubuntu/Debian/CentOS for example
sudo systemctl restart sshd
If you have installed an FTP service like proFTPd, you can also easily change the port via the configuration file. The procedure is similar to SSH.
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 desired port. Observe the notes above to avoid choosing 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 the server with likely malicious intent. Fail2Ban monitors log files for suspicious activity and thus secures your system relatively easily and effectively.
After installation, Fail2Ban comes with preconfigured settings for some commonly used services, including:
- apache
- lighttpd
- sshd
- qmail
- postfix
- Courier Mail Server Additional services can be easily added using regular expressions (RegEx) and specifying the desired log file.
/var/log/auth.log. This file contains all SSH login attempts, whether successful or failed.
Here is 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 authentications. Since the log file directly contains the attacker's IP address, Fail2Ban can block this IP after several 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 with 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 not yet active or configured. Check /etc/fail2ban, where you should currently find the following files:
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 make all settings in the jail.local file, including which services to monitor.
Look only at the part after [Default]. The Default section is for general basic settings.
Scroll down a bit until you find this section and adjust it, for example, like this:
| Attribute | Explanation | 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 | Time period in which failed logins are considered | 10m |
| maxretry | Number of failed attempts that lead to a ban | 5 |
# can be defined separated by spaces (and/or commas).
#ignoreip = 127.0.0.1/8 ::1
# ignorecommand = /path/to/command <ip>
ignorecommand =
# "bantime" is the number of seconds a host is banned.
bantime = 1h
# A host is banned if it has "maxretry" failures within the last "findtime"
# seconds.
findtime = 10m
# "maxretry" is the number of failures before a host is banned.
maxretry = 5
With this, you have completed the basic settings. To monitor the SSH service, scroll further down to the [sshd] tag. If you changed your port, you should enter it under port.
The [sshd] tag looks like this:
[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 4
As you can see, you can also make individual settings for each service (here, e.g., maxretry, which is lower than the default value). Although you have already made the general settings, you can override most settings per service. If you do not, the general setting is used.
Now you just 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 Fail2Ban works by blocking yourself. Using a VPN or a hotspot via your phone should give you a different IP to test Fail2Ban.
Do not test this on your normal network, as your own IP address could be blocked and you could lock yourself out.
Try to establish an SSH connection to your server (with a different IP!) and enter the wrong password each time. The result should look 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 is now refused by your Fail2Ban-protected server (Connection refused instead of Permission denied).
Now show the status of Fail2Ban. Here you see that an IP address has been banned.
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 unban the IP again, you can do so with the following command: fail2ban-client set sshd unbanip {your IP}.
If you have an unusually high number of IP bans, it is advisable to increase 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
# Ban time should increase with each ban of this IP
bantime.increment = true
# Scaling factor of 24 hours (1h,24h,48h,3d,4d....)
bantime.factor = 24
# Maximum ban time = 5 weeks
bantime.maxtime = 5w
