How to ssh into a server running on a home network, behind a router?

67,024

Solution 1

There are multiple things you need to do:

1) Like fkraiem mentioned in his comment, make sure your VM is accessible on the local network. Using a 'bridge' network connection in the VM settings should accomplish that. Then using one of your other computers (even your host), check to see if you can log into the SSH server. If you can, congrats, let's move to the next step.

2) Next you need to know how to port forward on your router. Since you didn't mention what router you have, you will have to figure that out yourself. Port 22 is the one used by SSH. Of course you can listen on a higher port on the router to avoid having your logs filled up by a bunch of script kiddies trying to hack into your server, and then forward the port to 22 on your server. For example, lets imagine your local IP for your server is 192.168.0.10. Your WAN (Internet) IP is 99.99.99.99.

99.99.99.99:60022 -> Router -> 192.168.0.10:22

What you want to do is tell your router to listen on port 60022, and forward that to IP 192.168.0.10, and port 22.

To figure out what your WAN IP is, simply google 'what is my IP address'. There are other ways, but it is the easiest.

Now, after you setup the router, try to connect with an outside computer to your WAN IP with the higher port. You could tether with your cellphone to a laptop to accomplish that.

If you could connect with that, congrats again.

3) Now you have to make sure your IP addresses stay static on your local network, and you would need to get a dynamic DNS entry for your WAN IP address. This way you would use a domain name instead of your WAN IP. Google 'dynamic dns' to find some free service.

Hope that helped.

Solution 2

My personal setup is the following:

  1. On the router forward port 22 from remote machine to 2222 of the local machine, let's say 192.168.0.33 for example. That way you can still have ssh access to the main OS on the machine, while 2222 is for the virtual OS.

    enter image description here

    1. On the virtual box, forward host OS 192.168.0.33 port 2222 to the guest OS's port 22. The IP of the guest can be found with the ifconfig command on the guest itself.

    enter image description here

Now if you know your router's IP, you can ssh to your router's IP, and it will be automatically forwarded to port 2222 of your physical machine. Physical machine will give that connection to port 22 of virtual machine (provided that VM is running).

If you want to ssh from local network, use ssh -p 222 192.168.0.33

Share:
67,024

Related videos on Youtube

Hugo Lobo
Author by

Hugo Lobo

Updated on September 18, 2022

Comments

  • Hugo Lobo
    Hugo Lobo over 1 year

    Okay, so I have a PC in my home network, running Ubuntu on a VM with openssh server installed.

    1. How can I remotely access it from another computer outside the home network, using the internet?
    2. What IP address do I use to connect to it?
    3. What kind of port-forwarding do I need to do?
    • fkraiem
      fkraiem over 8 years
      What kind of networking does the VM use? For typical home usage, bridged is recommended, then for network purposes the VM will be equivalent to a physical machine, so you just do the port forwarding on your router as usual.
    • Hugo Lobo
      Hugo Lobo over 8 years
      Under Network Settings on my VM, it says the network adapter is attached to NAT. Here is a screenshot: link Do I need to change this?
    • fkraiem
      fkraiem over 8 years
      Putting it in bridged will make things simpler, yes. (Basically, with NAT you need to do port forwarding twice, on your router and in VBox.)
    • Roomy
      Roomy over 8 years
      in fact you don't need any port forwarding when the machine that you want to connect from is visible to your local server - just us the -L flag at ssh command - but be sure the connection is keeping alive - run htop or something similar to keep the data transfer.
  • m4l490n
    m4l490n over 6 years
    Is this "99.99.99.99:60022 -> Router -> 192.168.0.10:22" the actual command? what does "Router" mean? I mean, what goes in here?
  • G Trawo
    G Trawo over 6 years
    @m4l490n No, that's not a command. It's simply a way to show the "flow" of the connection. 99.99.99.99 is just an IP address I picked. I could also have shown XXX.XXX.XXX.XXX, but I find numbers easier to understand. 60022 is the "outside" port where the router listens on. It then does the port forwarding to the internal address of 192.168.0.10 and moves it down to port 22. The reason I didn't go into more detail is because OP didn't state what router (and firmware) he was using.