How to Secure an SSH Server in Ubuntu 14.04

Secure SSH Server In Ubuntu 14.04

SSH (Secure Socket Shell) is a command line interface and protocol for securely getting access to a remote Linux server. It provides a secure and encrypted communication over a network and allows data to be exchanged over a secure channel between two servers. It is widely used by system admins to control the Web and other types of servers remotely. In this article we are going to show you how you can secure your SSH server.

Note: this tutorial assumes that the SSH server is running Ubuntu 14.04, and the client machine is a Linux.

Getting started – install SSH

First, you need to update your system and install necessary packages to your system.

To update the system and install the SSH server on the server machine, run the following command:

To install SSH client on the client machine, run the following command:

Configure SSH for password-less login

There are two different methods of logging into an SSH server: one is password-based authentication and the other is key-based authentication. Password authentication is a very basic method which is easy to use and crack. Using password authentication is very insecure, especially if your user uses a weak password. On the other hand, SSH keys provide an easy and secure way of logging into a remote server, and this method is recommend for all users.

On your client machine, generate SSH keys with the following command:

Simply press the Enter key at every prompt. This produces two files: (public key) and id_rsa (private key).

This will output something that looks like the following:


On your server, create the following folder (if it doesn’t exist):

Back to your client machine, copy the “” file to your server using the following command:

Change “yourport” to the port number that your SSH server is using (the default is 22) and the “serverip” to the server’s IP address.

On your server machine, change the filename and setup permissions.

To test if the key-based authentication method works, try connecting to your SSH server from the client machine:

If you are able to connect without entering a password, then the key-based authentication method works.

Secure SSH configuration file

The “/etc/ssh/sshd_config” file is the system-wide configuration file for SSH which allows you to set different options to improve the security of an SSH server. The default configuration in the config file is very insecure, so you need to edit it first and set proper options to improve the security.

To edit the “/etc/ssh/sshd_config” file, run

Change SSH listening port

By default, SSH listens on port 22. Attackers use port scanners to see whether an SSH service is running or not. It is recommended to change the default port.

To change the default port to 2200, change:



Only use Protocol 2

Version 1 of the protocol contains security vulnerabilities. Protocol 2 is the default entry on Ubuntu.

Change the line shown below:

Limit users access

It is necessary to allow only specific users to log in to SSH. It can improve your security. By default, this option is not available in the SSH configuration file.

To allow “user1” and “user2,” add the following line:

To deny “baduser1” and “baduser2,” add the following line:

Disable root login

It is not necessary to log in as root via ssh over a network. Normal users can also use su or sudo to gain root level access. Most attackers will try to use root user to log in. This is a big security risk, so it is recommended to deny the root login.

To disable root login, change the line



Hide last login

You can hide who logged in last when a user logs in.

For this, change the line



Restrict the interface to log in

By default, ssh will listen on all network interfaces. If you want to allow an SSH connection to be accepted from specific IP addresses, you can change the line



Disable password uthentication

Using password authentication is a big security risk if your user uses a weak password. It is recommended to use “ssh keys.” An “ssh key” can contain over 600 random characters and be difficult to break.

For this, change the line



Disable .rhosts files

The .rhosts files specify which users can access the r-commands (rsh, rcp, rlogin, etc.) on the local machine without a password. By default an .rhosts file is disabled; if not, then change the lines as shown below.

Disable host-based authentication

SSH’s host-based authentication is more secure than .rhosts authentication. However, it is not recommended that hosts trust one another. By default, this option is disabled.

If not, then change the line shown below.

Set a login grace timeout

The “LoginGraceTime” specifies how long after a connection request the server will wait before disconnecting. It is recommended to reduce it to 60 seconds.

For this, change the line



Set maximum startup connections

Setting up a proper maximum number of concurrent connections to the SSH daemon can be helpful against a brute-force attack.

For this, change the line



Disable forwarding

The port forwarding technique is used by attackers to tunnel network connections through an SSH session to log into systems. It is recommend to disable this option.

For this, change the line



Log more information

By default, SSH logs everything. If you want to log more information like failed login attempts. you can change the value of this to “VERBOSE.”

For this, change the line



Disable empty passwords

It is necessary to deny users with empty passwords on your server. By default PermitEmptyPasswords is disabled in Ubuntu.

If not, then change the line shown below.

Set idle timeout interval

By default, this options is not available in the SSH default configuration file. It is recommended to set a proper idle timeout to avoid an unattended ssh session.

For this, add the following lines.

Strict mode

This will prevent the use of insecure home directory and key file permissions. By default, this option is enabled.

If not, then change the following line.

Now save and exit the /etc/ssh/sshd_config file and restart the SSH server.

Secure SSH using TCP wrappers

A TCP wrapper provides host-based access control to network services used to filter network access to the Internet. Edit your “/etc/hosts.allow” file to allow SSH only from and

Add the following line:

Secure SSH using iptables

By default, an SSH server must only accept connections from your LAN or other remote sites. It is recommended to allow only specific IP addresses to access SSH and block access to SSH to unauthorized IP addresses.

To allow SSH connections only from run the following command:

Disable SSH connection from all other hosts by running the following command:

Now save your new rules using the following command:


The above instructions are very powerful techniques for securing your SSH server. This post covers all of the information most users will need for an SSH server. If you have any questions feel free to comment below.

Reference: SSH ubuntu

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox