Fetching contributors…
Cannot retrieve contributors at this time
313 lines (184 sloc) 19.2 KB
author description keywords license alias modified modified_by published title
name email
Chris Walsh
This is a starting point of best practices for hardening a production server. Topics include user accounts, an iptables firewall, SSH and disabling unused network services.
security,secure,firewall,ssh,add user,quick start
Wednesday, March 29th, 2017
Friday, February 17th, 2012
Securing Your Server

In the Getting Started guide, you learned how to deploy a Linux distribution, boot your Linode and perform basic administrative tasks. Now it's time to harden your Linode against unauthorized access.

<iframe src="//" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="100%" height="100%"></iframe>
<script src="//" async></script>

Update Your System--Frequently

Keeping your software up to date 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.

Automatic Security Updates

There are arguments for and against automatic updates on servers. Fedora's Wiki has a good breakdown of the pros and cons, but the risk of automatic updates will be minimal if you limit them to security updates.

The practicality of automatic updates is something you must judge for yourself because it comes down to what you do with your Linode. Bear in mind that automatic updates apply only to packages sourced from repositories, not self-compiled applications. You may find it worthwhile to have a test environment that replicates your production server. Updates can be applied there and reviewed for issues before being applied to the live environment.

Add a Limited User Account

Up to this point, you have accessed your Linode as the root user, which has unlimited privileges and can execute any command--even one that could accidentally disrupt your server. We recommend creating 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.

{: .note}

Not all Linux distributions include sudo on the system by default, but all the images provided by Linode have sudo in their package repositories. If you get the output sudo: command not found, install sudo before continuing.

To add a new user, first log in to your Linode via SSH.

CentOS / Fedora

  1. Create the user, replacing example_user with your desired username, and assign a password:

    useradd example_user && passwd example_user
  2. Add the user to the wheel group for sudo privileges:

    usermod -aG wheel example_user


  1. Create the user, replacing example_user with your desired username. You'll then be asked to assign the user a password:

    adduser example_user
  2. Add the user to the sudo group so you'll have administrative privileges:

    adduser example_user sudo


  1. Debian does not include sudo among their default packages. Use apt-get to install it:

    apt-get install sudo
  2. Create the user, replacing example_user with your desired username. You'll then be asked to assign the user a password:

    adduser example_user
  3. Add the user to the sudo group so you'll have administrative privileges:

    adduser example_user sudo

After creating your limited user, disconnect from your Linode:


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

ssh example_user@

Now you can administer your Linode 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.

Harden SSH Access

By default, password authentication is used to connect to your Linode 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 brute-force. In this section we'll create a key-pair and configure the Linode to not accept passwords for SSH logins.

Create an Authentication Key-pair

  1. This is done on your local computer, not your Linode, 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 / OS X

    {: .caution}

    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 in /home/your_username/.ssh before entering your passphrase.


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

  2. Upload the public key to your Linode. Replace example_user with the name of the user you plan to administer the server as, and with your Linode's IP address.


    From your local computer:

    ssh-copy-id example_user@

    OS X

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

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

    From your local computer:

    scp ~/.ssh/ example_user@

    {: .note}

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


    • Option 1: This can be done using WinSCP. In the login window, enter your Linode's public 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 Linode. 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 Linode. 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 Linode (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 700 -R ~/.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.

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

SSH Daemon Options

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

    {: .file-excerpt} /etc/ssh/sshd_config : ~~~ conf # Authentication: ... PermitRootLogin no ~~~

  2. 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 #.

    {: .file-excerpt} /etc/ssh/sshd_config : ~~~ conf # Change to no to disable tunnelled clear text passwords PasswordAuthentication no ~~~

    {: .note}

    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.

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

    Use the option:

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

    The AddressFamily option is usually not in the sshd_config file by default. Add it to the end of the file:

    echo 'AddressFamily inet' | sudo tee -a /etc/ssh/sshd_config
  4. 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 ssh restart

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. 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: Securing Your Server with Fail2ban.

Remove Unused Network-Facing Services

Most Linux distributions install with running network services which listen for incoming connections from the internet, the loopback interface, or a combination of both. Network-facing services which are not needed should be removed from the system to reduce the attack surface of both running processes and installed packages.

Determine Running Services

