Categories: Uncategorized

[Mac][VirtualBox][Ubuntu]How to connect to Ubuntu in Virtual Box using SSH

I followed the instructions.
  • Setting host-only adapter
  • Install openssh-server on Ubuntu
  • Setting static IP address on Ubuntu
  • Create start up script

Setting host-only adapter

Add hos-only adapter at Preferences… of VirtualBox menu.

Check host-only adapter’s IPaddress from right side icon.
Setting virtual machine’s network environment.

Install openssh-server on Ubuntu

Install oepnssh-server using apt-get.
$ sudo apt-get update
$ sudo apt-get install openssh-server

Set static IP address on Ubuntu

Fill in the address, gateway, netmask based on checked host-only IP address.
$ sudo vim /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto eth1
  iface eth1 inet static
  address xxx.xxx.xxx.xxx
  gateway xxx.xxx.xxx.xxx
  netmask xxx.xxx.xxx.xxx

Reboot after setting gives us to connect Ubuntu using ssh like this.
“ssh username@guest IP address”.

Make start-up script

Make start-up script and ssh connect script.
$ mkdir -p ~/myscript
$ vim ~/local-login

#! /bin/sh
isstarted=`ps aux | grep -c "GuestName"`
if [ $isstarted -eq 1 ]; then
  VBoxManage startvm GuestName -type vrdp
  sleep 5
else
  VBoxManage controlvm GuestName resume
fi
ssh username@guest IP address
remain=`ps -e -o args | grep -c "^ssh.*GuestName"`
if [ $remain -eq 0 ]; then
  VBoxManage controlvm GuestName pause
  echo vm paused.
fi
echo bye

$chmod 755 ~/myscript/local-login

GuestName part is virtual machine’s name、
username part is guest OS’s username、guest IP address part is guest OS’s IP address is set. And then, write the script path to .bash_profile.
PATH=$PATH:/Users/username/myscript
export $PATH
How useful!
zuqqhi2