CentOS / Redhat Linux Internet Connection Sharing

by on March 18, 2007 · 19 comments· LAST UPDATED January 18, 2008

in , ,

Q. How do I configure CentOS / Redhat Linux computer to share my internet connection? How do I configure RHEL as a software router with two interfaces? How do I share my single connection with other PCs on LAN?

A. Linux can be easily configured to share an internet connection using iptables. Al you need to two network interface cards as follows:
a) Your internal (LAN) network connected via eth0 with static ip address 192.168.1.254
b) Your external WAN) network is connected via eth1 with static ip address 192.168.2.1

Please note that interface eth1 may have public IP address or IP assigned by ISP. eth1 may be connected to a dedicated DSL / ADSL / WAN / Cable router.

Step # 1: Enable Packet Forwarding

Login as the root user. Open /etc/sysctl.conf file
# vi /etc/sysctl.conf
Add the following line to enable packet forwarding for IPv4:
net.ipv4.conf.default.forwarding=1
Save and close the file. Restart networking:
# service network restart

Step # 2: Enable IP masquerading

In Linux networking, Network Address Translation (NAT) or Network Masquerading (IP Masquerading) is a technique of transceiving network traffic through a router that involves re-writing the source and/or destination IP addresses and usually also the TCP/UDP port numbers of IP packets as they pass through. In short, IP masquerading is used to share the internet connection.

Share internet connection

To share network connection via eth1, enter the following rule at command prompt (following useful for ppp0 or dial up connection):
# service iptables stop
# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
# service iptables save
# service iptables restart

Open your Windows / Mac / Linux computer networking GUI tool and point router IP to 192.168.1.254 (eth0 Linux IP). You also need to setup DNS IP such as 208.67.222.222 and 208.67.220.220. You should now able to ping or browse the internet:
c:> ping 202.54.1.20
c:> ping google.com

Shell Script to Setup Basic Linux Network Sharing

This is basic connection sharing, following shell script is for more advance user. [ Download the script here and modify SHARE_IF as per your requirements. ]

#!/bin/bash
# Created by nixCraft - www.cyberciti.biz
IPT="/sbin/iptables"
MOD="/sbin/modprobe"
# set wan interface such as eth1 or ppp0
SHARE_IF="eth1"
# clean old fw
echo "Clearing old firewall rules..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
# Get some kernel modules
echo "Loading kernel modules..."
$MOD ip_tables
$MOD iptable_filter
$MOD iptable_nat
$MOD ip_conntrack
$MOD ipt_MASQUERADE
$MOD ip_nat_ftp
$MOD ip_nat_irc
$MOD ip_conntrack_ftp
$MOD ip_conntrack_irc
# Clean old rules if any, rhel specific but above will take care of everything
# service iptables stop
# unlimited traffic via loopback device
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
echo "Setting ${SHARE_IF} as router interface..."
$IPT --table nat --append POSTROUTING --out-interface ${SHARE_IF} -j MASQUERADE
# Start other custom rules
#$IPT 
# End other custom rules
echo "*** Instructions on TCP/IP On The Windows / Mac / Linux Masqueraded Client ***"
echo "1. Login to your other LAN desktop computers"
echo "2. Open network configuration GUI tool such. Under Windows XP - Click Start, click Control Panel, click Network and Internet Connections, and then click Network Connections"
echo "3. Set DNS (NS1 and NS2) to 208.67.222.222 and 208.67.220.220"
echo "4. Select the 'Gateway' tab in the TCP/IP properties dialog."
echo "5. Enter $(ifconfig ${SHARE_IF} | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}') as the default gateway."
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 19 comments… read them below or add one }

1 Anas February 2, 2008 at 3:14 pm

Dear friend,

could i find access.log in Internet Sharing
CentOS / Redhat Linux Internet Connection Sharing
http://www.cyberciti.biz/faq/rhel-fedora-linux-internet-connection-sharing-howto/

Reply

2 anand June 3, 2008 at 9:49 am

thank you for the tutorial. Saved my day :)

Reply

3 gilq June 24, 2008 at 10:05 am

Thanks for the tutorial. Tried the advanced version, went ok for few minutes then connection timed out. Appreciate any feedback.

Reply

4 Pallav July 3, 2008 at 11:36 am

Thanks

Reply

5 denish December 19, 2008 at 10:58 am

i have problem to share the internet through RedHat linux plz solve my problem

Reply

6 vadi01 June 7, 2009 at 9:15 pm

Good one. But unfortunately there is no way of controlling the users who use the connection via the server.

I advice you use try out traffpro. A open-source monitor and security system. Allows you to have a more easier way of setting up internet sharing.

Reply

7 sarfaraz August 27, 2009 at 12:40 pm

thank you very useful command

Reply

8 rahul khandelwal October 20, 2009 at 8:58 am

Great job !!!
thanks a lot !!!!
After Getting your solution , hardly it took 2 seconds to work out .

THANKS a lot!!!!!

Reply

9 Indiana September 4, 2010 at 7:23 pm

Hi,
I recently purchased a sony bluray player and wanted to connect it to the internet through an old dell wireless G laptop that is no longer being actively used. The setup was
BluRay(Ethernet port) -> Dell Laptop (Ethernet Port) -> Dell Laptop (Wireless G card) -> Wireless G Router (Integrated ADSL modem/wireless AP/router) -> Internet.
Tried this tutorial but could not get my setup working. I disconnected my bluray and hooked up an apple macbook to the dell laptop for testing. I was able to ping the dell ethernet port (eth 0 @ 192.168.1.254) and the dell wireless card (eth1 @ 10.0.0.7). The wireless network is setup to obtain an IP address using DHCP from my wireless router. I was almost ready to give up when I found a different website with a tutorial. The only additional command that seemed to get my connection to work is:
echo 1 > /proc/sys/net/ipv4/ip_forward
After executing this command, my apple macbook and the bluray were able to connect to the internet just fine.
I can’t figure why the additional command is required and why it’s missing from the script. Oh, and I also had to manually setup the ethernet card address like so:
ifconfig eth0 192.168.1.254
I hope this helps someone!

Reply

10 pramod February 18, 2013 at 6:28 am

first have to configure smb.config file to configure your network.
then u can see all connected systems which are connected to your network.
and up your ethernet port and give ip
configure other ip adderss in your network.

Reply

11 Joe November 8, 2010 at 4:07 am

It all works
But, all the settings disappear if network is restarted.

How to make changes persistent?

Reply

12 SIFE January 1, 2011 at 1:16 pm

I have issue with that in VirtualBox, I have lab in VirtualBox like this:
VBOX ( CentOS(2 nic’s, eth0 -> NAT and eth1 -> internal), 3 client’s)
CentOS:
eth0: have dynamic IP, eth1 have static IP, CentOS can access to internet but other client can’t, I setup in them default gateway and static IP but it doesn’t work.

Reply

13 Gurpreet Singh February 21, 2011 at 7:01 am

its working thanks a lot…………….

Reply

14 vijay jadon July 20, 2011 at 10:18 am

i am new to red hat and i am not able to make any wireless connection. so plz provide a solution ???

Reply

15 khimanand November 28, 2011 at 4:39 am

thanks, this solution helps me

Reply

16 KS Ghosh January 30, 2012 at 10:37 am

Thanks a Lot.. Its work fine…. Now I want to stop the LAN user for accessing Bit torrent , mp3 download and other bandwidth consuming process. Will you please guide me.

Reply

17 Jouni "Rautamiekka" Järvinen September 12, 2012 at 1:33 pm

The script uses
1) hardcoded paths to programs where the §`which iptables`§ would be better.
2) direct calls to iptables instead of the hardcoded.

I had a 3rd point but forgot it.

Reply

18 luis September 29, 2013 at 6:37 am

Can someone please tell me how I can share my wireless internet connection via my ethernet connection to another computer? Here is my scenario: Computer 1 is connected to internet via Wireless. I want to share that wireless internet connection to computer 2 via ethernet. I don’t want to use static IP’s, I want computer 2 to be able to pick up an IP address from my home router, via computer 1. How can I accomplish this?

Reply

19 luis September 29, 2013 at 6:38 am

thanks in advance for your response.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: