How to connect two Ubuntu hosts to the Internet using one network cable
Host A: ubuntu16.04
Host B: ubuntu16.04
Use the iwconfig command to view all network cards on the host. As shown above, the network cards on the author’s A host (laptop) are:
wlp2s0: This is a wireless network card.
enp1s0: Wired network card, the network card connected to host B.
The rest has nothing to do with us, no need to care.
Edit files
# vim /etc/network/interfaces
Configure a static IP address for interface enp1s0, as shown below
(The content below #===================== is newly added)
Restart interface enp1s0 (Note: Restarting may cause errors, so ignore it for now. This will only affect the result of step 5. If you need to handle it immediately, please go to step 6)
# ifdonw enp1s0 # ifup enp1s0 # ifconfig
Command to check whether the enp1s0 ip configuration is successful (the successful effect is as shown below)
Similarly modify/etc/network/interfaces
on host B this file. Modified to
(Note: There is an additional dns-nameservers item in the configuration of host B)
Restart B's interface, the same as A, no more details.
So far, executing the command ping 192.168.50.1 on host B can ping normally, indicating that the above work is correct.
This step is so that host B can access the external network through host A.
Execute in sequence on host A
# ip_forward : echo 1 > /proc/sys/net/ipv4/ip_forward # iptables -F # iptables -P INPUT ACCEPT # iptables -P FORWARD ACCEPT # iptables -t nat -A POSTROUTING -o wlp2s0 -j MASQUERADE (wlp2s0为A主机接外网的网卡)
At this point, host B can access the external network, and ping www.baidu.com on host B can ping normally.
Errors may occur during the process of restarting the interface
Error: /etc/resolv.conf isn't a symlink
Workaround for this error:
1), copy the/etc/resolv.conffile to the directory:/run/resolvconf/and download
2), delete/etc/resolv.conf
3), establish a soft connection:ln -s ../run/resolvconf/resolv.conf /etc/resolv.conf
The above is the detailed content of How to connect two Ubuntu hosts to the Internet using one network cable. For more information, please follow other related articles on the PHP Chinese website!