This article mainly introduces the method of converting computers installed with CentOS/RHEL 6/7 into routers. We will learn how to use NAT technology to convert computers installed with RHEL/CentOS 6/7 into routers. Interested can learn more about it.In this article, we will learn how to use NAT technology to transform a computer with RHEL/CentOS 6/7 into a router. As we all know, a router is a network device working in the third layer, which is used to connect two or more networks together, that is, to connect the LAN to the WAN or to connect the LAN directly. Routers are very expensive, especially for small organizations, which may be one reason why we pay attention to routers. Instead of using dedicated hardware, let's convert Linux computers into routers.We will talk about the operation process on RHEL/CentOS 6 and 7. But before we start, let's look at what needs to be prepared.
- A computer with RHEL/CentOS 6 or 7
- Two network cards with local IP and extranet IP respectively
- We need to assign IP addresses to both network cards, one local network IP (provided by our network administrator) and the other Internet IP (provided by ISP). Like this:Ifcfg-en0s3192.168.1.1 (LAN IP address)Ifcfg-en0s510.1.1.1 (WAN IP address)
- Note: Different Linux distributions have different network card names. Now that the preparation is complete, we can configure it.
Enable IP forwarding
The first step is to enable IP forwarding. This step is the same in RHEL/CentOS 6 and 7. Run: $sysctl -w net.ipv4.ip_forward=1
But this will be restored after the system restarts. To make the restart still effective, you need to open it: $vi /etc/sysctl.conf
Then enter the following: net.ipv4.ip_forward= 1
Save and exit. Now the system has IP forwarding enabled.
Now configure IPtables/Firewall rules
Next we need to start the IPtables / firewalld service and configure the NAT rules
$systemctl start firewalld(For Centos/RHEL7)$ service iptables start(For Centos/RHEL6)
Then run the following command to configure the NAT rules of the firewall
CentOS/RHEL 6$ iptables -t nat -A POSTROUTING -o XXXX -j MASQUERADE$ service iptables restartCentOS/RHEL 7$ firewall-cmd -permanent -direct -passthrough ipv4 -t nat -I POSTROUTING -o XXXX -j MASQUERADE -s 192.168.1.0/24$ systemctl restart firewalld
Here, XXXX is the name of the network card with an external IP. This will configure the Linux machine as a router. Now we can configure the client and test the router.
To test a router, we need to set the client's gateway to an intranet IP, in this case 192.168.1.1. So whether the client is Windows or Linux, make sure the gateway is 192.168.1.1 first. After completion, open the terminal or command line and Ping a website to test whether the client can access the Internet:
We can also check it by visiting websites through web browsers.Above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope you will support my website more.