configure ssh on ubuntu server

admin3 April 2024Last Update :

Mastering SSH Configuration on Ubuntu Server: A Comprehensive Guide

configure ssh on ubuntu server

Welcome to the definitive guide on configuring Secure Shell (SSH) on your Ubuntu server. In this digital era, where security and efficiency are paramount, mastering SSH is not just a skill—it’s a necessity for any tech professional. This article will take you through the intricate steps of setting up and securing SSH, ensuring that your server communications are both robust and impenetrable. Whether you’re managing a single server or an entire fleet, these insights will elevate your server administration game.

Understanding SSH and Its Importance

Before we dive into the configuration process, let’s understand what SSH is and why it’s crucial for server management. SSH, or Secure Shell, is a network protocol that provides administrators with a secure way to access a remote computer. It is widely used by system administrators for managing systems and applications remotely, allowing them to log into another computer over a network, execute commands in a remote machine, and move files from one machine to another.

SSH encrypts the session, making the connection secure from eavesdropping, connection hijacking, and other attacks. It’s an essential tool for anyone who needs to maintain the confidentiality and integrity of data while performing remote operations.

Prerequisites for Configuring SSH on Ubuntu Server

Before proceeding with the SSH setup, ensure that you have:

  • An Ubuntu server set up and running.
  • A user account with sudo privileges.
  • Access to a terminal/command line interface.
  • An updated system – run
    sudo apt update && sudo apt upgrade

    to ensure all packages are up to date.

Step-by-Step SSH Installation and Initial Configuration

Installing OpenSSH Server

To begin, install the OpenSSH server software on your Ubuntu system. OpenSSH is the most popular and widely used SSH implementation. Install it by running the following command:

sudo apt install openssh-server

Once installed, the SSH service should start automatically. You can check its status with:

sudo systemctl status ssh

Configuring the SSH Daemon

The main configuration file for SSH is located at /etc/ssh/sshd_config. Before making changes, it’s good practice to back up the original configuration file:

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original

Now, open the file with your preferred text editor, such as nano:

sudo nano /etc/ssh/sshd_config

Here are some critical configurations you might consider:

  • Changing the default SSH port (Port 22) to a non-standard port to deter automated attacks.
  • Disabling root login over SSH (PermitRootLogin no) to enhance security.
  • Limiting user access via SSH by specifying allowed users (AllowUsers username).
  • Using public key authentication instead of password-based authentication for added security.

After making your desired changes, save the file and restart the SSH service to apply them:

sudo systemctl restart ssh

Securing Your SSH Server

Implementing Key-Based Authentication

Passwords can be vulnerable to brute-force attacks, so using SSH keys is a more secure method of authentication. To set up key-based authentication:

  1. Generate a new SSH key pair on your local machine with


  2. Copy the public key to your Ubuntu server using
    ssh-copy-id username@server_ip


  3. Once copied, disable password authentication on your server by editing /etc/ssh/sshd_config and setting PasswordAuthentication no.
  4. Restart the SSH service to enforce the new settings.

Setting Up a Firewall

Ubuntu comes with ufw – Uncomplicated Firewall, which simplifies firewall management. To allow only SSH connections (on the port you’ve configured), use:

sudo ufw allow [your_ssh_port]/tcp

Then enable the firewall with:

sudo ufw enable

Always ensure that the firewall is configured to allow your SSH port before enabling it to prevent being locked out of your server.

Advanced SSH Server Tweaks

Tuning SSH Performance

You can improve SSH performance by tweaking some settings in the /etc/ssh/sshd_config file. For example, you can enable compression to speed up data transfer over slow connections by setting Compression yes.

Customizing SSH Sessions

For convenience, you can customize your SSH sessions by adding aliases, changing the MOTD (Message of the Day), or setting environment variables specific to SSH sessions.

Frequently Asked Questions

How do I change my SSH port on Ubuntu?

Edit the /etc/ssh/sshd_config file and change the Port directive to your desired port number. Remember to update your firewall settings accordingly.

Can I use SSH without a password?

Yes, by setting up key-based authentication, you can securely access your server without a password.

Is it safe to disable password authentication for SSH?

If you have set up key-based authentication correctly, it is generally safer to disable password authentication to protect against brute-force attacks.

What should I do if I’m locked out of my server after changing SSH settings?

If you have physical access to the server or console access through a hosting provider, you can log in directly and revert the changes. Always test new settings in a separate session before logging out of the current session.


Configuring SSH on an Ubuntu server is a critical task for maintaining a secure and efficient remote management environment. By following the steps outlined in this guide, you’ll be well-equipped to set up, secure, and optimize your SSH server. Remember, security is an ongoing process; keep abreast of best practices and regularly audit your configurations to ensure your server remains fortified against threats.


Leave a Comment

Your email address will not be published. Required fields are marked *

Comments Rules :

Breaking News