To see your Linode's running network services:

sudo netstat -tulpn

{: .note}

If netstat isn't included in your Linux distribution by default, install the package net-tools or use the ss -tulpn command instead.

The following is an example of netstat's output. Note that because distributions run different services by default, your output will differ. If you are unsure of what a service does, do an internet search to understand what it is before attempting to remove or disable it.

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0   *               LISTEN      7315/rpcbind
tcp        0      0    *               LISTEN      3277/sshd
tcp        0      0  *               LISTEN      3179/exim4
tcp        0      0 *               LISTEN      2845/rpc.statd
tcp6       0      0 :::48745                :::*                    LISTEN      2845/rpc.statd
tcp6       0      0 :::111                  :::*                    LISTEN      7315/rpcbind
tcp6       0      0 :::22                   :::*                    LISTEN      3277/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      3179/exim4
udp        0      0 *                           2845/rpc.statd
udp        0      0 *                           2845/rpc.statd
udp        0      0   *                           7315/rpcbind
udp        0      0 *                           3327/ntpd
udp        0      0 *                           3327/ntpd
udp        0      0   *                           3327/ntpd
udp        0      0   *                           7315/rpcbind
udp6       0      0 :::111                  :::*                                7315/rpcbind
udp6       0      0 fe80::f03c:91ff:fec:123 :::*                                3327/ntpd
udp6       0      0 2001:DB8::123           :::*                                3327/ntpd
udp6       0      0 ::1:123                 :::*                                3327/ntpd
udp6       0      0 :::123                  :::*                                3327/ntpd
udp6       0      0 :::705                  :::*                                7315/rpcbind
udp6       0      0 :::60671                :::*                                2845/rpc.statd

Netstat tells us that services are running for Remote Procedure Call (rpc.statd and rpcbind), SSH (sshd), NTPdate (ntpd) and Exim (exim4).


See the Local Address column of the netstat readout. The process rpcbind is listening on and :::111 for a foreign address of* or :::*. This means that it's accepting incoming TCP connections from other RPC clients on any external address, both IPv4 and IPv6, from any port and over any network interface. We see similar for SSH, and that Exim is listening locally for traffic from the loopback interface, as shown by the address.


UDP sockets are stateless, meaning they are either open or closed and every process's connection is independent of those which occurred before and after. This is in contrast to TCP connection states such as LISTEN, ESTABLISHED and CLOSE_WAIT.

Our netstat output shows that NTPdate is: 1) accepting incoming connections on the Linode's public IP address; 2) communicates over localhost; and 3) accepts connections from external sources. These are over port 123, and both IPv4 and IPv6. We also see more sockets open for RPC.

Determine Which Services to Remove

If you were to do a basic TCP and UDP nmap scan of your Linode without a firewall enabled, SSH, RPC and NTPdate would be present in the result with ports open. By configuring a firewall you can filter those ports, with the exception of SSH because it must allow your incoming connections. Ideally, however, the unused services should be disabled.

  • You will likely be administering your server primarily through an SSH connection, so that service needs to stay. As mentioned above, RSA keys and Fail2Ban can help protect SSH.

  • An NTP daemon is one option for your server's timekeeping but there are alternatives. If you prefer a time synchronization method which does not hold open network ports, and you do not need nanosecond accuracy, then you may be interested in replacing NTPdate with OpenNTPD.

  • Exim and RPC, however, are unnecessary unless you have a specific use for them, and should be removed.

Uninstall the Listening Services

How to remove the offending packages will differ depending on your distribution's package manager.


sudo pacman -Rs package_name


sudo yum remove package_name

Debian / Ubuntu

sudo apt-get purge package_name


sudo dnf remove package_name

Run sudo netstat -tulpn again. You should now only see listening services for SSH (sshd) and NTP (ntpdate, network time protocol).

Configure a Firewall

Using a firewall to block unwanted inbound traffic to your Linode 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.

Next Steps

These are the most basic steps to harden any Linux server, but further security layers will depend on its intended use. Additional techniques can include application configurations, using intrusion detection or installing a form of access control.

Now you can begin setting up your Linode for any purpose you choose. We have a library of documentation to assist you with a variety of topics ranging from migration from shared hosting to enabling two-factor authentication to hosting a website.