34

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?

5 Answers5

20

Step by step instructions for folks who might stumble into this.

Guest -> Linux on Hyper-V
Host -> Your OS (Windows 8/10/11)

  1. Make sure the Guest (Linux) can access the internet (using default switch in the hyper-v manager is the easiest).

  2. On guest, install net-tools and open ssh server.
    a. $ sudo apt-get install net-tools
    b. $ sudo apt-get install -y openssh-server

  3. Edit configuration file for openssh after it's installed
    nano /etc/ssh/ssh_config or vi /etc/ssh/ssh_config.

  4. Find and Uncomment
    #Password Authentication yes,
    so it looks like:
    Password Authentication yes. save the file

  5. Reload ssh after these changes
    a. sudo systemctl disable ssh
    b. sudo systemctl enable ssh
    if above commands are not working, also try these :
    sudo systemctl stop ssh sudo systemctl start ssh

  6. Ensure ssh is running # systemctl status ssh
    Now you can peacefully move ahead.

  7. On guest, find its IP from the terminal. Use
    a. ip address or,
    b. ip address | grep -i eth0
    c. Username is your Linux username, and password is your Linux login password.

On the Host Machine

  1. Use PowerShell to connect directly to the guest, or use programs like Putty. I personally prefer powershell. (Make sure the Hyper V is running)
    E.G: hostname: linuxusername, ip: 172.154.123.321 (see No. 7), password (linuxpassword) PowerShell
    ssh linuxusername@172.154.123.321 Type linuxpassword
    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.
deepyes02
  • 378
15

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.

MariusMatutiae
  • 48,517
  • 12
  • 86
  • 136
4

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.

1

Solution that I use to SSH into VMs with default switch (dynamic internal IP) with one command:

  • Enable static Mac address in the VM: In Hyper-V manager go to VM settings -> Network Adapter -> Advanced Features -> Mac address -> Static.
  • In PowerShell add the following to $profile file (e.g. open with notepad $profile):
function GuestVmSsh {
    ssh user@$((arp -a | findstr /i 00-00-00-00-00-00).Split(" ")[2].Trim())
}

Set-Alias guest-vm GuestVmSsh

change user to your guest system user and 00-00-00-00-00-00 to your static mac address specified in the first step.

  • Additionally, to make sure the above script is executed during shell startup, appropriate execution policy should be set. One way is to execute the following as Administrator:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

After that you should be able to SSH into your guest VM by executing guest-vm in PowerShell.

0

I am not sure if this is a universal answer or not, but after failing to see much success using other recommendations, I did the following and it worked:

My guest machine, which is a CentOS 7 box, is using a "default network" switch.

On the guest, I ran the "ifconfig" command to look at the ip for the box:

eth0: flags=4163 mtu 1500 inet 172.28.240.149

I then typed in that IP address to Putty, and lo and behold, it connected. Magic networking FTW.