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
Configuring DHCPv6
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 !