Change The Default SSH Port Mac OS X

back to tech articles
Mac OS X Snow Leopard, CentOS 6

A while back I wrote a few articles on improving security on your production server. A lot of chatter revolved around using non-standard ports for connecting via SSH. This affects other services that use SSH, such as Git, SFTP and others.

Today I’m going to show you a quick tip to set your SSH port permanently and spare you some typing.

Normally, an SSH connection would look like this:

1
$ ssh root@8.8.8.8

To improve security when connecting to the server, I suggested changing the port (on the server) so that your login would require a port to be specified, like so:

1
$ ssh -p 1234 root@8.8.8.8

This requires some other configuration to get certain services like Git and SFTP to work as expected. Take a look at these articles for more info:

We can save the extra -p 1234 part and a lot of configuration effort if we just change SSH’s default port on the local machine. It’s very easy to do:

1
$ vi ~/.ssh/config

And then we simply add the following lines:

1
2
Host *
  Port 1234

Save and restart SSH. This means every time we ask for an SSH connection, the default port that will be requested is 1234. Easy!

You can specify different ports for different hosts by adding more Host definitions, like so:

1
2
3
4
5
6
7
8
9
10
11
12
Host awesome
  User root
  HostName 192.168.1.1
  Port 1232

Host sauce
  User root
  HostName 192.168.1.2
  Port 1233

Host *
  Port 1234

Just make sure the last definition is your * one, the way I have it above.

Leave a Reply

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