Setting Up NAT Port Forwarding With VirtualBox
I’ve typically used bridged networking with VirtualBox, but you can run into some issues. If you’re somewhere without internet access, or if you’re changing IPs often, you might be better off using NAT settings. There’s a few other advantages - for instance you won’t need to check the IP of your machine in order to connect to it over SSH. It’ll just work. Sweetness.
To get started, your VM must be shut down. As far as I can tell, it cannot be suspended either. Shut it off.
First, get a list of your VMS.
`haddad:~ jhaddad$ VBoxManage list vms Oracle VM VirtualBox Command Line Management Interface Version 3.2.6 (C) 2005-2010 Oracle Corporation All rights reserved.
“WebServer” {2c61a180-e098-4926-b09a-27e431791c88} “Ubuntu” {01b85d8f-897a-412c-84b2-de285b34dd28}`
Next, set up a port over 1024 that you’ll use to access your VM. I chose port 2222 to be SSH.
haddad:~ jhaddad$ **VBoxManage modifyvm Ubuntu --natpf1 "ssh,tcp,,2222,,22"** Oracle VM VirtualBox Command Line Management Interface Version 3.2.6 (C) 2005-2010 Oracle Corporation All rights reserved.
Check your settings
haddad:~ jhaddad$ **VBoxManage showvminfo Ubuntu | grep NIC** NIC 1: MAC: 080027AFA78E, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0 NIC 1 Settings: MTU: 0, Socket( send: 64, receive: 64), TCP Window( send:64, receive: 64) NIC 1 Rule(0): name = ssh, protocol = tcp, host ip = , host port = 2222, guest ip = , guest port = 22
The important thing here is the NIC 1 Rule - it’ll list your host port (2222) and your guest port that it maps to (22).
To connect, use the host port you defined, in this case 2222.
haddad:~ jhaddad$ ssh localhost -p 2222
If you’ve set up a web server, say on 8888, make sure you put that in your URL. It’ll map back to port 80 and you’ll be good to go.
If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.