I want to change which port sshd
uses on a Mac server. For example, let's say from port 22 to port 32.
Editing /etc/sshd_config
does not seem to work. Does anyone know how to change it? I'd prefer a method that's compatible with all OSX versions (or as many as possible, at least).
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
/System/Library/LaunchDaemons/ssh.plist
:Note:
To be able to edit this file on El Capitan, Sierra and probably future versions as well, you need to disable SIP (System Integrity Protection). See How do I disable System Integrity Protection (SIP).
For Catalina, even after disabling SIP, the volumes are unwritable. Use
sudo mount -uw /
in order to enable writing to/System
. Do the change then restore SIP and reboot.The above edit will also force sshd to listen only over IPV4.
After making any changes to
ssh.plist
, the file must be reloaded as follows:Note that using
launchctl stop ...
andlaunchctl start ...
will NOT reload this file.The man page with more information can be found by typing
man launchd.plist
or using this link.If you want sshd to listen on an additional port, you can add multiple entries to the Sockets dictionary.
Example:
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.
sudo mv ./socat-1.7.3.2.tar.gz /usr/local/bin/socat-1.7.3.2.tar.gz
)cd /usr/local/bin
)sudo tar -xvzf socat-1.7.3.2.tar.gz
cd ./socat-1.7.3.2
sudo ./configure && sudo make && sudo make install
)sudo socat TCP-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.
You may also easily prepare a script that runs at start up to rebuild the socat redirection each you restart your machine. Place this in
/Library/LaunchDaemons/com.serverfault.sshdredirect.plist
:Use
sudo launchctl load -w /Library/LaunchDaemons/com.serverfault.sshdredirect.plist
to load it. It'll automatically load on future reboots.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.
Enjoy.
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!)