This is simple. Open a terminal and follow the steps below. This worked for me on MacOS 10.13.6. Also, brew a great cup of coffee at home for inspiration.
- Open the Terminal.
- Run sudo vim /etc/services
- Enter your password
- Find the lines assigned to port 22, something like the image below
Replace both ports 22 with your desired ssh port using a number between 1024 and 32,767
Now you need to restart the services. Be sure to do this if you have direct access to the console for your MacOS. If you do it remotely you may loose connection. I could not find a way to ‘restart’ a service on MacOS as we normally do in Linux. You have to ‘unload’ and ‘load’ the service in MacOS. Use the below commands.
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
Now you can SSH using:
ssh <youruser>@<yourHostOrIP> -p <yourNewPort>
For example: ssh john@localhost -p 18765
Hope this small post was useful. If you like to learn more about MacOS you can grab this book MacOS and iOS Internals, Volume I: User Mode (v1.3)