I want to change sshd's port on Mac OS X from default 22 to 32, but editing /etc/sshd_config seems does not work.
How can I change it?
Every previous answer is working (as google suggest too), but they are dirty and inelegant.
So the solution is as simple as to use the port number instead of the service name.
An excerpt from my edited
The above will also force sshd to listen only over IPV4.
After making any changes to
Note that using
The man page with more information can be found by typing
If you want sshd to listen on an additional port, you can add multiple entries to the Sockets dictionary.
In order to change the port on Mac OS-X for your SSH daemon, follow the following steps:
Now you should be able to run SSH command to your external IP as follows:
Better than all the options above, this is the easiest way to change your default SSH port in OS X.
I use this one-liner script in Terminal to update my Mac's default SSH Port:
I can't take the credit, all kudos go to the Desert Crystal Tech Blog - OSX SSH Script here.
From what I read (and experienced) so far, there are three main methods which can be used:
Another way to do it, which I personally by far prefer to all and each of these methods, because it avoids messing around with Mac OS X system files is using socat to redirect port 22 to whichever port you want.
A. Download socat: hhttp://www.dest-unreach.org/socat/download/socat-188.8.131.52.tar.gz
B. Move the tar.gz file to your /usr/local/ directory (sudo mv ./socat-184.108.40.206.tar.gz /usr/local/socat-220.127.116.11.tar.gz)
C. Go to your /usr/local directory (cd /usr/local)
D. Uncompress: sudo tar -xvzf socat-18.104.22.168.tar.gz
E. Move to the uncompressed file directory: cd ./socat-22.214.171.124
F. Run the usual configure, make and make install to install socat (sudo ./configure && make && make install)
G. Redirect port 22 (default ssh) to any port you want (in the following ex., 2222) using the correct option by sending a socat call (socat tcp4-listen:2222,reuseaddr,fork TCP:localhost:22)
You're done and your mac os x system files are left unchanged. In addition, this method works not only on Snow Leopard, but on all versions of Mac OS X and also on any machine on which socat may run.
The last thing you need to do if you use a router/firewall is to include the correct redirect commands in your router/firewall.
Also, it avoids getting stuck into the debate whether the ssh.plist method, the services method or the whatever method is better, more elegant or worse than the other.
With a little research, you may also easily prepare a script that runs at start up to rebuild the socat redirection each you restart your machine;
In addition, you can also improve security by (i) setting your firewall to block any connections to your port 22 from any other interface than the loopback (127.0.0.1) and (ii) make a similar change in your sshd.conf file to have ssh listen on the loopback only.
I couldn't see this documented anywhere properly in a man page, but if you want to do nothing more than add an extra listener, you can use an array of listeners and have an extra dict. This doesn't require editing /etc/services if you use the port directly (but remember to open up the port on your firewall!)
I was able to get this to work by just changing my SSH config file. It's actually a better solution to me because it's a change that applies to just the host I specify.
If you edit .ssh/config
Restart the shell and ssh that address.