Host Security with TCP Wrappers and Systemd Sockets

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

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.

  1. Verify that the sshd.socket unit is not active:

    systemctl status sshd.socket

    The output should show that it is not active.

  2. We will need to set up an at job that will stop the sshd.service unit and start the sshd.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 the sshd.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.

  3. After the time has expired on the at job, verify that the sshd.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.

  4. 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.
  1. 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.

  2. Edit the /etc/hosts.allow file, and add the following:

    sshd2 sshd : ALL

    This will permit incoming SSH connections from any network.

  3. 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.

Additional Resources

Secure your system using the TCP Wrappers using the Systemd Socket Units. Check that the sshd.socket unit is not running and then create an at job that will stop the SSH service and then start the sshd.socket service. After three minutes, verify that the sshd.socket unit is running.

Enable the socket unit while disabling the SSHD service so that this change is permanent. Using TCP Wrappers, configure the service so that it allows specific connections to use SSH. Edit the appropriate file to allow SSH incoming from any network.

Be sure to DENY any other connections from any other systems to any other services. Verify you can then reconnect to the SSH server as expected.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?