A Linux system administrator is responsible for keeping their servers secure. There are a multitude of tools and software packages available to keep a networked Linux system safe from malicious intruders. In this hands-on lab, we will learn how to move away from always-on services to those that use systemd socket units. Socket units only provide access to a network service when an incoming connection requests it. To further enchance the security of the service, we will apply TCP wrappers to allow incoming connections to a specified service.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Configure `sshd` to use sockets.
You will be using an active SSH connection to the server when you log in.
Verify that the
sshd.socket
unit is not active:systemctl status sshd.socket
The output should show that it is not active.
We will need to set up an
at
job that will stop thesshd.service
unit and start thesshd.socket
for us. Run the following commands in this sequence:sudo at now + 3 minutes at> systemctl stop sshd.service at> systemctl start sshd.socket at> <EOT>
The <EOT> is from the key combination Ctrl+D. After three minutes, the
sshd.service
will stop, and thesshd.socket
unit will take over for your connection, so your remote shell should not disconnect. If it does, SSH back into the learning activity again.After the time has expired on the
at
job, verify that thesshd.socket
unit is active and running:systemctl status sshd.socket
Now any new secure shell connections that come into the system will utilize an on-demand socket. This way, the server does not have to keep a running secure shell running.
Enable the socket for SSH and disable the service for SSH:
sudo systemctl enable sshd.socket sudo systemctl disable sshd.service
- Set up TCP wrappers to only allow SSH.
First, verify that the
sshd
server has been compiled to use TCP wrappers:ldd /usr/sbin/sshd | grep libwrap
You should see that the
sshd
binary is capable of being used with TCP wrappers.Edit the
/etc/hosts.allow
file, and add the following:sshd2 sshd : ALL
This will permit incoming SSH connections from any network.
Now set up a default deny rule for TCP wrappers to deny any other incoming connections. Edit the
/etc/hosts.deny
file and add the following:ALL : ALL
Exit out of the SSH session and attempt to reconnect. Provided that the commands have been entered as described, you will be granted access back into the system.