个人工具

UbuntuHelp:Router

来自Ubuntu中文

Wikibot讨论 | 贡献2009年5月12日 (二) 18:42的版本

跳转至: 导航, 搜索

This is where the new Ubuntu Router page is in development, please visit UbuntuWirelessRouter/New for more information

Setting up an Ubuntu Wired/Wireless Router

Preface

This article is intended for intermediate and advanced users who would like to set up an Ubuntu installation acting as a router at home or in their office. The end result is a powerful router that can provide functionality similar to popular products (for example, the Linksys WikiPedia:WRT54G).

Network Manager

This tool is perfect for simple networking problems, especially for laptop users. It is not appropriate for setting up a router, even though it does have a very basic internet connection sharing capability.

Typical Example

Home Office The basis is one PC connected to a broadband internet connection. Additional devices:

  • network printer
  • wifi PDA
  • laptop

Some of these devices should be able to communicate with each other, some should be allowed to communicate with the internet. Some are wired, and some are wireless. With this guide, your PC can share the internet over additional network interfaces, such as wireless cards and ethernet cards. University Single PC on a university LAN. Additional devices:

  • X-Box running mythbuntu
  • Wifi mobile phone
  • laptops
Quick and Dirty

If you would like to try to do the same thing the 'quick and dirty' way, see EasyRouter, a much quicker method with much less flexibility.

Technical Overview

The router that will be created is an Internet gateway for wired and/or wireless clients to share an internet connection with one IP address. The essential components are:

  • routing of packets from your local networks to the internet, with WikiPedia:IP_masquerading
  • handling DNS requests
  • providing IP addresses to devices on your local networks (DHCP)

This router can also provide:

  • A firewall
  • port forwarding

Prerequisites

Internet Connection

This is not strictly necessary, but is probably the reason you are following this article. You can set up a router in an isolated network using these instructions, but you will likely have specialist requirements, and need to make changes.

Router Hardware

Use your desktop PC

If you have a recent PC, bought in the last two years, it probably has enough horsepower to run all your normal applications and act as a router at the same time.

Dedicated Hardware

