chrisspeck.com

Chris' random ramblings

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

posted by specky in IT and have No Comments

Remotely starting X11 apps on other displays

user@laptop:~$ ssh -X user@desktop
...
user@desktop:~$ printenv
...
DISPLAY=localhost:10.0
...
_=/usr/bin/printenv
user@desktop:~$

Take note of the “DISPLAY” variable, this is the local port (on the machine called “desktop”) which is forwarding X11 server commands to the X11 client running on the machine called “laptop”.

user@desktop:~$ export DISPLAY=:0.0
user@desktop:~$ vlc &

This sets the “DISPLAY” variable to the first screen on the desktop, and starts VLC as a background process. For this to work you must be logged into the server with the same user account, and have an X11 client running on the desktop.

user@server:~$ export DISPLAY=localhost:10.0
user@server:~$ gedit &

This sets the “DISPLAY” variable to the laptop, and starts Gedit on the server, but displaying it on the laptop.

posted by specky in IT and have No Comments

It’s a feature, not a bug, right?

Right…so if anybody else in the world is wondering, it appears as though the 32bit version of Ubuntu Desktop 10.04 will disable networking following a hibernate.

It does not matter whether the system actually manages to restore from the hibernate (mine actually crashed), nor how many times you power it down and restart it. $ sudo service networking start will not fix it either.

To fix it you have to use the mouse, right click on the network connection icon in the top left hand corner, and choose “Enable Networking”. WTF?

posted by specky in Uncategorized and have No Comments

Using SSH in Bash to tunnel IP traffic

While it is trivial to set up locally forward ports using a GUI SSH client like Putty, I have a habit of forgetting how to do the same at the Bash command line, and I find the ssh man page a tad confusing. Cutting straight to the chase:-

$ ssh -X myserver.dyndns.org -p 1045 -L 10001:192.168.0.100:10000

This will connect to server called “myserver.dyndns.org” on arbitrary external port 1045, and map your localhost port 10001 to port 10000 on the machine with IP address 192.168.0.100 on the remote site (allowing you, for instance to access Webmin on the remote machine by going to https://localhost:10001 ).

The tack “-X” tells SSH to forward X11 requests, allowing you to run GUI applications on the remote machine and have the display on your local machine without further configuration in a Linux environment, or after installing a program like Cygwin on a Windows machine.

posted by specky in Uncategorized and have No Comments