3 min read

Setting Up Port Forwarding on a VirtualBox RedHat VM With A NAT Network Adapter

Setting Up Port Forwarding on a VirtualBox RedHat VM With A NAT Network Adapter

OK, quickly discovered that I was less brilliant than I thought yesterday. It appears that for the functionality I wanted (both the ability to SSH into the VM and to port forward from my local machine to an application running on the VM) I need to use a NAT and not a bridge network.

I spent a while struggling with this and discovered that to assign your VM to the standard 10.0.2.* range and successfully connect to the internet, a NAT drive should be your first network adapter. I'd initially had a bridge network as Adapter 1 and my NAT network as Adapter 2, but I couldn't connect to the internet with that setup.

I'm not entirely clear why adapter order matters and I'd like to understand better, but that's a problem for another day. For now, you should be on the right track if ip addr show in your vm gives you a device with inet 10.0.2.15 or similar.

Setting Up SSH

Once you're successfully connected to the internet:

dnf install openssh-server openssh-clients
systemctl enable sshd
systemctl start sshd
systemctl status sshd

If everything worked, you should get something like this:

If you're using firewalld:

firewall-cmd --zone=public --permanent --add-service=ssh
firewall-cmd --zone=public --add-port=22/tcp --permanent
firewall-cmd --reload

Now, add a port forwarding rule to your network adapter:

And you should be able to ssh into it: ssh -p 8022 user@127.0.0.1.

Port forwarding to an application

Add another port forwarding rule to your network adapter, forwarding from wherever you'd like on your host machine to the port running the app on your guest machine: in this case, I set both to port 8000 for simplicity's sake.

Be sure to enable the port in firewalld and reload:

firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --reload

Hypothetically this should have done the trick but I still wasn't able to get forwarding working and was only able to resolve it by running my server on 0.0.0.0:8000. In the case of Django I did this with python manage.py runserver 0:8000. Visit 127.0.0.1:8000 in your browser and it should successfully forward to the server running on your VM!

And, if you're like me, you'll realize you forgot to debug some build errors. Oops.

I'd like to figure out more about what virtualbox and RHEL are doing behind the scenes - it's still not clear to me why I could only get my app running on 0.0.0.0 or why I had to have my first adapter set to NAT - but this is a good stopping point for my second day of work on a virtualbox VM.