SSH Client: Automatically Connect to a Servers’ Non-Standard Port

If you run an SSH server, or many SSH servers, then you may have set the SSH daemon to run on a non-standard port. SSH normally listens on port 22, but with the large amount of script/bot attacks now on this port, attempting random logins, changing it to something different is a quick way to stop the majority of these automated attacks.  I’ve detailed this in another post here.

Once you’ve done this, though, you probably get bored with having to specify the port every time you connect by typing ssh -p 1234 hostname or scp -P 1234 hostname (where 1234 is the port SSH is running on hostname) – I know I do!

You can get around this problem, by using the ~/.ssh/config file (config in the .ssh directory in your home directory).

You can add multiple hosts to this file, along with their port, so you can simply ssh hostname and it’ll connect  using whatever port is set in that file.

The entry you need to add is:

  Port 1234

Where is the hostname you SSH too (if you SSH to an IP address, then this will need to be set here) and Port 1234 is the port number SSH is running on hostname.

Once this is done, you’ll be able to:


… and it’ll know that you need to connect on the port set in the file.

You can add multiple hosts to this file, if you access multiple SSH servers:

  Port 1234
  Port 4567

Give it a try – it’s certainly saved me some time!

You may also want to checkout another post on with further options for the ~/.ssh/config file: