个人工具

UbuntuHelp:EasyWirelessToWiredConnectionSharing

来自Ubuntu中文

跳转至: 导航, 搜索

<<Include(Tag/StyleCleanup)>> <<Include(Tag/ContentCleanup)>>

Introduction

To share wireless connection via router, switch, or hub to the wired machine from a machine with a wireless connection. The simplest solution to sharing that wireless connection is to purchase a wireless adaptor for the second computer, but sometimes that is not an option. Note

  • An alternate solution is to use a crossover cable from the wireless system to the wired one, thus cutting out the entire switch/router/hub element.
  • To be sure of which interfaces you need to configure, first try the networking tools provided by Gnome or KDE. (Alternately, when your interfaces are up and running, before you proceed with these instructions, check ifconfig.)

The most common interface device names are: eth0, eth1, ath0, ath1, wlan0,wlan1,ppp0,ppp1.


Firestarter is no longer recommended for configuring IPtables. UFW is now the recommended program. Please note that the application "firestarter" (from the Universe package repository) also can set up Internet connection sharing, with a pretty easy-to-use graphical user interface.

Setup

Firstly, you will need the following:

  • Desktop or Laptop system with both wireless and Ethernet adaptors. (Which we will call Wireless-PC)
  • Ethernet adaptor for the second system. (Which we will call Wired-PC)
  • A Switch, Hub, or Router. (Which we will call Share-Device)
  • Two lengths of Ethernet Cable, long enough to connect Wireless-PC->Share-Device-Wired-PC
  • The program dhcpd should be installed for these steps to work. If it isn't, please go to Applications->Accessories->Terminal and type:
sudo apt-get install dhcpd

Section One: The Basic Setup

Now that all the necessary equipment has been assembled, it's time to get the easy part over with now.

  1. Connect the Ethernet Cables to Wireless-PC, and Wired-PC
  2. Connect their opposite ends to Share-Device
  3. Power up all devices involved.

Section Two: Configuring the Setup

Now that we've got all that out of the way, it's time to begin our journey.

  1. Double-check all physical connections between Wireless-PC, Share-Device and Wired-PC.
  2. Make a backup of your dhcpd.conf
sudo cp /etc/dhcpd.conf /etc/dhcpd.conf.backup
  1. Use your favorite text editor to open /etc/dhcpd.conf, In this example, we'll use GEdit.
gksudo gedit /etc/dhcpd.conf

Warning Remember, don't use sudo with GUI applications!

  • Add the following (It is safe, at this point, to remove everything in the configuration.):
#/etc/dhcpd.conf
option subnet-mask 255.255.255.0;
default-lease-time 600;
max-lease-time 7200;

subnet 192.168.27.0 netmask 255.255.255.0 {
  range 192.168.27.10 192.168.27.20;
  option broadcast-address 192.168.27.255;
  option routers 192.168.27.1;
}

Note You can change the IP range to whatever you choose, just make sure that you do not cause an IP conflict by assigning the same range as your Wireless Access Device. Double check this by typing 'ifconfig' and looking at the IP set for your wireless device. (wlan0, ath0, etc.) Also it will probably be easiest to stick to a class C network (e.g. 192.168.x) to lower the risk of confusion.

Backing Up

  1. Backup the following file, just for extra safety..
  • sudo cp /etc/default/dhcp /etc/default/dhcp.backup
  1. Edit the file. Again, in this case we'll use GEdit.
  • gksudo gedit /etc/default/dhcp (don't use sudo with graphical apps -cpk1)
  1. In the section "INTERFACES", we will set dhcpd to listen for DHCP requests on your Ethernet interface.
  • INTERFACES="eth0"

Save and exit GEdit.

  1. Next, we'll modify the priority of your interfaces. This may not be a necessary step for everyone, but I found that after bringing up eth0, my system attempted to use that as my internet connection, rather than my wireless interface. Make sure you back up the /etc/network/interfaces file using the steps for backing up outline in previous steps.
  • gksudo gedit /etc/network/interface (don't use sudo with graphical apps -cpk1)
  • In the section # The primary network interface', add the following:
iface wlan0 inet dhcp
wireless-essid default

iface eth0 inet static
address 192.168.27.1
netmask 255.255.255.0
gateway 192.168.0.100
pre-up /sbin/iptables-restore /etc/network/iptables

The pre-up section is necessary. It's something we'll touch on later, but for now just trust me and add it. (Remember to change the IP ranges, and interface device names to reflect those present on your system. Also, don't forget to set the gateway to your Wireless Connection's gateway if you want Wired to be able to resolve domains.)

  1. Drop your wireless and wired connections.
  • ifconfig wlan0 down
  • ifconfig eth0 down
  1. Do the following to add the appropriate iptables rule:
sudo iptables -t nat -A POSTROUTING -s 192.168.27.0/16 -o wlan0 -j MASQUERADE
sudo iptables-save /etc/network/iptables

Now when you bring up eth0, you won't have to set up the iptables rules again. They'll be auto-set for you each time you bring up eth0. 10. Starting and Testing the Network

  • ifconfig eth0 up
  • sudo /etc/init.d/networking start
  • sudo /etc/init.d/dhcp start
  • ifconfig wlan0 up

(You can also make a shell script using those commands. It is suggest that be done since otherwise these commands will have to be retyped after every reboot.) On Wired-PC, ping ubuntu.com. If the ping is successful all is well. You now have a working connection to the Internet.

Troubleshooting

If the ping is unsuccessful: On Wireless-PC ping ubuntu.com to get the site's IP address and write it down. Go back to Wired-PC and try pinging google with the IP address. If this works then you simply have a DNS issue which can easily be fixed. Open /etc/resolv.conf with your favourite editor (for this example we will use gedit) and type the following command:

gksudo gedit /etc/resolv.conf

Then add in "nameserver <IP Address>" (without the angle brackets or the double quotes and change the IP address to whatever your router that Wireless-PC is getting the Internet from).


TODO

  • TODO: there is only one mention of checking for interface names, the rest of the article only makes the most generic references to interface names.
  • TODO: Review that this is actually the appropriate syntax. "sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE" should be all that you need here, since the source should not matter here -cpk1