Home > System Tutorial > LINUX > body text

How to connect two Ubuntu hosts to the Internet using one network cable

WBOY
Release: 2024-08-07 13:39:22
Original
336 people have browsed it

How to connect two Ubuntu hosts to the Internet using one network cable

1. Preparation

Host A: ubuntu16.04

Host B: ubuntu16.04

2. Host A has two network cards, one is connected to the external network and the other is connected to host B.

How to connect two Ubuntu hosts to the Internet using one network cable
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.

3. Configure A’s static IP

Edit files

# vim /etc/network/interfaces
Copy after login

Configure a static IP address for interface enp1s0, as shown below
How to connect two Ubuntu hosts to the Internet using one network cable
(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
Copy after login

Command to check whether the enp1s0 ip configuration is successful (the successful effect is as shown below)
How to connect two Ubuntu hosts to the Internet using one network cable

4. Configure B static IP

Similarly modify /etc/network/interfaces
on host B this file. Modified to
How to connect two Ubuntu hosts to the Internet using one network cable
(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.

5. Configure NAT

This step is so that host B can access the external network through host A.
Execute in sequence on host A

#  ip_forward : <span class="a14c"> echo 1 > /proc/sys/net/ipv4/ip_forward  </span>
<span class="a14c">#  <span class="postbody">iptables -F</span></span>
<span class="a14c"><span class="postbody">#  iptables -P INPUT ACCEPT</span></span>
<span class="a14c"><span class="postbody">#  iptables -P FORWARD ACCEPT</span></span>
<span class="a14c"><span class="postbody"># iptables -t nat -A POSTROUTING -o wlp2s0 -j MASQUERADE     (wlp2s0为A主机接外网的网卡)</span></span>
Copy after login

At this point, host B can access the external network, and ping www.baidu.com on host B can ping normally.

6. Error resolution

Errors may occur during the process of restarting the interface

Error: /etc/resolv.conf isn't a symlink
Copy after login

Workaround for this error:
1), copy the /etc/resolv.conf file 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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template