by peter

Share

Building a Secure Linux System

After you have successfully created a system, or you have a new laptop/workstation or whatever, there are a few initial configuration steps you should perform to build your Secure Linux system. This includes updating your system, setting the timezone, configuring a custom hostname, adding a limited user, hardening SSH to prevent unauthorised access, and configuring a firewall. These steps ensure your instance is updated to date, secure, and ready for use. In this guide we are looking rather generally at the common versions of linux you might use to build a secure linux system.

1. Secure Linux needs System Updates

Building a secure Linux system means you need to start from an up-to-date OS version. Updating your system frequently is the single biggest security precaution you can take for any operating system. Software updates range from critical vulnerability patches to minor bug fixes and many software vulnerabilities are actually patched by the time they become public. Updating also provides you with the latest software versions available for your distribution.

Ubuntu, Debian, and Kali Linux
apt update && apt upgrade

When updating some packages, you may be prompted to use updated configuration files. If prompted, it is typically safer to keep the locally installed version”.

CentOS/RHEL Stream and Fedora
This includes CentOS Stream 8 (and above), CentOS 8, other RHEL derivatives (including AlmaLinux 8 and Rocky Linux 8), and Fedora.

dnf upgrade

Alpine
apk update && apk upgrade

Arch Linux
pacman -Syu

CentOS 7
yum update

Gentoo
emaint sync -a

After running a sync, it may end with a message that you should upgrade Portage using a –oneshot emerge command. If so, run the Portage update. Then update the rest of the system:

emerge -uDU --keep-going --with-bdeps=y @world

OpenSUSE
zypper update

Slackware
slackpkg update
slackpkg upgrade-all

2. Ensure your logs are time accurate

Most new Linux systems are set to UTC time by default. However, you may prefer to use the time zone in which you live so log file timestamps are relative to your local time. Ensuring time accuracy is crucial for secure Linux systems. You need to be 100% confident in your logs.

Most Distributions
This includes CentOS Stream 8 (and newer), CentOS 7 (and newer), other RHEL derivatives (including AlmaLinux 8 and Rocky Linux 8), Fedora, and Arch. These instructions also work for most Ubuntu, Debian, and OpenSuse distributions, though other methods may be preferred in those cases.

Use timedatectl to output a list of available timezones.

timedatectl list-timezones

Use the arrow keys, Page Up, and Page Down to navigate through the list. Copy or make note of your desired time zone and press q to exit the list.

Set the time zone using the command below, replacing America/New_York with your preferred time zone.

timedatectl set-timezone 'America/New_York'

Ubuntu, Debian, and Kali Linux
The instructions under the Most Distributions section above (which outlines the timedatectl command) are valid. That said, both Ubuntu and Debian come with a more friendly tool called tzdata, outlined below.

Open the tzdata tool.

dpkg-reconfigure tzdata

Select the continent of your choice using the arrow keys and press Enter.

Select your region using the arrow keys and press Enter.

Alpine
Use the setup-timezone command to initiate the timezone selection process:

setup-timezone

Enter the timezone you are located within. If you aren’t sure of the timezone string to use, enter ? to display a list of available timezones

If you selected a region with sub-timezones, enter ? again to see a list of available sub-timezones and then enter the sub-timezone you are located within.

Gentoo
View a list of available time zones.

ls /usr/share/zoneinfo

Write the selected time zone to /etc/timezone (for example, GMT for Greenwich Time).

echo "GMT" > /etc/timezone

Configure the sys-libs/timezone-data package, which sets /etc/localtime.

emerge --config sys-libs/timezone-data

OpenSUSE
The instructions under the Most Distributions section above (which outlines the timedatectl command) are valid. OpenSuse also has a more friendly way to select a timezone, discussed below.

Open the YaST2 timezone selector tool.

yast2 timezone

Use the arrow keys to select your region within the Region pane.

Press tab to switch to the Time Zone pane and then use the arrow keys to select your time zone or sub-region.

Press F10 to save the changes. Alternatively, press tab until the [OK] text button is highlighted. Then press enter.

