Connecting to a remote host through a Ubuntu VM and sharing that back to your Windows Host

The VM in this walkthrough is Ubuntu 11.10 32bit (“VM”). It is running on my desktop, the physical machine (“PM”) running which runs Windows 7 32 bit as the Host Operting System (“Host OS”).

Things will be a lot easier for you if your username on your VM (“vmuser”), Host OS (“huser”) and remote host (“rmuser”) are the same.

Part 1: Generate RSA keys on your VM, and install them on the remote host

Follow steps 3 and 4 from the Ubuntu community help page, on your VM:

mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa
ssh-copy-id rmuser@remotemachine

Try sshing from the VM to the remote host. If it does not ask you for a password, then key-generation and installation worked. If not, then you must fix this before continuing.

Part 2: Preparing your VM and setting up an entry in /etc/fstab

Install:

  • *samba file server
  • *sshfs
  • *[optionally] Install webmin (will make checking user group, and creating/checking home share a lot easier).
  • *[optionally] Install the ssh meta-package so that you can ssh between your PM and VM.

Create a folder in home called remotehome.

Create a /etc/fuse.conf and add ‘user_allow_other’ to it:

sudo vim /etc/fuse.conf
user_allow_other
:wq

Check that your VM user is a member of the fuse group.

Add the following line to /etc/fstab:

sshfs#rmuser@remotemachine:/home/rmuser /home/vmuser/remotehome fuse _netdev,noauto,user,idmap=user,suid,workaround=rename,allow_other 0 0

In a terminal on the VM, type in mount ~/remotehome to test.

Part 3: Editing your VM’s crontab.

We could have used the ‘auto’ directive in fstab to mount the remote path, however I never got this to run properly within a VirtualBox VM.

An easy work-around is to edit your crontab like this:-

crontab -e

Then add the following line to the bottom of the file:

@reboot /bin/mount /home/vmuser/remotehome

Save your crontab and reboot your VM to check that this is all working. Check that you can open and create files, and make folders etc.

Part 4: (a) Creating a Samba user and (b) sharing your vmuser home folder

4(a) Creating a Samba User.

Open a terminal on the VM and type the following:

sudo smbpasswd -a huser

If your huser != vmuser, then you are in for a World of Pain (TM) and need to carry out these additional steps:

sudo vim /etc/samba/smbusers

Add the following line:

vmuser="huser"

If your huser is part of an Active Directory domain, you need to add that too, i.e.

vmuser="huser" "ADDOMAIN\huser"

4(b) Sharing your vmuser home folder.

You need to either use Webmin to create/turn on home shares (make sure it is writable to authenticated users) or do the following in a terminal on your VM:

sudo bash
cd /etc/samba
cp smb.conf smb.conf.bak
mv smb.conf smb.conf.master
vim smb.conf.master

If you had to edit the file smbusers, then add the following line to the [Global] section:

username map = /etc/samba/smbusers

Find, and uncomment the following lines:

;[homes]
;   comment = Home Directories
;   browseable = no
;   read only = yes

Change browseable to yes and read only to no.

Add the line writeable = yes for good measure.

Save your file, quit vim and then do the following:

testparm -s smb.conf.master > smb.conf
service smbd restart
exit

Part 5: Setting your VM to launch on start-up

In your Host OS, click on the Start button, click on All Programs, find and right click on Startup and choose “Open”.

Right click anywhere within the new Explorer window, and choose New –> Shortcut.

Enter the following for location of item:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm NAMEOFYOURVM

e.g.

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm Ubuntu32

Click Next, call the name something sensible, other then VBoxManage.exe, and click Finish.

Part 6: Give your VM a second NIC, a static IP and expose it to your Host OS

Shut down your VM, open Oracle VM VirtualBox Manager, click on your VM, and click “Settings”.

Click on the Network group, then on the Adapter 2 tab, and enable the Network Adapter. Change “Attached to:” to Host-only Adapter, note the adapter name and then click ok.

Still in VirtualBox Manager, click on File and then Preferences, click on the Network group, click on the adapter named in the previous step and click on edit. Give the adapter a fixed ip of 192.168.56.1 and network mask of 255.255.255.0. The IP address 192.168.56.1 is the IP on which your VM will communicate to your Host OS.

Fire up your VM. Open a terminal and open up your interfaces file with:

sudo sudo vim /etc/network/interfaces

Make sure your interfaces file looks exactly like this:

auto lo eth1
iface lo inet loopback

iface eth1 inet static
address 192.168.56.10
netmask 255.255.255.0

Save and close the file and restart the VM’s networking service with:

sudo /etc/init.d/networking restart

On your Host OS, edit the file C:\Windows\System32\drivers\etc\hosts and add the following line:

192.168.56.10       ubuntusvr

At this point you should be able to:

  • ping your VM from your Host OS with ping 192.168.56.10 or ping ubuntusvr from the command prompt
  • ssh into your VM from your Host OS to 192.168.56.10 or ubuntusvr
  • browse shares on your VM by opening Explorer and going to \\192.168.56.10\huser or \\ubuntusvr\huser
  • if apache, webmin, psql, mysql or any other sort of server is installed on your VM, access it through 192.168.56.10 or ubuntusvr

Part 7: Connecting network drive

In the Host OS, open Windows Explorer, click Tools –> Map Network Drive. Choose any drive and set the folder to \\192.168.56.10\huser OR \\ubuntusvr\huser, make sure ‘Reconnect at logon’ is checked and click on Finished.

References:
Ubuntu: SSHOpenSSHKeys
How-To Geek: Create a Samba User on Ubuntu

Leave a Reply

*

Contact Nixz Kerr