SSH access via port 22 from outside Intranet fails

I have an identical problem as described in BigSur SSH can connect a server in the in… - Apple Community (that discussion is closed). Problem is still present in Ventura 13.2.1. In summary:


  • I can connect to the SSH server from within my home Intranet without any problems
  • I can connect to the same server from outside my Intranet if the server is listening on a port different from 22 (say, 4444). If I set up that router forwards port 22 to 4444 on the internal server, I can connect (ie. the router is not blocking 22). If I use any other port, it also works.
  • if the server listens on port 22, then I cannot connect, whatever I do. I tried default, with enabling remote login (sshd is then started via launchd), I also tried disabling the remote log in and manually starting the server from the command line (to check if launchd was causing trouble) by executing sudo /usr/sbin/sshd -d -p 22 - still the same.


So, it doesn't appear that the problem is in the sshd server setup, nor forwarding setup on the router, nor anything in my client setup. I tried removing known_hosts file as well, no difference. I checked firewall settings etc, there's nothing there. Yet, something *IS* blocking port 22 *IF* the connection is coming from outside the Intranet.


I just don't know where else to look... any tips are greatly appreciated.

Posted on Feb 14, 2023 10:43 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 15, 2023 12:35 PM

So if I understand correctly, you want to have your router Port Forward port 22 "AS" port 22 to the Internet and be able to connect from a remote system via ssh?


You can port forward port 4444 (or other high numbered port) and that does work for making a connection from a remote system.


If the above 2 are correct, and you have port forwarded port 22 to the internet via your router, then it is possible your ISP is blocking port 22 connections.


I do know that most ISPs block ports 80 (HTTP), 443 (HTTPS), 25 (mail), and maybe others. So it is possible the ISP is blocking port 22 as well. This is a guess, but it is an educated guess based on other ports ISPs commonly block.


In the case of 80 and 443, the ISP wants the customer to pay for a business connection before they will allow the customer to setup a public web server.


NOTE: I'm with EtreSoft in that opening port 22 through your router is a very VERY BAD idea. There are script kiddies that probe systems for an open port 22, and beat on the system attempting to login. Even if they cannot get it, it is a waste of your ISPs bandwidth, your routers CPU and your Mac's CPU resources.


If you are going to open an ssh port, then it is much better to choose a high numbered port below 65534, DO NOT enable the root account, and make sure any accounts on your system have very strong passwords.

https://www.grc.com/haystack.htm

9 replies
Question marked as Top-ranking reply

Feb 15, 2023 12:35 PM in response to milan_sydney

So if I understand correctly, you want to have your router Port Forward port 22 "AS" port 22 to the Internet and be able to connect from a remote system via ssh?


You can port forward port 4444 (or other high numbered port) and that does work for making a connection from a remote system.


If the above 2 are correct, and you have port forwarded port 22 to the internet via your router, then it is possible your ISP is blocking port 22 connections.


I do know that most ISPs block ports 80 (HTTP), 443 (HTTPS), 25 (mail), and maybe others. So it is possible the ISP is blocking port 22 as well. This is a guess, but it is an educated guess based on other ports ISPs commonly block.


In the case of 80 and 443, the ISP wants the customer to pay for a business connection before they will allow the customer to setup a public web server.


NOTE: I'm with EtreSoft in that opening port 22 through your router is a very VERY BAD idea. There are script kiddies that probe systems for an open port 22, and beat on the system attempting to login. Even if they cannot get it, it is a waste of your ISPs bandwidth, your routers CPU and your Mac's CPU resources.


If you are going to open an ssh port, then it is much better to choose a high numbered port below 65534, DO NOT enable the root account, and make sure any accounts on your system have very strong passwords.

https://www.grc.com/haystack.htm

Feb 15, 2023 10:24 AM in response to milan_sydney

LittleSnitch and Anti-Virus packages can accept local ssh connections, but reject ssh connections that originate from a different subnet. That is to say, they can reject ssh connection requests from the Internet.


Ventura depreciated RSA keys. If you are using ssh-keygen keys that might be your issue.


Doing 2 ssh connections

ssh -v -v -v using.local.system
ssh -v -v -v user.remote.system


Compare the output, and see where they make a major diversion.


Are any of the remote systems also Ventura? That might be a way to see if it is the older ssh on other systems that is causing issues.

Feb 15, 2023 4:19 PM in response to milan_sydney

I have been remotely ssh connecting to my Macs, my Mom’s Mac and my Synology NAS for years.


I always have the router port forward a high numbered unique port per system to port 22 on each system. I never open port 22, always a high numbered port.


To make connecting easier, I get a dynamic DNS name from No-IP.com, or Synology will provide one because I own a Synology NAS.


I ssh -p nnnnn my.dynamic.dns.name


The nnnnn is the unique port number I have opened for the system I wish to make an ssh connection.


