Is it possible to “SSH” into my virtual machine remotely?

49,881

Solution 1

To install/configure SSH you should check out one of many guides you can find browsing the internet, this one for example.

You can connect to your viritual machiene via SSH using the following command:

ssh user@ip

You will thereafter be prompted to enter your password. To be able to connect you need, as said, an IP. When connecting locally you have a local IP. You can check this address for the current system using the ifconfig command. Example:

ifconfig
eth0      Link encap:Ethernet  HWaddr e8:39:35:42:ed:96  
          inet addr:191.13.238.54  Bcast:191.13.255.255  Mask:255.255.0.0
          inet6 addr: fe80::ea39:35ff:fe42:ed96/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:85385 errors:0 dropped:0 overruns:0 frame:0
          TX packets:77885 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:48235010 (48.2 MB)  TX bytes:9640323 (9.6 MB)

The inet addr being your local IP address, in my case 191.13.238.54. If I would connect to my SSH server on my computer I would therefore execute the following:

ssh [email protected]

To allow connecting from outside of your network you are required to forward the incoming traffic, on the port SSH is running on, to your local IP address via a admin panel for your router. Some routers may not support this and the admin panel work differently between manufacturers. You can always call customer support if you are stuck with this issue.

To check your "global" IP address you can visit this website: what's my IP?

When connected to your "server" through SSH you can perform various tasks such as SQL:

mysql -u root -p -h 127.0.0.0

In the above scenario root is the user, password is enabled through -p and the host is 127.0.0.0/localhost (your local computer).

You can also access files using nano or any other text editor like the very popular and powerful (and complicated) vim. All in all you now have the power of a terminal in your hands, that is if you connect via PuTTY.

Connecting via, say Ubuntu's file manager, gives you a better visual experience and allow you to edit files in a GUI editor rather than through the command line. Similar programs exist on Windows, you'll have to figure that one out yourself.

Solution 2

If you want to ssh to your VM (or even your computer) from anywhere your host(work) computer must have an IP which you may rent from your ISP if they offer.

But if you want to ssh to your VM from your own computer it's due to your VirtualBox setup. Navigate to your VM settings, network tab. Attach your VM to NAT and in advanced, you should setup port forwarding as the following:

Protocol -> TCP, HostIP -> 127.0.0.1, Host Port -> 1222, GuestIP -> (your VM ip shown via command ifconfig), GuestPort -> 22

Finally you can ssh to your machine via ssh [email protected] -p 1222

Share:
49,881

Related videos on Youtube

user51819
Author by

user51819

Updated on September 18, 2022

Comments

  • user51819
    user51819 over 1 year

    My home computer: Windows 7 laptop also running an Ubuntu virtual machine via VirtualBox with a bridged network connection (as opposed to NAT).

    My work computer: A Windows 7 computer.

    I want to SSH into my home machine remotely from my work computer because I want to be able to run / manipulate my LAMP webserver (which involves MySQL stuff, yes). I am running Apache on the VirtualBox and it would be great to have control from afar.

    To my understanding, SSH is a way to "log in" to your terminal remotely, yes? I've Googled this subject (which led me to things like PuTTY) but am having difficulty understanding how all of this works or what I need to set up or keep in mind as a beginner.

    However it would also be nice to have the ability to SSH in from anywhere, if possible! that'd be really convenient.

    • terdon
      terdon almost 10 years
      We might be able to help but you haven't explain what you actually need. ssh is a protocol that allows you to connect to (usually) remote computers. That said, please don't combine multiple questions in a single post, break them into separate ones instead.
    • saiarcot895
      saiarcot895 almost 10 years
      My guess is that the remote login feature in SQL is for access from outside of the system (as in from a remote web service or something). If you are using SSH, then it will be like you are a local user and you can use SQL, since it won't be able to tell that you're not physically there (well, technically, it might be able to, but it probably won't).
    • terdon
      terdon almost 10 years
      Unfortunately, you will need to be way more precise in order for us to help you. What was the exact command? (no "or something like that" please) What exactly are these remote logins you're talking about? Where did you see a message concerning them? If you mean mySQL remote logins, they have nothing to do with ssh. Once you've logged in via ssh, you are basically on the remote machine, it won't be a "remote" login as far as mySQL is concerned. Please edit your question and clarify.
    • user51819
      user51819 almost 10 years
      @saiarcot895 That is what I suspect as well but just wanted to be sure
    • user51819
      user51819 almost 10 years
      @terdon Edited the post
    • terdon
      terdon almost 10 years
      Thanks but we still don't know what you're actually trying to do. Do you want to be able to log into a virtual machine using ssh and run mySQL stuff? If so, you need to tell us. What machine will you be connecting from? The Windows host? Why bother with ssh then? Another host? Any computer from the internet? If you tell us what the final aim is, we'll be happy to help you achieve it.
    • user51819
      user51819 almost 10 years
      @terdon Edited the question again
    • Bikesh M
      Bikesh M over 7 years
      I am able to connect - Check this post wiki.workassis.com/virtualbox-ssh-between-host-and-guest
  • user51819
    user51819 almost 10 years
    So SSH is a "server" (sort of like Apache?) that I have to always have running on my VM? Also what exactly does "mysql -u root -p -h 127.0.0.0" do?
  • user51819
    user51819 almost 10 years
    And when you say "ssh [email protected]" do you literally type "user" or your username?
  • Xweque
    Xweque almost 10 years
    A SSH Server is the listening part if so to speak. It waits for you to make a request to sign on. It needs to be running for you to be able to connect. You connect to ssh with the username on your virtual machine. As for the mysql command... it connects to mysql. root is the username for mysql, 127.0.0.0 is the IP. Should be 127.0.0.0 if it's a local connection though, which this is...
  • user51819
    user51819 almost 10 years
    I still don't quite understand what -u and -p -h do but thank you for the answer!
  • Xweque
    Xweque almost 10 years
    -u is followed by the username. -p indicate that you want to enter your password and -h is followed by the host.
  • user51819
    user51819 almost 10 years
    What if you didn't specify -p? Or -h for that matter?
  • Xweque
    Xweque almost 10 years
    If you don't specify that you will use a password using -p, it will assume that you don't want to enter a password. If you don't specify the host you will connect to a local mysql server, user@localhost by default. All above was true on my system at least, it may be different on yours, but it's always good practice to specify everything when making connections to avoid any errors.
  • user51819
    user51819 almost 10 years
    What are the implications of not using a password? Is one still able to actually access anything?
  • Xweque
    Xweque almost 10 years
    If no password was entered but it was required you will be declined access, completely.
  • cdosborn
    cdosborn over 8 years
    First link is broken.
  • Xweque
    Xweque over 8 years
    Fixed the broken link.
  • Erik Aronesty
    Erik Aronesty almost 6 years
    Thanks! This is what I needed. 3 years after the question is asked....