Slackware
Run the timeconfig tool.

timeconfig

Select NO Hardware clock is set to local time.

Select a timezone.

Check the Time

Use the date command to view the current date and time according to your server.

root@localhost:~# date
Thu Mar 1 10:10:23 GMT 2016

3. Configure a Custom Hostname for your Secure Linux device

A hostname is used to identify your system using an easy-to-remember name. It can be descriptive and structured (detailing what the system is used for) or a generic word or phrase. While this is not essential, you will want to uniquely identify your secure Linux system on your network. Here are some examples of hostnames:

Descriptive and/or Structured: web, staging, blog, or something more structured like [purpose]-[number]-[environment] (ex: web-01-prod).

Generic/Series: Such as the name of a fruit (apple, watermelon), a planet (mercury, venus), or an animal (leopard, sloth).

This hostname can be used as part of an FQDN (fully qualified domain name) for the system (ex: web-01-prod.example.com).

After you’ve made the change below, you may need to log out and log back in again to see the terminal prompt change from localhost to your new hostname. The command hostname should also show it correctly. See our guide on using the host file if you want to configure a fully qualified domain name.

Most Distributions
This includes Ubuntu 16.04 (and newer), CentOS Stream 8 (and newer), CentOS 7 (and newer), and other RHEL derivatives (including AlmaLinux 8 and Rocky Linux 8), Debian 8 (and newer), Fedora, OpenSuse, Kali Linux, and Arch.

Replace example-hostname with one of your choices.

hostnamectl set-hostname example-hostname

Alpine
See Update Your Systems hosts File.

Gentoo
echo "HOSTNAME="example-hostname"" > /etc/conf.d/hostname
/etc/init.d/hostname restart

Slackware
echo "example-hostname" > /etc/HOSTNAME
hostname -F /etc/HOSTNAME

Update Your System’s hosts File

The hosts file creates static associations between IP addresses and hostnames or domains which the system prioritises before DNS for name resolution.

Open the hosts file in a text editor, such as Nano.

nano /etc/hosts

Add a line for your systems IP address. You can associate this address with your Fully Qualified Domain Name (FQDN) if you have one, and with the local hostname you set in the steps above. In the example below, 10.10.0.1 is the public IP address, example-hostname is the local hostname, and example-hostname.example.com is the FQDN.

File: /etc/hosts

127.0.0.1 localhost.localdomain localhost
10.10.0.1 example-hostname.example.com example-hostname

Add a line for your servers address. Applications requiring IPv6 will not work without this entry:

File: /etc/hosts

127.0.0.1 localhost.localdomain localhost
10.10.0.1 example-hostname.example.com example-hostname
2600:3c01::a123:b456:c789:d012 example-hostname.example.com example-hostname

The value you assign as your system’s FQDN should have an “A” record in DNS pointing to your Linode’s IPv4 address. For IPv6, you should also set up a DNS “AAAA” record pointing to your Linode’s IPv6 address.

See our guide to Adding DNS Records for more information on configuring DNS. For more information about the hosts file, see Using your System’s hosts file.

4. Add a Limited User Account

Up to this point, you have accessed your system as the root user, which has unlimited privileges and can execute any command–even one that could accidentally disrupt your server. In order to be Cyber Essentials and Cyber Essentials Plus certified, you must create a limited user account and using that at all times. Administrative tasks will be done using sudo to temporarily elevate your limited user’s privileges so you can administer your server.

Not all Linux distributions include sudo on the system by default. If you get the output sudo: command not found, install sudo before continuing.

Ubuntu and Debian
Create the user, replacing example_user with your desired username. You’ll then be asked to assign the user a password:

adduser example_user

Add the user to the sudo group so you’ll have administrative privileges:

adduser example_user sudo

CentOS/RHEL Stream and Fedora
Create the user, replacing example_user with your desired username, and assign a password:

useradd example_user && passwd example_user

Add the user to the wheel group for sudo privileges:

usermod -aG wheel example_user

Log in as the New User
After creating your limited user, disconnect from your system:

exit

Log back in as your new user. Replace example_user with your username, and the example IP address with your systems IP address:

ssh example_user@192.0.2.1

Now you can administer your system from your new user account instead of root. Nearly all superuser commands can be executed with sudo (example: sudo iptables -L -nv) and those commands will be logged to /var/log/auth.log.

5. Harden SSH Access

By default, password authentication is used to connect to your system via SSH. A cryptographic key pair is more secure because a private key takes the place of a password, which is generally much more difficult to decrypt by brute force. Using a key pairing is a solid form to two factor authentication that is mandated for administrative accounts in Cyber Essentials and Cyber Essentials Plus. In this section, we’ll create a key pair and configure your system to not accept passwords for SSH logins.

As of Autumn 2018, OpenSSH has been added to Windows 10, simplifying the process for securing SSH. Windows 10 in this guide assumes OpenSSH has been installed as part of this update, while Earlier Windows Versions would apply to earlier versions.

This is done on your local computer, not your system, and will create a 4096-bit RSA key-pair. During creation, you will be given the option to encrypt the private key with a passphrase. This means that it cannot be used without entering the passphrase, unless you save it to your local desktop’s keychain manager. We suggest you use the key-pair with a passphrase, but you can leave this field blank if you don’t want to use one.

Linux / macOS / Windows 10 or 11

If you’ve already created an RSA key-pair, this command will overwrite it, potentially locking you out of other systems. If you’ve already created a key-pair, skip this step. To check for existing keys, run ls ~/.ssh/id_rsa*.

ssh-keygen -b 4096

Press Enter to use the default names id_rsa and id_rsa.pub before entering your passphrase. On Linux and OS X, these files will be saved in the /home/your_username/.ssh directory. On Windows, they will be saved in C:UsersMyUserName.ssh

Earlier Windows Versions

This can be done using PuTTY as outlined in our guide: Use Public Key Authentication with SSH.

Upload the public key to your Compute Instance. Replace example_user with the name of the user you plan to administer the server as, and 192.0.2.1 with your instance’s IP address.

Linux

From your local computer:

ssh-copy-id example_user@192.0.2.1

macOS

On your Compute Instance (while signed in as your limited user):

mkdir -p ~/.ssh && sudo chmod -R 700 ~/.ssh/

From your local computer:

scp ~/.ssh/id_rsa.pub example_user@10.10.0.10:~/.ssh/authorized_keys

ssh-copy-id is available in Homebrew if you prefer it over SCP. Install with brew install ssh-copy-id.

Windows 10 or 11

On your system (while signed in as your limited user):

mkdir -p ~/.ssh && sudo chmod -R 700 ~/.ssh/

From your local computer:

scp C:UsersMyUserName.ssh/id_rsa.pub example_user@192.0.2.1:~/.ssh/authorized_keys

Earlier Windows Versions

Option 1: This can be done using WinSCP. In the login window, enter your systems IP address as the hostname, and your non-root username and password. Click Login to connect.

Once WinSCP has connected, you’ll see two main sections. The section on the left shows files on your local computer and the section on the right shows files on your system. Using the file explorer on the left, navigate to the file where you’ve saved your public key, select the public key file, and click Upload in the toolbar above.

You’ll be prompted to enter a path where you’d like to place the file on your system. Upload the file to /home/example_user/.ssh/authorized_keys, replacing example_user with your username.

Option 2: Copy the public key directly from the PuTTY key generator into the terminal emulator connected to your system(as a non-root user):

mkdir ~/.ssh; nano ~/.ssh/authorized_keys

The above command will open a blank file called authorized_keys in a text editor. Copy the public key into the text file, making sure it is copied as a single line exactly as it was generated by PuTTY. Press CTRL+X, then Y, then Enter to save the file.

Finally, you’ll want to set permissions for the public key directory and the key file itself:

sudo chmod -R 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

These commands provide an extra layer of security by preventing other users from accessing the public key directory as well as the file itself. For more information on how this works, see our guide on how to modify file permissions.