I also create ssh tunnels for VNC and file sharing, so I can do that securely across the internet.


I used to backup my Mom’s iMac from 300 miles away over an ssh tunnel, and do remote admin services.


I do NOT enable the macOS firewall, as my router blocks unsolicited connection requests.


The Mac does not block ssh connection requests, as long as Remote Login Sharing is enabled, the firewall is off, there is no anti-virus software running, nor LittleSnitch. And connections from the outside work if the router port forwarding has been setup correctly, and the ISP is not blocking the port.


And by a lot of years, I started remote ssh connections in the mid-2000’s.


Note: I have an eero mesh router, and I can setup, enable and disable my port forwarding from anywhere. I typically keep my ports disabled, unless I need to make a connection. Keeps the script kiddies from annoying my systems all day and all night.

Feb 15, 2023 10:49 AM in response to milan_sydney

milan_sydney wrote:

I have an identical problem as described in BigSur SSH can connect a server in the in… - Apple Community• (that discussion is closed).

No such thing exists. But if you insist, then I'll give you a reply identical to my reply in that thread. 😄

I can connect to the SSH server from within my home Intranet without any problems
I can connect to the same• server from outside my Intranet if the server is listening on a port different from 22 (say, 4444). If I set up that router forwards port 22 to 4444 on the internal server, I can connect (ie. the router is not blocking 22). If I use any other port, it also works.
• if the server listens on port 22, then I cannot connect, whatever I do. I tried default, with enabling remote login (sshd is then started via launchd), I also tried disabling the remote log in and manually starting the server from the command line (to check if launchd was causing trouble) by executing sudo /usr/sbin/sshd -d -p 22 - still the same.

So, it doesn't appear that the problem is in the sshd server setup, nor forwarding setup on the router, nor anything in my client setup. I tried removing known_hosts file as well, no difference. I checked firewall settings etc, there's nothing there. Yet, something *IS* blocking port 22 *IF* the connection is coming from outside the Intranet.

Please described "the SSH server". Is this a Mac or a PC? Please describe "my Intranet". Is this an internal server or it is somewhere on the internet? Please describe "firewall settings". Most definitely please describe "etc".


To make a long reply short, your network is misconfigured for what you are trying to do, whatever that is. Consumer-grade equipment on consumer-grade services are not designed to run servers. We don't even know if your network has been even minimally configured for any of this to work.


From the point of view of the internet, your "server" doesn't exist. The only thing that exists is your ISP router. Hopefully, it does not listen on port 22. If it does, then it is your responsibility to forward those connections, through one of a number of means, depending on your router. It may not even be possible to route port 22, given the consumer nature of such things. If it was my network, I sure wouldn't want customers running any services on privileged ports. Or I'd make them pay $199/month for the privilege. And I'd probably still lose money on support costs.

Feb 15, 2023 12:07 PM in response to etresoft

SSH server - Mac Mini running Ventura 13.2.1, enabled when you enable "Remote login" in "Sharing". This is available on every Mac.

Intranet - home network, with a modem/router that has a connection to Internet.


I don't think you read the description. I made it work, I just had to use a port other than 22. And everything works just fine EVEN using port 22, as long as both computers are on the same local network. This is exactly the problem that was described in the post I referred to and that didn't provide a resolution.


I know very well how to configure router, networks etc, I'm far from a beginner. As I said, I made it work in various configurations. But the one I wanted, to just use the standard port and default setting for sshd on the target computer, couldn't work.

Feb 15, 2023 1:01 PM in response to BobHarris

I had the same thought, so I did that experiment as well: open the port 4444 on the router and forward it to the port 22 on the target computer - still the same. It doesn't even get to the sshd. When I run it in debug mode (-d option) from the command line, it sits still with no indication that any connection was made. When I do the same with the SSH client inside the local network, I get a lot of debug messages and a successful connection.


I'm not too worried about opening the port 22. I have changed /etc/ssh/sshd_config to disable password based log ins. I'm using only key-based authentication and it works fine.

Feb 15, 2023 2:39 PM in response to BobHarris

PS. I tried one more thing:

  1. open port 22 on the router
  2. forward it to the VirtualBox port 22
  3. have SSH server running inside the VM


So, what I get is the same behaviour:

  • if I connect from the local network to the SSH on the Mac running the VM guest, all good
  • if I try the same from outside the local network, I get the same problem.


My guess is that there is some kind of network filtering in place on the Mac that runs SSH server (or hosts VM that runs SSH server) that creates this problem. This is indicated by the fact that I have two different applications (ssh server and VirtualBox gues) both being prevented to receive connections on port 22.


I don't know anything about how OSX does packet filtering, but I was hoping someone on this forum would know that (I just know of pfctl utility, but haven't used it).

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

SSH access via port 22 from outside Intranet fails

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.