Ubuntu搭建DHCP 和ipv6

1. 安装dhcp3 server

apt-get install isc-dhcp-server

2. 编辑文件/etc/default/isc-dhcp-server

填入eth0(eno1)

INTERFACES="eth0"

3. 编辑文件:/etc/dhcp/dhcpd.conf

写入

# minimal sample /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;

subnet 192.168.1.0 netmask 255.255.255.0 {
 range 192.168.1.150 192.168.1.200;
 option routers 192.168.1.254;
 option domain-name-servers 192.168.1.1, 192.168.1.2;
 option domain-name "mydomain.example";
} 

4.重启dhcp服务 sudo systemctl restart isc-dhcp-server.servic

5. eth0配置同网段静态ip.

https://help.ubuntu.com/lts/serverguide/dhcp.html

http://www.linuxidc.com/Linux/2014-05/101579.htm

IPV6搭建:

http://xiaopangxiongyuan.blog.163.com/blog/static/12490117020116263115442/?COLLCC=2319623943&

http://mirrors.bieringer.de/Linux+IPv6-HOWTO/hints-daemons-isc-dhcp.html

http://cache.baiducontent.com/c?m=9d78d513d98411e804abd3690d67813c481697634d8c92517bc0d408cd6b01070124f4ba543f0d548d98297001d8181dbcac2172405f77f186968a4edfbc90282d8b24346d4fc607498247f8d610728773cb04a8b21fe4b0fb3992a9d9&p=90648415d9c041ad00aec7710f0794&newp=857cc910c5904ead0dbd9b7d0d1481231610db2151d7d2136b82c825d7331b001c3bbfb423231705d8c27d6d01ac4f59e1f33071360625a3dda5c91d9fb4c57479dd&user=baidu&fm=sc&query=ubuntu+dhcp+ipv6&qid=9d1d705e000053da&p1=1

DHCP启动和停止:

sudo /etc/init.d/isc-dhcp-server start

sudo /etc/init.d/isc-dhcp-server stop

service isc-dhcp-server6 start

service isc-dhcp-server6 stop

systemctl start isc-dhcp-server.service

systemctl stop isc-dhcp-server.service

http://blog.sina.com.cn/s/blog_71d9aee401012bq9.html

两台笔记本 A和B

A: Ubuntu 11.04 + 有线网卡(eth0) + 无线网卡(eth1)

B: Windows 7 + 有线网卡 + 无线网卡

A,B的无线网卡都连接到家庭无线路由器,网关为192.168.0.1,可以上互联网。A,B的有线网卡通过网线直连,A作为DHCP server和网关,为B的有线网卡分配IP地址

A安装DHCP server步骤:

1. sudo apt-get install dhcp3-server

2. vi /etc/default/isc-dhcp-server

INTERFACES="eth0"

3. 给eth0配置ipv4静态地址: 192.168.1.1。

注意不要用network connection配置静态IP,它不会写/etc/network/interfaces文件,导致dhcp server无法自动启动。

为了使DHCP server在启动时自动启动,vi /etc/network/interfaces, 加入:

auto eth0 iface eth0 inet static address 192.168.1.1 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255

4.vi /etc/dhcp/dhcpd.conf

subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.10 192.168.1.200; option broadcast-address 192.168.1.255; option routers 192.168.1.1; default-lease-time 6000; max-lease-time 8000000; }

5. sudo /etc/init.d/isc-dhcp-server start

如果出错,查看/var/log/syslog的错误提示

A安装玩DHCP server后,B的有线网卡可以分配到IP地址。但是B不能上互联网了。

route print

0.0.0.0 0.0.0.0 192.168.0.1 192.168.0.99 26

0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.10 10

连互联网时都走HOP数小的192.168.1.1路由,导致无法上互联网。

运行下面命令解决问题:

route delete 0.0.0.0

route add 0.0.0.0 mask 0.0.0.0 192.168.0.1

route add 192.168.1.0 mask 255.255.255.0 192.168.1.1

实验2:B关闭无线网卡,希望通过A作为路由器来连互联网

1. 在DHCP服务器的配置文件中,加入DNS服务器。这样B才能通过A知道DNS服务器的地址

vi /etc/dhcp/dhcpd.conf

subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.10 192.168.1.200; option broadcast-address 192.168.1.255; option routers 192.168.1.1;

option domain-name-servers 180.168.255.18; default-lease-time 6000; max-lease-time 8000000; }

2.

A中打开ip包转发功能, vi /etc/sysctl.conf,删除下面一行的注释

net.ipv4.ip_forward=1

(sudo sysctl -p 使改变的配置立即生效)

同时添加iptables(临时方法,重启后会失效):

sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j MASQUERADE

(-o eth1 指定output network interface为eth1)

实验3:A使用radvd(Router Advertisement Daemon)为B分配ipv6地址

radvd 使用 Neighbor Discovery Protocol (NDP) as specified in RFC 2461

1. sudo apt-get install radvd

2. vi /etc/radvd.conf (需新建该文件),如下面几行:

interface eth0 { MinRtrAdvInterval 3; MaxRtrAdvInterval 10; AdvLinkMTU 1280; AdvSendAdvert on; prefix fec0:1111:2222:3333::/64 { AdvOnLink on; AdvAutonomous on; AdvValidLifetime 86400; AdvPreferredLifetime 86400; AdvRouterAddr on; }; };

3. 在/etc/sysctl.conf 中打开 net.ipv6.conf.all.forwarding=1

4. sudo /etc/init.d/radvd start

(如果在 radvd 运行状态下重启机器,radvd 会在开机后自动运行)

5. 在B上运行ipconfig:

以太网适配器 本地连接:

连接特定的 DNS 后缀 . . . . . . . : 本地站点的 IPv6 地址. . . . . . . : fec0:1111:2222:3333:a432:4f4d:72bf:ed0a%1

本地链接 IPv6 地址. . . . . . . . : fe80::a432:4f4d:72bf:ed0a IPv4 地址 . . . . . . . . . . . . : 192.168.1.10 子网掩码 . . . . . . . . . . . . : 255.255.255.0 默认网关. . . . . . . . . . . . . : fe80::211:43ff:fe76:cb8

注意windows下 fec0:1111:2222:3333:a432:4f4d:72bf:ed0a%1 中的%1表示网络接口编号

ping本机地址:

C:\>ping fec0:1111:2222:3333:a432:4f4d:72bf:ed0a (本地站点地址)

正在 Ping fec0:1111:2222:3333:a432:4f4d:72bf:ed0a 具有 32 字节的数据: 来自 fec0:1111:2222:3333:a432:4f4d:72bf:ed0a 的回复: 时间<1ms

C:\>ping fe80::a432:4f4d:72bf:ed0a (本地链路地址)

正在 Ping fe80::a432:4f4d:72bf:ed0a 具有 32 字节的数据: 来自 fe80::a432:4f4d:72bf:ed0a 的回复: 时间<1ms

6.A 上运行ifconfig

$ifconfig

eth0 Link encap:Ethernet HWaddr 00:11:43:76:0c:b8 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::211:43ff:fe76:cb8/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4634 errors:0 dropped:0 overruns:0 frame:0 TX packets:1299 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:677921 (677.9 KB) TX bytes:178937 (178.9 KB) Interrupt:18

ping本机地址 (注意linux上ping6的用法,必须指定接口):

$ ping6 -I eth0 fe80::211:43ff:fe76:cb8

PING fe80::211:43ff:fe76:cb8(fe80::211:43ff:fe76:cb8) from fe80::211:43ff:fe76:cb8 eth0: 56 data bytes 64 bytes from fe80::211:43ff:fe76:cb8: icmp_seq=1 ttl=64 time=0.046 ms

7. B ping A

C:\>ping fe80::211:43ff:fe76:cb8

正在 Ping fe80::211:43ff:fe76:cb8 具有 32 字节的数据: 来自 fe80::211:43ff:fe76:cb8 的回复: 时间<1ms

8. A ping B

$ ping6 -I eth0 fe80::a432:4f4d:72bf:ed0a PING fe80::a432:4f4d:72bf:ed0a(fe80::a432:4f4d:72bf:ed0a) from fe80::211:43ff:fe76:cb8 eth0: 56 data bytes 64 bytes from fe80::a432:4f4d:72bf:ed0a: icmp_seq=1 ttl=64 time=0.733 ms

g$ ping6 -I eth0 fec0:1111:2222:3333:a432:4f4d:72bf:ed0a connect: Network is unreachable

A只能ping通B的本地链路地址,无法ping通本地站点地址。因为使用本地站点地址作为源或目的地址的数据报文不会被转发到本站点(相当于一个私有网络)外的其它站点,B的本地站点地址只能在B本地使用。

实验4: A 使用 wide-dhcpv6-server 为B分配ipv6地址

1. sudo apt-get install wide-dhcpv6-server

2. vi /etc/dhcp/dhcpd6.conf

http://www.buntschu.ch/blog/?p=344

As the IPv4 address space is now exhausted, all networking people are focusing on IPv6. In this post, I will explain how to configure DHCPv6 on a Linux machine running Ubuntu 11.04 server (64 bits) with the ISC DHCPv6 service. Be carefull, you need the version 4.x of the ISC DHCP server that support IPv6. It does not exist as a package for older Ubuntu version. The ISC version 4.1 and above superseed the DHCPv6 serveur.

Step 1: configure a fixed IPv6 address on your ethernet card

To specify a fixed (manual) IPv4/IPv6 address, you need to modify the file /etc/network/interfaces:

auto eth1

iface eth1 inet static

address 172.16.14.251

netmask 255.255.255.0

gateway 172.16.14.1

iface eth1 inet6 static

pre-up modprobe ipv6

address 2001:620:40b:555::4

netmask 64

gateway 2001:620:40b:555::1

Don’t forget to restart the networking service to take this configuration into account:

#/etc/init.d/networking restart

Step 2: install the DHCPv6 server

As of this writing, the version 4.1.1P1 of the ISC DHCP server is the actual package available on Ubuntu.

#apt-get install isc-dhcp-server

Step 3: configure the DHCPv6 server

The actual version of ISC does not allow to run one instance of ISC and support at the same time DHCPv4 and DHCPv6. You need to start two instances of DHCP, one for IPv4 and one for IPv6.

  • Be sure that your server will listen for DHCP request on the correct interface. Edit the /etc/default/isc-dhcp-server file:

INTERFACES="eth1"

  • Configure the IPv4 address Pool by editing/creating the /etc/dhcp/dhcpd.conf file:

option domain-name "buntschu.ch";

option domain-name-servers ns.buntschu.ch;

default-lease-time 600;

max-lease-time 7200;

log-facility local7;

subnet 172.16.14.0 netmask 255.255.255.0 {

range 172.16.14.100 172.16.14.110;}

  • Configure the IPv6 address Pool by editing/creating the /etc/dhcp/dhcpd6.conf file:

option domain-name "buntschu.ch";

option domain-name-servers ns.buntschu.ch;

default-lease-time 600;

max-lease-time 7200;

log-facility local7;

subnet6 2001:620:40b:555::/64 {

range6 2001:620:40b:555::100 2001:620:40b:555::110;}

  • Create a second startup file for the DHCPv6 server

#cp /etc/init.d/isc-dhcp-server /etc/init.d/isc-dhcp6-server

#update-rc.d isc-dhcp6-server defaults

  • Modify the new file “/etc/init.d/isc-dhcp6-server” to support IPv6 by:
    • adding the “-6” option everywhere the dhcpd process is called (3 times)
    • change all “dhcp.leases” with dhcp6.leases”
    • modify the CONFIG_FILE variable :
      CONFIG_FILE=/etc/dhcp/dhcpd6.conf
    • modifiy the DHCPID variable:
      DHCPDPID=/var/run/dhcp-server/dhcpd6.pid
  • The DHCP Deamon is controlled by the “apparmor”, so we need to authorize the DHCPv6 to create some files and access some others. Here are the lines to add to the “/etc/apparmor.d/usr.sbin.dhcpd” file:

...

network inet6 raw,

@{PROC}/[0-9]*/net/if_inet6 r,

/var/lib/dhcp/dhcpd6.leases* lrw,

/var/run/dhcp-server/dhcpd6.pid w,

...

Don’t forget to restart the “apparmor” process:

#/etc/init.d/apparmor restart

  • Your are now ready to start both servers !

#/etc/init.d/isc-dhcp-server start

#/etc/init.d/isc-dhcp6-server start

Happy playing with IPv6 !