macOS – SSH into Another Mac on Different Network Using Non-Standard Port

macossshterminal

I tried to access one of my mac on a different network using a specified port: 1234

ssh remoteuser@remoteip -p 1234

but I got the result that connection refused. How do I specify the port I want to use other than the default 22?

Best Answer

From our comment exchange,

How did you tell your other Mac to run its ssh server on port 1234? – pion Feb 19 at 23:19

I set it on the router website, designating port 1234 for the remote Mac – JackeyOL Feb 20 at 5:25

it sounds like you did not actually configure sshd on your remote Mac to listen on port 1234 rather than its default port 22. This is why you're getting a refused connection: Although you may be routing traffic correctly to your remote Mac, sshd did not open port 1234 on that Mac in the first place.

You can confirm this by getting onto your remote Mac and either running a port scan (/System/Library/CoreServices/Applications/Network Utility.app, Port Scan, address 127.0.0.1, ports 1234-1234) or by trying to ssh locally (ssh -p 1234 user@localhost).

To fix this: You can change your sshd port by editing its launchd property list. Note that you will first need to disable System Integrity Protection on macOS 10.11+ and remount your System volume as read/write on macOS 10.15+.