Now exit and log back into your system. If you specified a passphrase for your private key, you’ll need to enter it.

SSH Daemon Options
Open the SSH configuration file on your Compute Instance using a Linux text editor, such as nano or vim:

sudo nano /etc/ssh/sshd_config

Disallow root logins over SSH.

This requires all SSH connections be by non-root users. Once a limited user account is connected, administrative privileges are accessible either by using sudo or changing to a root shell using su -.

# Authentication:
...
PermitRootLogin no

Disable SSH password authentication. This requires all users connecting via SSH to use key authentication. Depending on the Linux distribution, the line PasswordAuthentication may need to be added, or uncommented by removing the leading #.

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

You may want to leave password authentication enabled if you connect to your Linode from many different computers. This will allow you to authenticate with a password instead of generating and uploading a key-pair for every device.

Listen on only one internet protocol. The SSH daemon listens for incoming connections over both IPv4 and IPv6 by default. Unless you need to SSH into your Linode using both protocols, disable whichever you do not need. This does not disable the protocol system-wide, it is only for the SSH daemon. Depending on the Linux distribution, the line AddressFamily may need to be added, or uncommented by removing the leading #

Use the option:

AddressFamily inet to listen only on IPv4.
AddressFamily inet6 to listen only on IPv6.

# Port 22
AddressFamily inet

Restart the SSH service to load the new configuration.

If you’re using a Linux distribution which uses systemd (CentOS 7, Debian 8, Fedora, Ubuntu 15.10+)

sudo systemctl restart sshd

If your init system is SystemV or Upstart (CentOS 6, Debian 7, Ubuntu 14.04):

sudo service sshd restart

6. Use Fail2Ban for SSH Login Protection

Fail2Ban is an application that bans IP addresses from logging into your server after too many failed login attempts and acts as a level of Firewalling and Intrusion Prevention, preventing brute force attacks. This level of protection should be applied to all services on your device to ensure a secure linux system. Since legitimate logins usually take no more than three tries to succeed (and with SSH keys, no more than one), a server being spammed with unsuccessful logins indicates attempted malicious access.

Fail2Ban can monitor a variety of protocols including SSH, HTTP, and SMTP. By default, Fail2Ban monitors SSH only, and is a helpful security deterrent for any server since the SSH daemon is usually configured to run constantly and listen for connections from any remote IP address.

For complete instructions on installing and configuring Fail2Ban, see our guide: A Tutorial for Using Fail2ban to Secure Your Server.

7. Configuring the Firewall

Using a firewall is mandatory for Cyber Essentials and Cyber Essentials Plus and you can not have a secure linux system without a host based firewall. Using a firewall to block unwanted inbound traffic to your system provides a highly effective security layer. By being very specific about the traffic you allow in, you can prevent intrusions and network mapping. A best practice is to allow only the traffic you need, and deny everything else. See our documentation on some of the most common firewall applications:

Iptables is the controller for netfilter, the Linux kernel’s packet filtering framework. Iptables is included in most Linux distributions by default.

FirewallD is the iptables controller available for the CentOS / Fedora family of distributions.

UFW provides an iptables frontend for Debian and Ubuntu.

Common Lockout Recovery Steps

If for whatever reason you find yourself locked out of your system after putting your security controls into place, there are still a number of ways that you can regain access to your system.

If you need to re-enable password authentication and/or root login over ssh to your system, you can do this by reversing the following sections of this file to reflect these changes

# Authentication:
...
PermitRootLogin yes
...
PasswordAuthentication yes

From there, you just need to restart SSH.

If you’re using a Linux distribution which uses systemd (CentOS 7, Debian 8, Fedora, Ubuntu 15.10+)

sudo systemctl restart sshd

If your init system is SystemV or Upstart (CentOS 6, Debian 7, Ubuntu 14.04):

sudo service sshd restart

If you need to remove your public key from your Linode, you can enter the following command:

rm ~/.ssh/authorized_keys

You can then replace your key by re-following the Create an Authentication Key-pair section of this guide.