How to SSH into Ubuntu on a Custom Port

Connecting to an Ubuntu Linux SSH server using a non-standard port is a common security practice to prevent automated brute-force attacks. This article provides the exact command needed to establish this connection, explains the syntax, and offers a quick configuration tip to simplify your future remote logins.

The SSH Command for Custom Ports

To connect to an SSH server running on a non-standard port, use the -p flag (lowercase “p”) followed by the port number. The basic syntax is:

ssh -p [port_number] [username]@[server_ip_address]

Example Usage

If your remote Ubuntu user is ubuntu, the server’s IP address is 192.168.1.50, and the custom SSH port is configured to 2222, run the following command in your terminal:

ssh -p 2222 ubuntu@192.168.1.50

Once you run this command, you will be prompted to accept the server’s ECDSA key fingerprint (if connecting for the first time) and then enter your user password or SSH key passphrase to gain access.

Simplify Connections with SSH Config

If you connect to this server frequently, typing the port number every time can be tedious. You can automate this by adding the connection details to your local SSH configuration file.

  1. Open (or create) your local SSH config file:

    nano ~/.ssh/config
  2. Add the following configuration block:

    Host myubuntu
        HostName 192.168.1.50
        User ubuntu
        Port 2222
  3. Save and close the file.

Now, you can connect to your server by simply typing:

ssh myubuntu