Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to help a colleague to connect to my MySQL server. However the situation is a bit special, and here's why (let's call him person A and me, person B):

Person A has a PC, on which he has a virtual machine, which is in the same network as the actual PC he's running. However person A is also in the same network with person B (a different network).

I want the site that lives on A's VM to be able to connect to the MySQL server on B's PC. For this I've thought a port forwarding would be appropriate: from ip-of-person-A:3306 to ip-of-person-B:3306. This way the site would connect to the IP of the PC it's living on (not the VM), which would forward to A's MySQL.

I've seen several examples of port forwarding, but I don't think it's what I need, from what I've seen, it's kind of the opposite. So would something like this be achievable?

share|improve this question
Are you sure you can't just add an IP for B's network to that VM? BTW Alice and Bob are good names to use. – Erik Johansson Dec 4 '12 at 11:11
I'm really bad at networks & stuff, so I don't know how to add the IP for B to the VM's network, but yes, that would be even better. Alice & Bob, noted, thanks! – Eduard Luca Dec 4 '12 at 11:47
up vote 1 down vote accepted

Person A can use port-forwarding with iptables on his machine, it will allow connections made to his machine to be forwarded to PersonBs machine

echo "1" > /proc/sys/net/ipv4/ip_forward    

mysql=3306 # port

### Make all packets from VM to mysql go to Person B
iptables -t nat -I PREROUTING -p tcp --dport $mysql \ 
   -s $personA_VM/32  -d $personA  -j DNAT --to $personB:$mysql

### Make all packets local to PersonBs network.
iptables -t nat -I POSTROUTING -p tcp --dport $mysql \
   -s $personA_VM/32 -d $personB/32  -j MASQUERADE

But as I say in the comments a better solution is to add the VM to both networks

share|improve this answer
I will definitely try this. I'll also look up the suggestion from your comment. I'll get back and accept the answer if it works. – Eduard Luca Dec 4 '12 at 11:47
note I'm not sure how your setup looks, but this will work for some setups. It all depends where the IP packets are inserted from the VM. – Erik Johansson Dec 4 '12 at 11:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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