Change mac sshd port

  1. Open Terminal.app, type sudo vi /etc/services, type mac password, then press enter, enter edit mode
  2. Find ssh service line, like below:
1ssh    22/udp     # SSH Remote Login Protocol
2ssh    22/tcp     # SSH Remote Login Protocol
  1. Modify 22 to the port number you want to change, for example 10022, and save :wq to exit.
1sudo launchctl unload -w /System/Library/LaunchDaemons/ssh.plist
2sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
  1. Now you can login your mac with 10022 port.

Notice

When you change the default ssh port, you need to add -p parameter to connect the default 22 port. For example: ssh root@localhost will connect to the port 10022 which you DIY.

Change default ssh port

  1. Open Terminal.app, type sudo vi /etc/ssh/ssh_config, type mac password, then press enter, enter edit mode
  2. Find #Port 22,remove # comment, change to Port 22, save :wq exit.

Now you can use ssh command to connect to the default 22 port without -p parameter.