You'll need a dedicated computer to act as the router. The computer can use old hardware and having the minimum requirements to install Ubuntu should suffice. The author of this article runs his router on a P3 600mhz processor with 256MB of RAM. You are encouraged use this as a server for other applications perhaps by installing postfix, apache, mysql, and/or samba. This guide recommends a server installation of Ubuntu, but there's no reason why a desktop installation wouldn't work. If you plan to be able to access the router remotely, install ssh before proceeding. The following needs to be physically installed and recognized by the kernel on your router:

  • A network adapter
  • For a wired network,
  • Another network adapter, typically an ethernet port on a motherboard, or PCI card.
  • For a wireless network,
  • A wireless network adapter
  • Ideally it should be able run in "master" mode, although "ad-hoc" mode might be good enough. Cards and chipsets which can work in master mode (otherwise known as access point or ap mode, are listed at the Linux Wireless Project.
  • `sudo ifconfig <interface name> down && sudo iwconfig <interface name> mode master` should not return an error; but some cards take extra commands to set master mode. mac80211 compatible drivers can run cards in master mode. Pay careful attention to what hardware you buy.
  • If your wireless network adapter is not recognized by your server installation of Ubuntu, it may use the madwifi chipset (like the D-Link DWL-G520). Please visit Router/Madwifi for more information.

Running `ifconfig` will show you what network interfaces are available.

Terminology

interface is used to mean the operating system's name for a place which sends or receives data packets. It is often, but not necessarily, the same as a device. An interface may have several devices associated (e.g. a bridge), or a single device may have several interfaces. device will refer here to the bit of hardware dealing with your network connections.

Internal Network Information

Here are the values we'll use to set up your internal network. Advanced users use caution when changing them as the changes will need to be reflected in all further router configuration. Firstly, discover the IP address of your internet-connected interface. If you are plugged into a business or academic LAN, this is very likely to be given by: ifconfig eth0 For a home broadband user, it may be a ppp interface; and a wireless user could have any number of interface names, such as ethX, wlanX, athX, where X is a digit. Be aware that it is important to use different numbers below to the one given to your internet device. This is probably the case already, but if you are not directly connected to the internet, you may be on a LAN with the same address range. If you already have a broadband router, for example, it might have given you the 192.168.0.2 address, and kept 192.168.0.1 for itself. If so, change the third digit (0) to another number (and do so throughout the rest of this tutorial).

Router
Address 192.168.0.1
Network 192.168.0.0
Netmask 255.255.255.0
Broadcast 192.168.0.255
Clients
Addresses 192.168.0.2 - 192.168.0.254
Netmask 255.255.255.0
Broadcast 192.168.0.255
Gateway 192.168.0.1

Setting Up Your Network Interfaces

Device Naming Overview

Network Device Internal or External Network Description
eth0 External Network adapter connected to an external network (your broadband connection)
eth1 Internal Network adapter connected to a hub or switch
wlan0 Internal Wireless network adapter
br0 Internal Network bridge between eth1 and wlan0 that will treat the two like one device

It is important to note that the names of the network interfaces above (eth0, eth1, and wlan0) are used as convention. It is very likely that your router will recognize its devices under different names (for example, madwifi calls its wireless device ath0). Please substitute the names of your device accordingly. For information about how to change the names of your network devices, try `man iftab`.

Taking a Backup

Issue the following command to take a backup of your current network configuration:

sudo cp /etc/network/interfaces /etc/network/interfaces.bak

Configuring the External Network Interface

Setting up External Network Interface

Here, we configure the Ubuntu networking system to bring the the local loopback and external network interfaces up by editing `/etc/networking/interfaces`. The primary goal here is to set up your external network interface (eth0, or whatever you're using in place of it) to be brought up by the networking subsystem. The examples below are only for the most basic setups. If your setup requires additional configuration, for example you need to setup ADSL with PPPoE, adapt the following examples so that the end result is your external network interface connected to the Internet.

For Dynamic IP Addresses (DHCP) Only

Open `/etc/network/interfaces` with your favourite editor. Delete everything and paste in what is below. Follow the commented out instructions carefully.

# Set up the local loopback interface
auto lo
iface lo inet loopback

# Set up the external interface
#
# Don't forget to change eth0 to the proper name of the external
# interface if applicable.
#
auto eth0
iface eth0 inet dhcp
For Static IP Address Only

Open `/etc/network/interfaces` with your favourite editor. Delete everything and paste in what is below. Follow the commented out instructions carefully.

# Set up the local loopback interface
auto lo
iface lo inet loopback

# Set up the External interface
#
# For every xxx.xxx.xxx.xxx, enter the numeric address given to you
# by your Internet provider. Don't forget to change eth0 to the proper
# name of the external interface if applicable.
#
auto eth0
iface eth0 inet static
    address xxx.xxx.xxx.xxx
    netmask xxx.xxx.xxx.xxx
    gateway xxx.xxx.xxx.xxx

Now, set up your DNS servers as given to you by your service provider in `/etc/resolv.conf`, which should look something like this

nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx

You can visit the Ubuntu Server Guide - Network Configuration documentation for more information

Testing Connectivity

Reload the network configuration and test for connectivity,

sudo /etc/init.d/networking restart
ping -c 3 -W 10 ubuntu.com

And if all goes well something similar should return:

PING ubuntu.com (82.211.81.166) 56(84) bytes of data.
64 bytes from signey.ubuntu.com (82.211.81.166): icmp_seq=1 ttl=43 time=99.9 ms
64 bytes from signey.ubuntu.com (82.211.81.166): icmp_seq=2 ttl=43 time=109 ms
64 bytes from signey.ubuntu.com (82.211.81.166): icmp_seq=3 ttl=43 time=100 ms

--- ubuntu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 99.982/103.450/109.419/4.254 ms

Configuring the Internal Network Interfaces

Wired Only

Append the following to `/etc/network/interfaces` and follow the commented out instructions carefully.

# Set up the internal wired network
#
# Don't forget to change eth1 to the proper name of the internal
# wired network interface if applicable.
#
auto eth1
iface eth1 inet static
    address 192.168.0.1
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255

Your internal network interface is: eth1 (or whatever you're using in place of it)

Wireless Only

If you plan on using WEP, generate a network key,

dd if=/dev/random bs=1 count=13 2>/dev/null | xxd -p

Append the following to `/etc/network/interfaces` and follow the commented out instructions carefully.

# Set up the internal wireless network
#
# Don't forget to change wlan0 to the proper name of the internal
# wireless network interface if applicable.
#
# If you would like to use WEP, uncomment the line 'wireless-key'
# and replace '<key goes here>' with a WEP key.
# 
# You may also change the network essid and channel.
#
auto wlan0
iface wlan0 inet static
    wireless-mode master
    wireless-essid "UbuntuWireless"
    wireless-channel 1
    #wireless-key <key goes here>
    address 192.168.0.1
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255

Your internal network interface is: wlan0 (or whatever you're using in place of it)

Both Wired and Wireless

First install the necessary tools to create a network bridge,

sudo apt-get install bridge-utils

If you plan on using WEP, generate a network key,

dd if=/dev/random bs=1 count=13 2>/dev/null | xxd -p

Append the following to `/etc/network/interfaces` and follow the commented out instructions carefully.

# Set up the internal wireless network
#
# Don't forget to change wlan0 to the proper name of the internal
# wireless network interface if applicable.
#
# If you would like to use WEP, uncomment the line 'wireless-key'
# and replace '<key goes here>' with a WEP key.
# 
# You may also change the network essid and channel.
#
auto wlan0
iface wlan0 inet manual
    wireless-mode master
    wireless-essid "UbuntuWireless"
    wireless-channel 1
    #wireless-key <key goes here>

# Set up the internal wired network
#
# It's not necessary to bring this interface up as the bridge
# we are about to create does this. Leave these lines commented.
#
#auto eth1
#iface eth1 inet manual


# Set up the internal wired/wireless network bridge
#
# Don't forget to change wlan0 and eth1 to the proper name of
# the internal wired and wireless interfaces if applicable.
#
auto br0
iface br0 inet static
    address 192.168.0.1
    network 192.168.0.0
    netmask 255.255.255.0
    broadcast 192.168.0.255
    bridge-ports eth1 wlan0

Your internal network interface is: br0

Restart Networking

Now, if the following command is executed successfully, your networking devices should have been properly configured.

sudo /etc/init.d/networking restart

Firewall

See Router/Firewall

DHCP and DNS

Dynamic Host Configuration Protocol (DHCP)