Simple steps for securing your Linux system
Fail2ban Frustrates Attackers
The attacker usually leaves password guessing to an automated attack program, which stubbornly processes one entry after the other from a dictionary. You can protect yourself against this kind of attack. Tools such as Fail2ban do not completely prevent dictionary attacks, but they ramp up the time required for the attack to the extent that the attacker usually gives up in frustration.
Fail2Ban consists of a server daemon and a client that interprets the central configuration files fail2ban.conf
and jail.conf
and sends commands to the server. The program reads one or more logfiles and checks each line with regular expressions. For example, if a defined number of login attempts is exceeded, Fail2Ban can use iptables to block the IP address of the attacker for a configurable time.
You can install Fail2ban with the apt install fail2ban
command. Many distributions come with Fail2ban already preconfigured to let you start working with it and protect SSH (and often other services) right after installation without having to move a muscle. You can see whether Fail2ban is working on your system, after a certain wait, by checking the log, in this case /var/log/fail2ban.log
(Figure 6).
If you suspect that this protection is not yet enabled, check the configuration file /etc/fail2ban/jail.conf
. You will find a section starting with [sshd]
. If this section says enabled=false
, change the false
to true
and restart the service by typing systemctl restart ssh
. Fail2ban starts up immediately.
By default, the ban beam hits anyone who generates three login failures in 10 minutes or less. Fail2ban then blocks the incoming IP address for one hour. You can adapt these values to suit your own needs by changing the findtime
, maxretry
, and bantime
parameters in the configuration file.
Mail Me on Login
Intrusion Detection Systems (IDS) are usually complex applications that try to detect and report unusual activities on the system. But you can also go one size smaller. A one-liner (Listing 4) is all you need to keep track of who is logging on to the system and when.
Listing 4
Who's Logged In?
$ echo 'Login on' $(hostname) $(date) $(who) | mail -s "Login on $(hostname) $(who) | awk '{print $5}'" my_email
All you have to do is swap my_email
for your actual email address. Then open the /etc/bash.bashrc
file and append the one-liner to the file.
You will now receive an email every time a user logs on to the system. You can see the user name and the time of the login. For example, if our test user bob logs on to the influx system, you will receive a message like the one in the example (Figure 7).
Disable Services
Sometimes you try something on a system, play around with it, and then forget it. Forgotten services pose a security risk on exposed systems. It is useful to check from time to time what exactly is running on the system, and specifically, which services can be accessed from outside.
An easy way to check which services are running is the netstat -tlpn
command, which you need to call with administrative rights. netstat
provides a tabular list of all services and the ports to which they are bound.
On the system shown in Figure 8, for example, InfluxDB is running along with a few other services. netstat
shows that this service is open for arbitrary access from the network and listening for requests on port 8086 – showing 0.0.0.0:port_number
for IPv4 and :::port_number
for IPv6. On a conventional home network, port 8086 would still have to be forwarded from the WLAN router to the computer for the service to be accessible from the Internet.
Let's assume you don't have time to work on InfluxDB right now. You want to disable the service, but leave it installed so that you can continue using it later. To do this, stop InfluxDB with the systemctl stop influx
command first. The command is executed, but it does not give you any output. To check that InfluxDB really isn't running anymore, take a look at /var/log/syslog
or the Systemd log (Listing 5).
Listing 5
Checking /var/log/syslog
[...] Jul 18 11:22:37 influx influxd[11322]: ts=2020-07-18T10:22:37.769190Z lvl=info msg="Closed service" log_id=0O4MSfDG000 service=subscriber Jul 18 11:22:37 influx influxd[11322]: ts=2020-07-18T10:22:37.769493Z lvl=info msg="Server shutdown completed" log_id=0O4MSfDG000 Jul 18 11:22:37 influx systemd[1]: influxdb.service: Succeeded. [...]
However, the fact that you have stopped the service now does not mean that it will remain permanently disabled. After a restart, it would start again automatically. You can prevent the service from starting automatically by typing systemctl disable influxdb
. This command means that InfluxDB stays in place but no longer launches automatically. To enable the service again, type systemctl enable influxdb
.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Fedora 39 Beta is Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.
-
SUSE Going Private (Again)
The company behind SUSE Linux Enterprise, Rancher, and NeuVector recently announced that Marcel LUX III SARL (Marcel), its majority shareholder, intends to delist it from the Frankfurt Stock Exchange by way of a merger.