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?
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+.
/etc/sshd_config
as many suggested online, but it won't working. Not sure if that's not a osx thing or just simply deprecated.
sshd
launchd plist to accomplish this.
Everything after the destination/address is passed to the target computer as a command. Try
ssh -p 1234 user@remote
instead.
-v
to the command line; this may help debuggingdebug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 47: Applying options for * debug1: Connecting to ip [ip] port 1234. debug1: connect to address ip port 1234: Connection refused