Tuesday, September 18, 2012

SSH into a VM from host machine

I'm using Virtualbox 4.1.2 on Ubuntu 12.04 LTS
After installed an Ubuntu server 12.04 into a VM machine, I want to ssh to it.

1. Config network
The fastest way to do this is enable a new adapter, attach it to Bridged Adapter. You have to turn off your VM. Then choose setting

Add caption


Start your VM. Now you need to config ip address for new interface:



hvn@server:~$ ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:e8:da:87 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet6 fe80::a00:27ff:fee8:da87/64 scope link
       valid_lft forever preferred_lft forever
3: eth1...

Now open /etc/network/interfaces to config it:

hvn@server:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
#Add bellow lines
auto eth1
iface eth1 inet dhcp

Restart networking:
hvn@server:~$ sudo /etc/init.d/networking restart
When this done, you will see ip of your new interface eth1:

hvn@server:~$ ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:e8:da:87 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
    inet6 fe80::a00:27ff:fee8:da87/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:f7:65:dd brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.107/24 brd 192.168.0.255 scope global eth1
    inet6 fe80::a00:27ff:fef7:6 5dd/64 scope link
       valid_lft forever preferred_lft forever

You can set static ip for eth1.I will add it later here...

2. Set up and config SSH
First, you have to install SSH server on VirtualMachine.
sudo apt-get install openssh-server 
and install SSH client on your machine:
sudo apt-get install openssh-client
Create a key
ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/b/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/b/.ssh/id_rsa.
Your public key has been saved in /home/b/.ssh/id_rsa.pub.
 
You should not enter file to save the key, just let it empty, the program will set default to 
~/.ssh/id_rsa
 
Then copy it to  Virtual Machine:
ssh-copy-id <username>@<host> 

Now have to add the key that you've created:
ssh-add ~/.ssh/id_rsa
<enter your passphrase>

Done, connect to your Virtual Machine
ssh <username>@<host>

See more here:
https://help.ubuntu.com/community/SSH/OpenSSH/Keys

No comments: