29

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?

Albert Gao
  • 435
  • 2
  • 5
  • 7

5 Answers5

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
  • 46,990
  • 12
  • 80
  • 129
  • 2
    Finally, I open a SSH in CentOS at Hyper-V, figure out its IP and then using putty in windows to connect to this hyper-v instance via this IP,works like a charm. – Albert Gao Nov 29 '13 at 09:42
  • 1
    Is my case, the IP isn't the same as LAN adapter IP, I have to get into CentOS VM to get IP via command `ip address` – John Jang Nov 19 '18 at 08:58
  • 1
    Also, don't forget to actually install the SSH server `sudo apt install openssh-server`. – Monsignor Jul 15 '20 at 12:32
12

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

Guest - Virtual Machine on Linux, Host - Windows 8/10/11

  1. Make sure that the Guest (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.
    a. $ sudo apt-get install net-tools if you are not root. Use sudo for commands if they don't work without.
    b. apt-get install -y openssh-server.

  3. Edit the configuration file for openssh once it's installed, it's in path /etc/ssh/ssh_config
    nano /etc/ssh/ssh_config or vi /etc/ssh/ssh_config whatever is available.

  4. Find the line #Password Authentication yes, make sure that the # is removed (i.e the statement is uncommented, so it looks like):
    Password Authentication yes.

  5. let's reload ssh - because we just configured it.
    a. sudo systemctl disable ssh
    b. sudo systemctl enable ssh
    alt commands if somehow above commands are not working:
    -> sudo systemctl stop ssh
    -> sudo systemctl start ssh
    "In the end, make sure that you check that the ssh is running"
    --> # systemctl status ssh
    Peace of mind

  6. 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 will need to use this info on your host machine (windows) to connect to the guest OS. a. ip address
    b. ip address | grep -i eth0 (alternate command to grab the ip address)
    c. To find the username: type hostname in linux and it will give you your username. Username is your linux username simply put.

Switch to HOST Machine ###

  1. From your Host Machine (Windows), you can use PowerShell to connect directly.

SSH INTO THE GUEST
E.G: hostname: misskiller, ip: 172.154.123.321
a. From PowerShell Type this:
ssh misskiller@172.154.123.321 (basically hostname@ip)
Type password (System Login Password for linux when you set it up)
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.

deepyes02
  • 166
  • 2
  • 8
  • 1
    Welcome to SU. Your answer shows one way how to do it, so I don't understand why it got downvoted. However, please edit your answer and put all code in code boxes (instead of writing it as bold text). – Binarus Nov 05 '20 at 21:38
  • 2
    @Binarus - it leaves out lots of things, such as saving ssh_config after editing it, and it calls the user name the 'hostname'. Likely to confuse a new Linux user. – Michael Harvey Dec 30 '22 at 20:09
  • May God bless you for this direct answer. – Emmanuel Aliji Jul 21 '23 at 12:40
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.

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.

Byron Katz
  • 331
  • 2
  • 5