I am running CentOS 6.6 VM on a virtual box with the following network adapter settings.

  1. Bridged Adapter for hosting web server that could be accessed by Host machine

  2. NAT with port forwarding for ssh from Host machine to guest VM

From this VM, I want to ssh into a remote machine on a VPN, that host machine is connected to. The following diagram describes the setup.

MyCentosVM =======SSH======= TargetMachine
MyCentosVM == Host == VPN == TargetMachine

If I disable the bridged adapter, I can access the Target no problem because the NAT allows the VM to be transparent and be "part of" of the VPN. However, if I enable the bridged adapter, now the sshd of Target rejects the connection, even before the authentication process.

So my question is, is it possible to specify the VM to use NAT interface to ssh into Target?


Directly, no there isn't. You can, however, specify the use of a proxy machine or a tunnel using SSH's configuration file, most often located in ~/.ssh/config. Alternately, you will want to configure the routing table on the guest to go through the VPN when traffic is intended for the target machine. SSH will simply use whatever network path it is directed to by the machine you're SSHing from.


As I have run out of red pills, the following does not provide a solution for forcing a specific interface for SSH traffic to a specific host. This also looks like an XY problem, because you are not looking to force an interface, you are trying to make your local VM interact with a host that sits in a remote network. I still think I can help you a little.

TL;DR: Add a route to your CentOS VM that it should use the router reachable via the NAT-interface when interacting with Target. ip route add <Target>/32 via <router_ip_address>

On the whole your setup seems broken. If it really is the target machine that rejects your SSH connections, it is definetly broken.

  • Either the VPN is meant to only let your host into the network,
  • or it is meant to connect your networks.

In the first case, your SSH traffic should never reach the target machine. As a consequence it can't reject it.

In the second case, the VPN/setup fails in its task to connect your two networks properly.

From what you describe I gather that the VPN is only meant to connect your host to some network. If that is the case, then the entire problem is a routing problem and the only viable way for your VM to interact with machines on the VPN (other than your host) is through the NAT of your host. Therefore you always have to choose the NAT-interface, not just for SSH.

If you don't activate the bridged adapter, your VM uses a route via the NAT-interface to interact with Target. (Use ip route show to display configured routes.) As soon as you activate the bridged adapter it uses a route via the bridged adapter. This is also indicative that your setup is broken. For instance, there could be overlapping IP-subnets and/or multiple default routes on your VM.

Your current problem seems to be that as soon as you activate the bridged adapter your CentOS VM learns some default route which it preferes over using the NAT-interface.

To solve your problem you have to add a route to your CentOS VM, which tells the system to use the router reachable via the NAT-interface when interacting with Target, or even for every host on the VPN-network. If there isn't anything else wrong with your setup this route would always take precedence over the default route.

In the end it is a single:

ip route add \
<network_id_of_vpn_network>/<prefix_length_of_vpn_network> \
via <your_hosts_ip_on_the_NAT_network>`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.