3

I have multiple machines on my network that I access regularly using ssh. I want to enable remote access through port forwarding, which means I'll need separate sshd ports for each machine. That said, I'd like to continue to use port 22 for local access.

I realize that I have to add the configs to /etc/services and that the key to my answer lies in sshd.plist. I've attempted to follow various suggestions online, all of which seem to indicate that the plist files exist in different locations than mine do. One suggested ssh.plist and another sshd.plist. My changes to ssh.plist accomplished nothing. Another suggested changing sshd.plist, which didn't even exist on my machine, but /System/Library/LaunchDaemons/com.apple.FileSyncAgent.sshd.plist does. I was going to make and rename a copy of that file, but I couldn't tie it to a call in /etc/services. I made a copy anyway, but then I couldn't figure out how to modify the key as it's current value had no relationship to the file name (unlike the example for ssh.plist).

So, I currently have ssh and ssh-alt set in services, two ssh.plist files that vary based on the different file names (one ssh and the other ssh-alt) and two .sshd.plist files that are mirror images of each other. When I rebooted the machine, port 22 no longer works, but my alternate port does.

Any suggestions would be appreciated.

3
  • 1
    This Q/A might be what you need - let us know if your issue is different than the other. apple.stackexchange.com/q/21341/5472
    – bmike
    Commented Feb 20, 2013 at 4:09
  • Thanks, but that was the first one I followed without success. I chalked that failure up to it being an older version of OS X (lion not mountain lion). Note that he refers to ssh.plist and not sshd.plist Commented Feb 21, 2013 at 0:48
  • Rather than adjusting the machines, can you adjust your router to forward different ports externally to the proper machine at port 22? This may be a more sustainable solution if you have a router that supports this...
    – Scot
    Commented Sep 14, 2013 at 4:49

2 Answers 2

2

Welp, I found the answer after a few more days of research, with the help of a response to this MacWorld post I thought there had to be some way to include multiple ports in the one .plist file, but I couldn't figure it out. You can add an "Alternate Listener" key to ssh.plist for the extra port inside the Sockets scope, such as:

 <key>Sockets</key>
 <dict>
         <key>Listeners</key>
         <dict>
            (...)
         </dict>
         <key>Alternate Listeners</key>
         <dict>
            <key>SockServiceName</key>
            <string>ssh-alt</string>
         </dict>
 </dict>

Notice the ssh-alt string for SockServiceName. The original "Listeners" key remains as is, so you have Listeners and Alternate Listeners. You have two entries in /etc/services for ssh and ssh-alt like:

ssh              22/udp
ssh              22/tcp
ssh-alt        4790/udp
ssh-alt        4790/tcp

which preserves the port 22 internal access and creates access via port 4790 for external use (assuming you disable port 22 and port forward port 4790 on your firewall/router.

1
  • 1
    Unless you need to use the "ssh-alt" definition somewhere else, I find it easier to put the port number in the ssh.plist directly and don't bother editing the /etc/services file. (E.g. change <string>ssh-alt</string> to <string>4790</string>.) Also, "Alternate Listeners" can be any label you want (e.g. "External").
    – ithos67
    Commented Jan 22, 2014 at 4:27
0

Update: no longer works on Catalina... alternative is to copy the launch daemon, rename the label and launchctl load it.
Cannot change ssh port on High Sierra
The theory is still the same, so I'll leave up... he's also wrong about not being able to use the same process... you can if you load it with root. But it's not recommended as it conflicts with MacOS.

Tl;Dr:

  1. open nano /System/Library/LaunchDaemons/ssh.plist and add this key/dict inside sockets/dict.
    <key>Netgate SSH</key>
    <dict>
        <key>SockServiceName</key>
        <string>2269</string>
    </dict>
  1. restart the ssh daemon (danger!) launchctl unload /System/Library/LaunchDaemons/ssh.plist && launchctl load /System/Library/LaunchDaemons/ssh.plist

Your Sockets/dict key value should look like this

<key>Sockets</key>
<dict>
    <key>Listeners</key>
    <dict>
        <key>SockServiceName</key>
        <string>ssh</string>
        <key>Bonjour</key>
        <array>
            <string>ssh</string>
            <string>sftp-ssh</string>
        </array>
    </dict>
    <key>My Little Pony SSH</key>
   <dict>
       <key>SockServiceName</key>
       <string>2269</string>
   </dict>
</dict>

You must log in to answer this question.

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