Crmgo Blog

Sharing Internet on CentOS

Sharing Internet on CentOS

Internet Sharing on CentOS Suppose you have a machine connected to the internet and need to share it with other computers. Enable packet forwarding. To do this, edit /etc/sysctl.conf: insert the line Execute: to apply this rule without rebooting. Set up iptables,…

Sharing Internet on CentOS

Internet Sharing on CentOS

Assume you have a machine connected to the internet and need to share it with other computers.

ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0
ifconfig eth0:1 up

Enable packet forwarding.
To do this, edit /etc/sysctl.conf: add the following line

net.ipv4.ip_forward=1

Execute:

sysctl -w net.ipv4.ip_forward="1"

to apply this rule without rebooting
Install iptables, enter the following rule (to share internet with the second computer) and save it.

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Install and start dnsmasq (or you can use your provider's DNS).
On the second computer, set:

IP: 192.168.0.2
Netmask/Маска: 255.255.255.0
Gateway/Шлюз: 192.168.0.1
DNS: 192.168.0.1

The second computer should now be connected to the internet.
If iptables rules are not restored after reboot, add

/sbin/ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0
/sbin/ifconfig eth0:1 up
/sbin/sysctl -w net.ipv4.ip_forward="1"
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

to any startup script (e.g., /etc/rc.local)

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.