1

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?

8
  • Try adding -v to the command line; this may help debugging – lhf Feb 19 at 20:39
  • debug1: 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 – JackeyOL Feb 19 at 20:49
  • 1
    Then the server is not running on that port, or blocked by a firewall. – nohillside Feb 19 at 21:04
  • how do I get around with it? – JackeyOL Feb 19 at 21:10
  • @JackeyOL Without more info about your setup, it's impossible to tell. Is the remote Mac actually running an ssh server on port #1234? What firewalls lie between the client and that Mac? Is the remote Mac on a private network, and if so what IP are you using for it and is port mapping set up on the router for that private network? – Gordon Davisson Feb 19 at 22:17
0

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+.

2
  • Your answer is brilliant! I scanned my ports and 1234 is not open but 22 is still open and I think that's the reason. I enabled the port 1234 in /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. – JackeyOL Feb 27 at 23:39
  • @JackeyOL It's deprecated. The link I gave above will show you how to edit the sshd launchd plist to accomplish this. – pion Mar 1 at 7:58
0

Everything after the destination/address is passed to the target computer as a command. Try

ssh -p 1234 user@remote

instead.

1
  • No luck. Got exactly same result: Connection refused – JackeyOL Feb 19 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .