个人工具

UbuntuHelp:Internet/ConnectionSharing

来自Ubuntu中文

跳转至: 导航, 搜索
  1. title Internet Connection Sharing

<<Include(Tag/StyleCleanup)>>

Introduction

Internet Connection Sharing (ICS) provides the ability for one computer to share its internet connection with another computer. To do this, the computer with an internet connection must be configured to function as an internet gateway. The gateway computer is connected to the internet via a network card (wired or wireless) or a dial up adapter. A second computer (or network of computers) connects to the internet indirectly via the gateway computer. Situations in which ICS may be necessary include: dial up connection, an authenticated (PPPoA/E) connection, a wireless connection, or when it is impractical (such as with distance) to run multiple network cables to each computer. However, when possible it is preferable to share internet through a dedicated router. There are many reasons for this, but primarily because dedicated routers are built expressly for this purpose and are generally better suited for this simple task. Also, dedicated routers use only a fraction of the electricity required by an entire computer.

Ubuntu 9.10 Method

You will need either two Ethernet ports, one for the incoming internet and 1 for the connection to the other computer, or a Wireless card for the incoming internet and an Ethernet port for the connection to the other computer. When logged into your computer right click on the network indicator in the top right hand corner, click on "Edit Connections..." select "Auto Eth0" and click "Edit..." then go to the "IPv4 Settings" tab and next to "Method:" select the drop down box and select the option that says "Shared to other computers". Restart your computer and then you will be able to just plug-in any computer to your Ethernet port to share the connection.

Ubuntu Internet Gateway Method (iptables)

You will need two network cards in the gateway computer, or a PPP interface and a network card. One network card (or PPP interface) connects to the internet, we will call this card eth0. The other card connects to your internal network, we will call this eth1. It is also possible to do ICS with a single network card. In this case, use eth0 for the internet and eth0:0 for the internal network.

  1. Internet <<==>> eth0 <> Ubuntu gateway <> eth1 <<==>> Client PC
  2. Internet <<==>> ppp0 <> Ubuntu gateway <> eth1 <<==>> Client PC
  3. Internet <<==>> eth0 <> Ubuntu gateway <> eth0:0 <<==>> Client PC

Gateway set up

The following example will focus on the most common gateway setup; an Ubuntu computer with two wired network adapters (eth0 and eth1) hosting ICS to a static internal network configured for the 192.168.0.x subnet. For this example, eth0 is used to represent the network card connected to the internet and eth1 represents the network card connected to a client PC. You can replace eth0 and eth1 as needed for your situation. Also, any private IP subnet can be used for the internal network IP addresses. In summary: eth0 = the network adapter with internet (external or WAN).<
> eth1 = the network adapter to which a second computer is attached (internal or LAN).<
> 192.168.0.x = IP subnet for eth1<
> Your setup may be different. If so, make sure to change them accordingly in the following commands.

Configure internal network card
Configure your internal network card (eth1) for static IP like so:
sudo ifconfig eth1 192.168.0.1

(The external and internal network cards cannot be on the same subnet)

Configure NAT
Configure iptables for NAT translation so packets can be correctly routed through the Ubuntu gateway.
sudo iptables -A FORWARD -i eth0 -o eth1 -s 192.168.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE 

(rule1 allows forwarded packets (initial ones), rule2 allows forwarding of established connection packets (and those related to ones that started), rule3 does the NAT.)

Enable routing
  • Configure the gateway for routing between two interfaces by enabling IP forwarding:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
  • Edit /etc/sysctl.conf and add these lines:
net.ipv4.conf.default.forwarding=1
net.ipv4.conf.all.forwarding=1

The /etc/sysctl.conf edit is required because of following Bug (Hardy and later releases) Launchpad Bug Report

Client set up

Any OS can connect to the internet as an ICS client as long as networking has been configured correctly. The following example will focus on how to set up an Ubuntu ICS client. For this example, it is assumed that the client is connected to an Ubuntu gateway which has been configured to share ICS on the 192.168.0.x subnet according to the gateway set up outlined above. For this example, eth0 is the network card on the client which is connected (by crossover cable) to eth1 on the Ubuntu gateway. You can replace eth0 as needed for your situation. Also, any private IP subnet can be used for the internal network IP address, as long as it matches the subnet on the gateway.

Disable networking
sudo /etc/init.d/networking stop
Give the client a static IP address
sudo ifconfig eth0 192.168.0.100

This IP address can be anything within the gateway's private IP range.

Configure routing
sudo route add default gw 192.168.0.1

This address should match the IP address on the gateway's internal network card (eth1 in the above example).

Configure DNS servers

Unless your ICS gateway can also perform DNS, you must manually configure the client with your ISP DNS servers. If you do not know your ISP's DNS servers, you can use OpenDNS servers instead.

  • Backup your current /etc/resolve.conf file:
sudo cp /etc/resolv.conf /etc/resolv.conf.backup
  • Open /etc/dhcp3/dhclient.conf with your favorite text editor:
sudo nano /etc/dhcp3/dhclient.conf
  • Search for the line that starts "prepend domain-name-servers", and change it to look like this:
prepend domain-name-servers 208.67.222.222,208.67.220.220;

208.67.222.222 and 208.67.220.220 are OpenDNS DNS servers. If you wish to use your ISP's DNS servers, use them here instead of the OpenDNS servers.

Restart networking
sudo /etc/init.d/networking restart

Once this is finished, your client will now have access to the internet via ICS. Please direct any questions/comments to the Internet Connection Sharing Documentation thread.


A beginner's working example of a Ubuntu Desktop with 2 nic cards, sharing internet connection http://ubuntuforums.org/showthread.php?p=3713684

Advanced Gateway Configuration

The above example outlines how to do basic ICS on a static IP network. Once you have configured your Ubuntu computers for ICS and confirmed that everything works across your static network, there are a few advanced routing configurations which can make it much easier to set up the ICS client. Advanced configurations include DHCP server, and DNS server. A DHCP server allows the client to get an ip address automatically without having to manually configure a static IP. A DNS server allows the client to resolve internet host names without manually configuring DNS addresses.

DHCP/DNS server

This is deceptively easy, and will be acceptable for most situations. However, it will not allow the ICS client to see computers on different subnets.

  • Install software
sudo aptitude install dnsmasq
  • Stop the server

After dnsmasq has been installed, it is automatically started, so it will need to be stopped before changes can be made.

sudo /etc/init.d/dnsmasq stop
  • Make a backup of the well commented configuration file (we won't use any of this, but it's handy to have a copy of for reference later)
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf-backup
  • Edit /etc/dnsmasq.conf with your favorite text editor and add the following two lines:
interface=eth1
dhcp-range=192.168.0.100,192.168.0.250,72h

Note: The "interface" should match the interface that your clients are connected to, and the "dhcp-range" should be within the gateway's private IP subnet you configured according to the "Gateway set up" directions above.

  • Start the DHCP/DNS server
sudo /etc/init.d/dnsmasq start

Now your clients should be able to pull an automatic ip address and resolve host names.

Other approaches

The following section includes a rough outline of some alternative methods for configuring an ICS gateway. They are incomplete and untested. They are included simply for the sake of information.

Alternate server software (CLI)

There are other ways to host ICS, but they are outside the scope of this article.

Alternate NAT

The ipmasq daemon does NAT routing so you don't have to configure iptables. The following directions are incomplete and should not be considered a full description of what needs to be done to configure ipmasq.

sudo aptitude install ipmasq

Configure ipmasq to allow dhcp requests, otherwise you need to stop ipmasq to make a connection. You need to copy a .rul from the documentation directory into the /etc config and edit the interface name. Then reconfigure ipmasq to start after networking has been started

sudo dpkg-reconfigure ipmasq.
Dedicated DHCP server

dhcp3 is an easy to configure and scalable true DHCP server that can be configured for many different aplications. dhcp3 configuration is more complex, but it can be useful in many situations: https://help.ubuntu.com/community/dhcp3-server

Dedicated DNS server

BIND9 is a popular and well supported local DNS server. It is very versatile, and very powerful, but difficult to configure correctly: https://help.ubuntu.com/community/BIND9ServerHowto

Alternate gateway software (GUI)

Another approach --- set up Firestarter, to run connection sharing, set up dhcp3-server, and set its configuration to listen to the correct eth*. To change this later, run sudo dpkg-reconfigure dhcp3-server. Basically, you need to have Firestarter active/turned on/protecting, to have the connection shared. When you install dhcp3-server, it will place a sample config file in your /etc/dhcp3 folder, called dhcpd.conf. I suggest you install dhcp3-server first, and then firestarter, cause if you are lucky, firestarter will set up a new config file for dhcp3 for you. At any time that changes are made to your dhcpd.conf file, restart the server - sudo /etc/init.d/dhcp3-server restart will do it. Alternatively, every time you run the sudo dpkg-reconfigure dhcp3-server, at the end, your server will restart. There are several issues that I had...first of all, the Firestarter firewall won't even start if you don't have it configured to listen to the right interface...You can change which one it listens to in Preferences --> Network Settings. The Local network connected device must be the same as you have dhcp3-server listening to, of course, both checkboxes under that need to be checked. The Internet connected network device will be the one that is configured for Internet. Now, I have two NICs, but I have pppoe configured on eth0, and I have Internet connection sharing configured on the same one, cause eth0 is also configured for a static 192.168 internal IP for my internal network.

simple iptables example

simple example wlan0 has the internet connection eth0 is being used to share the connection it could be directly with a single pc via a crossover cable or switch or you could have a router with a cable from eth0 to the wan port and a whole lan setup behind this. Interestingly the internet connection could be ppp0 a 3g or mobile Internet modem.

  1. !/bin/sh <
    >
  2. <
    >
  3. internet connection sharing wlan0 is the gate way <
    >
  4. eth0 is the lan port this might use a straight ethernet cable to a router wan port or a switch or a single PC<
    >
  5. 192.168.2.2 is the port that is being used by the lan for access I changed it to 192.168.2.254 and set fixed addresses for the wan and router<
    >
  6. <
    >
  7. change wlan0 to ppp0 and you can use this for mobile broadband connection sharing<
    >
  8. <
    >

ifconfig eth0 up"<
> ifconfig eth0 192.168.2.1<
> echo “1” > /proc/sys/net/ipv4/ip_forward<
> iptables -t nat -A POSTROUTING -o wlan0 -s 192.168.2.0/24 -j MASQUERADE<
> iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 3074 -j DNAT --to-destination 192.168.2.2<
> iptables -t nat -A PREROUTING -i wlan0 -p udp -m multiport --dports 88,3074 -j DNAT --to-destination 192.168.2.2<
> iptables -A FORWARD -i wlan0 -d 192.168.2.2 -p tcp --dport 3074 -j ACCEPT<
> iptables -A FORWARD -i wlan0 -d 192.168.2.2 -p udp -m multiport --dports 88,3074 -j ACCEPT<
> You could use the above as a bash script changing things to suit <
> If things go wrong The Following script should save you if things get badly messed up. <
>

  1. !/bin/sh<
    >
  2. <
    >
  3. rc.flush-iptables - Resets iptables to default values. <
    >
  4. <
    >
  5. Copyright (C) 2001 Oskar Andreasson <bluefluxATkoffeinDOTnet><
    >
  6. <
    >
  7. This program is free software; you can redistribute it and/or modify<
    >
  8. it under the terms of the GNU General Public License as published by<
    >
  9. the Free Software Foundation; version 2 of the License.<
    >
  10. <
    >
  11. This program is distributed in the hope that it will be useful,<
    >
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of<
    >
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<
    >
  14. GNU General Public License for more details.<
    >
  15. <
    >
  16. You should have received a copy of the GNU General Public License<
    >
  17. along with this program or from the site that you downloaded it<
    >
  18. from; if not, write to the Free Software Foundation, Inc., 59 Temple<
    >
  19. Place, Suite 330, Boston, MA 02111-1307 USA<
    >
  20. <
    >
  21. Configurations<
    >
  22. <
    >

IPTABLES="/usr/sbin/iptables"<
>

  1. <
    >
  2. reset the default policies in the filter table.<
    >
  3. <
    >

$IPTABLES -P INPUT ACCEPT<
> $IPTABLES -P FORWARD ACCEPT<
> $IPTABLES -P OUTPUT ACCEPT<
>

  1. <
    >
  2. reset the default policies in the nat table.<
    >
  3. <
    >

$IPTABLES -t nat -P PREROUTING ACCEPT<
> $IPTABLES -t nat -P POSTROUTING ACCEPT<
> $IPTABLES -t nat -P OUTPUT ACCEPT<
>

  1. <
    >
  2. reset the default policies in the mangle table.<
    >
  3. <
    >

$IPTABLES -t mangle -P PREROUTING ACCEPT<
> $IPTABLES -t mangle -P POSTROUTING ACCEPT<
> $IPTABLES -t mangle -P INPUT ACCEPT<
> $IPTABLES -t mangle -P OUTPUT ACCEPT<
> $IPTABLES -t mangle -P FORWARD ACCEPT<
>

  1. <
    >
  2. flush all the rules in the filter and nat tables.<
    >
  3. <
    >

$IPTABLES -F<
> $IPTABLES -t nat -F<
> $IPTABLES -t mangle -F<
>

  1. <
    >
  2. erase all chains that's not default in filter and nat table.<
    >
  3. <
    >

$IPTABLES -X<
> $IPTABLES -t nat -X<
> $IPTABLES -t mangle -X<
> Further reading https://help.ubuntu.com/community/IptablesHowTo Internet Connection Sharing Documentation thread http://ubuntuforums.org/showthread.php?s=88b74f79f0ab07638e6b361c09040b45&t=503287

See also