WHCSRL 技术网

Linux-DHCP服务器搭建过程

 

目录

一、DHCP服务器搭建

二、HDCP中固定地址分发

三、DHCP服务的超级作用域

四、HDCP中继功能


  搭建环境介绍

    虚拟机中仅主机模式下,同时关闭虚拟机DHCP网卡服务

    关闭防火墙和SElinux情况

    命令如下:

    关闭防火墙:chkconfig iptables off

    关闭SELinux:vim /etc/sysconfig/selinux 修改配置文件的中的为SELINUX=disabled

一、DHCP服务器搭建

  1. # This file controls the state of SELinux on the system.
  2. # SELINUX= can take one of these three values:
  3. # enforcing - SELinux security policy is enforced.
  4. # permissive - SELinux prints warnings instead of enforcing.
  5. # disabled - No SELinux policy is loaded.
  6. SELINUX=disabled

 安装hdcp服务器 

yum -y install dhcp

 修改配置文件(dhcpd.conf

1.找到配置文件模板进行替换。

  1. [root@CetnOS6-02 ~]# cp -a /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
  2. cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y

 2.修改配置文件里面的内容

[root@CetnOS6-02 ~]# vim /etc/dhcp/dhcpd.conf 

 将配置文件的其他subnet声明注释掉。

  1. subnet 192.168.10.0 netmask 255.255.255.224 {
  2. range 192.168.10.100 192.168.10.110;
  3. # option domain-name-servers ns1.internal.example.org;
  4. # option domain-name "internal.example.org";
  5. # option routers 10.5.5.1;
  6. # option broadcast-address 10.5.5.31;
  7. default-lease-time 600;
  8. max-lease-time 7200;
  9. }

模版详细解释:

subnet  192.168.88.0   netmask   255.255.255.0  {    #声明要分配的网段和子网掩码
range    192.168.88.3  192.168.88.254;                   #声明可用 IP 地址池
option  domain-name  "atguigu.com"  ;                       #设置 DNS 域
option  domain-name-servers  8.8.8.8  ;                     #设置 DNS 服务器地址
option  routers   192.168.88.2;                                 #默认网关的地址
option  broadcast-address  192.168.88.255;           #广播地址(可不写)
default-lease-time  600;                                           #默认租约(s)
max-lease-time  7200;                                             #最大租约(s)

}
      需要注意:“ {  }缺失、每行结尾;”  ,以及分配的网段和子网掩码必须和当前的网卡的网段保持一致,不一致服务无法启动,直接报错。

启动服务

  1. [root@CetnOS6-02 ~]# service dhcpd start
  2. 正在启动 dhcpd: [确定]
  3. [root@CetnOS6-02 ~]# netstat -tlun | grep 67
  4. udp 0 0 0.0.0.0:67 0.0.0.0:*
  5. #服务启动成功。

3.通过客户机验证HDCP服务器运行情况。

  1. [root@Cent0S-03 ~]# ifconfig
  2. eth0 Link encap:Ethernet HWaddr 00:0C:29:23:E8:75
  3. inet addr:192.168.10.100 Bcast:192.168.10.255 Mask:255.255.255.0
  4. inet6 addr: fe80::20c:29ff:fe23:e875/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:46643 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:3164 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:1000
  9. RX bytes:59141951 (56.4 MiB) TX bytes:214084 (209.0 KiB)
  10. lo Link encap:Local Loopback
  11. inet addr:127.0.0.1 Mask:255.0.0.0
  12. inet6 addr: ::1/128 Scope:Host
  13. UP LOOPBACK RUNNING MTU:65536 Metric:1
  14. RX packets:4 errors:0 dropped:0 overruns:0 frame:0
  15. TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
  16. collisions:0 txqueuelen:0
  17. RX bytes:276 (276.0 b) TX bytes:276 (276.0 b)

4.读取日志文件查看验证HDCP运行

  1. [root@CetnOS6-02 ~]# tail -10 /var/log/messages
  2. Oct 30 10:59:17 CetnOS6-02 dhcpd: DHCPDISCOVER from 00:0c:29:23:e8:75 via eth0
  3. Oct 30 10:59:18 CetnOS6-02 dhcpd: ns1.example.org: temporary name server failure
  4. Oct 30 10:59:18 CetnOS6-02 dhcpd: ns2.example.org: temporary name server failure
  5. Oct 30 10:59:18 CetnOS6-02 dhcpd: DHCPOFFER on 192.168.10.100 to 00:0c:29:23:e8:75 (Cent0S-03) via eth0
  6. Oct 30 10:59:18 CetnOS6-02 dhcpd: Wrote 0 class decls to leases file.
  7. Oct 30 10:59:18 CetnOS6-02 dhcpd: Wrote 0 deleted host decls to leases file.
  8. Oct 30 10:59:18 CetnOS6-02 dhcpd: Wrote 0 new dynamic host decls to leases file.
  9. Oct 30 10:59:18 CetnOS6-02 dhcpd: Wrote 1 leases to leases file.
  10. Oct 30 10:59:18 CetnOS6-02 dhcpd: DHCPREQUEST for 192.168.10.100 (192.168.10.20) from 00:0c:29:23:e8:75 (Cent0S-03) via eth0
  11. Oct 30 10:59:18 CetnOS6-02 dhcpd: DHCPACK on 192.168.10.100 to 00:0c:29:23:e8:75 (Cent0S-03) via eth0

二、HDCP中固定地址分发

1.获取客户机的mac地址

  1. [root@CentOS-04 ~]# ifconfig
  2. eth0 Link encap:Ethernet HWaddr 00:0C:29:4D:28:4F
  3. inet addr:192.168.10.101 Bcast:192.168.10.255 Mask:255.255.255.0
  4. inet6 addr: fe80::20c:29ff:fe4d:284f/64 Scope:Link

2.修改服务器的配置文件给客户机指定IP地址

[root@CetnOS6-02 ~]# vim /etc/dhcp/dhcpd.conf 
  1. host fantasia {
  2. hardware ethernet 00:0C:29:4D:28:4F;
  3. #客户机的mac地址
  4. fixed-address 192.168.10.110;
  5. #绑定分发的IP地址
  6. }

重启HDCP服务,否则无法进行分发IP地址。

  1. [root@CetnOS6-02 ~]# service dhcpd restart
  2. 关闭 dhcpd: [确定]
  3. 正在启动 dhcpd: [确定]

 通过查看日志验证发送成功。

  1. Oct 30 11:23:01 CetnOS6-02 dhcpd: DHCPREQUEST for 192.168.10.110 from 00:0c:29:4d:28:4f via eth0
  2. Oct 30 11:23:01 CetnOS6-02 dhcpd: ns1.example.org: temporary name server failure
  3. Oct 30 11:23:01 CetnOS6-02 dhcpd: ns2.example.org: temporary name server failure
  4. Oct 30 11:23:01 CetnOS6-02 dhcpd: DHCPACK on 192.168.10.110 to 00:0c:29:4d:28:4f via eth0

三、DHCP服务的超级作用域

1. 同一个网卡增加第二个网段。

  1. [root@CetnOS6-02 ~]# cd /etc/sysconfig/network-scripts/
  2. [root@CetnOS6-02 network-scripts]# cp -a ifcfg-eth0 ifcfg-eth0:0
  3. #给同一张网卡增加IP地址。
  4. [root@CetnOS6-02 network-scripts]# vim ifcfg-eth0:0
  5. #修改配置文件
  6. IPADDR=192.168.99.20
  7. #因为是同一个网卡,只需要更改的网卡IP
  8. [root@CetnOS6-02 network-scripts]# ifup eth0:0
  9. #启动网卡,并查看运行结果
  10. Determining if ip address 192.168.99.20 is already in use for device eth0...
  11. [root@CetnOS6-02 network-scripts]# ifconfig
  12. eth0 Link encap:Ethernet HWaddr 00:0C:29:E7:DA:51
  13. inet addr:192.168.10.20 Bcast:192.168.10.255 Mask:255.255.255.

2.修改配置文件,启动转发功能并且刷新配置。 

  1. [root@CetnOS6-02 ~]# vim /etc/sysctl.conf
  2. # Controls IP packet forwarding
  3. net.ipv4.ip_forward = 1
  4. # 启动转发只需修改成1。
  5. [root@CetnOS6-02 ~]# sysctl -p #刷新配置文件
  6. net.ipv4.ip_forward = 1
  7. net.ipv4.conf.default.rp_filter = 1
  8. net.ipv4.conf.default.accept_source_route = 0
  9. kernel.sysrq = 0
  10. kernel.core_uses_pid = 1
  11. net.ipv4.tcp_syncookies = 1
  12. kernel.msgmnb = 65536
  13. kernel.msgmax = 65536
  14. kernel.shmmax = 68719476736
  15. kernel.shmall = 4294967296

 3.修改文件:vim /etc/dhcp/dhcpd.conf 

  1. #文件里面的其他相关配置视情况不用的可以全部注释掉。
  2. shared-network 10-99 {
  3. #网卡的IP地址号码段
  4. subnet 192.168.10.0 netmask 255.255.255.0 {
  5. option routers 192.168.10.20;
  6. range 192.168.10.21 192.168.10.21;
  7. }
  8. subnet 192.168.99.0 netmask 255.255.255.0 {
  9. option routers 192.168.99.20;
  10. range 192.168.99.110 192.168.99.115;
  11. }
  12. }
  13. 重新启动服务
  14. [root@CetnOS6-02 ~]# service dhcpd restart
  15. 关闭 dhcpd: [确定]
  16. 正在启动 dhcpd: [确定]

四、HDCP中继功能

服务器网段设置:

DHCP服务器:   eth0(192.168.10.20)   VMnet10

DHCP中继:       eth0(192.168.10.30)   VMnet10

                            eth1(100.100.100.30) VMnet11

外网客户机:      eth0(IP地址自动获取)  VMnet11

1、HDCP服务器设置网关

 修改网卡配置文件eth0,向文件里面添加网关信息:GATEWAY=192.168.10.30

2、修改配置dhcpd配置文件

  1. subnet 192.168.10.0 netmask 255.255.255.0 {
  2. range 192.168.10.30 192.168.10.35;
  3. option routers 192.168.10.30;
  4. }
  5. #实验中并未用到该地址池分配IP,但是DHCP服务启动需要地址。
  6. subnet 100.100.100.0 netmask 255.255.255.0 {
  7. range 100.100.100.111 100.100.100.112;
  8. option routers 100.100.10.30;
  9. }
  10. #声明两个subnet,其他无关可以不做操作或删除

3.修改中继服务器网卡

  1. [root@Cent0S-03 network-scripts]# cp ifcfg-eth0 ifcfg-eth1
  2. #建立eth1网卡配置文件
  1. DEVICE=eth1
  2. #这里需要修改名字为当前网卡名
  3. TYPE=Ethernet
  4. ONBOOT=yes
  5. NM_CONTROLLED=yes
  6. BOOTPROTO=none
  7. USERCTL=no
  8. PEERDNS=yes
  9. IPV6INIT=no
  10. IPADDR=100.100.100.30
  11. #修改ip地址
  12. NETMASK=255.255.255.0

 重新启动网卡

  1. [root@Cent0S-03 network-scripts]# service network restart
  2. 正在关闭接口 eth0: [确定]
  3. 关闭环回接口: [确定]
  4. 弹出环回接口: [确定]
  5. 弹出界面 eth0: Determining if ip address 192.168.10.30 is already in use for device eth0...
  6. [确定]
  7. 弹出界面 eth1: Determining if ip address 100.100.100.30 is already in use for device eth1...
  8. [确定]

4.修改配置文件:vim /etc/sysconfig/dhcrelay

  1. INTERFACES="eth0 eth1"
  2. #添加的网卡名称
  3. DHCPSERVERS="192.168.10.20"
  4. #添加HDCP服务的ip地址,不是中继器的IP。

5.开启路由转发

修改配置文件:vim /etc/sysctl.conf  里面的为 net.ipv4.ip_forward = 1

然后刷新配置:sysctl   -p

6.启动中继服务:service dhcrelay start

7.测试外网主机。

  1. Oct 30 13:13:57 CetnOS6-02 dhcpd: DHCPDISCOVER from 00:0c:29:4d:28:4f via 100.100.100.30
  2. Oct 30 13:13:58 CetnOS6-02 dhcpd: ns1.example.org: temporary name server failure
  3. Oct 30 13:14:03 CetnOS6-02 dhcpd: ns2.example.org: temporary name server failure
  4. Oct 30 13:14:03 CetnOS6-02 dhcpd: DHCPOFFER on 100.100.100.111 to 00:0c:29:4d:28:4f (CentOS-04) via 100.100.100.30
  5. Oct 30 13:14:03 CetnOS6-02 dhcpd: DHCPDISCOVER from 00:0c:29:4d:28:4f (CentOS-04) via 100.100.100.30
  6. Oct 30 13:14:03 CetnOS6-02 dhcpd: DHCPOFFER on 100.100.100.111 to 00:0c:29:4d:28:4f (CentOS-04) via 100.100.100.30
  7. Oct 30 13:14:03 CetnOS6-02 dhcpd: DHCPREQUEST for 100.100.100.111 (192.168.10.20) from 00:0c:29:4d:28:4f (CentOS-04) via 100.100.100.30
  8. Oct 30 13:14:03 CetnOS6-02 dhcpd: DHCPACK on 100.100.100.111 to 00:0c:29:4d:28:4f (CentOS-04) via 100.100.100.30

推荐阅读