How to Configure SSH Server Settings on Ubuntu
To configure the SSH (Secure Shell) server settings on Ubuntu Linux, you must edit a specific system configuration file. This article identifies that file, explains how to safely open and modify it, and details the steps required to apply your changes so that your server remains secure and accessible.
The SSH Server Configuration File
On Ubuntu Linux, the primary configuration file for the SSH daemon (the server-side component of SSH) is:
/etc/ssh/sshd_config
Note: Be careful not to confuse this with
/etc/ssh/ssh_config, which is used to configure the SSH
client settings, not the server.
How to Edit the Configuration File
Since /etc/ssh/sshd_config is a system-critical file, it
is owned by the root user. To edit it, you must use a text editor with
administrative privileges (using sudo).
To open the file using the Nano text editor, run the following command in your terminal:
sudo nano /etc/ssh/sshd_configOnce the file is open, you can modify various settings. Some of the most common configurations include:
- Port: Change the default port (22) to a custom port to reduce automated brute-force attacks.
- PermitRootLogin: Disable root login
(
PermitRootLogin no) to force users to log in with a standard account and elevate privileges only when necessary. - PasswordAuthentication: Disable password-based
logins (
PasswordAuthentication no) to enforce more secure SSH key-based authentication.
Applying the Changes
After editing the file, save your changes and exit the text editor
(in Nano, press Ctrl+O to save, Enter to
confirm, and Ctrl+X to exit).
The changes will not take effect until the SSH service is restarted. Before restarting, it is highly recommended to test the configuration file for syntax errors by running:
sudo sshd -tIf no errors are returned, restart the SSH service with the following command:
sudo systemctl restart sshAlways keep your current SSH terminal window open while testing the new configuration in a separate window. This ensures you can revert the changes if you accidentally lock yourself out of the server.