How to connect to a local hyper-v virtual machine via SSH?

What I have:
Windows 8.1 professional edition

What I want?
To simulate a circumstance: connect a remote Linux system using SSH.

What I do:
Install CentOS 6.4 x64 under the built-in Hyper-v. It works like a charm.

What is the question?
How to connect to this hyper-v CentOS via a terminal by SSH?


Solution 1:

The simplest solution is to enable a bridged connection, you can find here how to do that. Once you have done this, and started your VM, it will appear on your LAN with an IP just like your host. Then you can ssh into it by means of

 ssh me@IP_of_my_VM 

or whatever you use on Windows, Putty I presume.

Solution 2:

I think the people missed one point so I'm going to elaborate really short. Guest - Virtual Machine, Linux, Host - WIndows 10

  1. Make sure that the guest os (linux) can access the internet (as long as you use default switch in the hyper-v manager that's fine).

  2. in the guest, install net-tools and open ssh server. You need sudo privilidges.
    a. either sudo su, if not possible su root, provide the password. You will get a # instead of a $.
    b. # apt-get install net-tools,
    or, $ sudo apt-get install net-tools if you are not root. Use sudo infront of each command if you are not root from herein.

c. apt-get install -y openssh-server - openssh server will now install.

  1. Edit the configuration file for openssh once it's installed, it's in path /etc/ssh/ssh_config
    nano /etc/ssh/ssh_config

  2. Command text editor opens, key down to the line #Password Authentication yes, make sure that the # is removed (i.e the statement is uncommented, so it looks like):
    Password Authentication yes . All is well? Continue on.

  3. disable and enable ssh - because we just configured it.
    a. # systemctl disable ssh
    b. # systemctl enable ssh you can also try if somehow above commands are not working: -> # systemctl stop ssh
    -> # systemctl start ssh "In the end, make sure that you check that the ssh is running" --> # systemctl status ssh
    Peace of mind, When you are sure ssh is running

  4. Find the ip of your guest os (linux). The ip looks like 172.154.123.321 and comes with a subnet mask (255.255.255.0) and default gateway (172.xyz.xyz.xyz. You need the ip, the first one that you find. You will need this ip on your host machine. Also remember your hostname. It is on your terminal, eg: misskiller@debian, here hostname is *misskiller

On the terminal type:
a. ip address (you will most likely find the ip of your computer in the eth0 adapter, for ease to read in the mess, you can also type command as below -> (alternative way, longer command but easy fish)
b. ip address | grep -i eth0 (this is optional just for your ease, you can just go with 6. a)

  1. On your Host Machine, you can either use Putty or directly your PowerShell to connect.

SSH INTO THE GUEST*. For this example: hostname: misskiller, ip: 172.154.123.321
A. From PowerShell Type this:
ssh [email protected] (basically hostname@ip)
You will be asked for the password that you setup at your machine. Type your password properly. You should be logged in now. B. From Putty: same, just input the ip address in the hostname and press enter, type password into the terminal and you should be in there too. This works - Tried and tested. :-)

Solution 3:

I'm going to chime in because the given answer only covers the use case where you want your VM to also be exposed to the internet, and is also more work than necessary. If you want to get this working on a virtual internal network:

  1. Set up the virtual switch as Internal network and apply it to the VM
  2. In the Hyper-V manager, under the networking tab, you should see the IPv6 addresses associated with the VM.
  3. Adjust the width of the columns if necessary or mouse over to reveal the far right IPv6 address (probably starts with fe80). Connect to this IP address using your software of choice.

In the case of the External network, you should see the IPv4 address it is using on the network tab and should be able to connect to that.