CentOS 6.5 学习笔记 搭建DHCP服务器
DHCP 动态主机配置协议,使用 UDP 协议工作,自动分配 IP 地址给内网机器
DHCP 主要有 3 个端口, 其中 UDP67, UDP68, 分别作为 server 端和 client 端的服务端口.
客户端从服务器获取 IP 地址, 子网掩码, 网关, DNS 等.
注意:如果使用虚拟机练习,服务器和客户端都需要使用桥接方式连接到网络.
1 服务器配置
>>> 1.1 安装 DHCP a服务
[root@ ~]#: yum install dhcp* -y
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base | 3.7 kB 00:00
...
Complete!
# 安装完成
>>> 1.2 修改配置文件
[root@ ~]#: vi /etc/dhcp/dhcpd.conf # 配置如下
1 # DHCP Server Configuration file.
2 # see /usr/share/doc/dhcp*/dhcpd.conf.sample
3 # see 'man 5 dhcpd.conf'
4 #
5
6 ddns-update-style interim;
7 ignore client-updates;
8 allow booting;
9 allow bootp;
10
11 subnet 192.168.10.0 netmask 255.255.255.0 { # 生效范围
12 # --- default gateway
13 option routers 192.168.10.1;
14 option subnet-mask 255.255.255.0;
15
16 # option nis-domain "domain.org";
17 # option domain-name "192.168.10.61";
18 # option domain-name-servers "192.168.10.61";
19
20 option time-offset -18000; # Eastern Standard Time
21
22 # option ntp-servers 192.168.10.61;
23 # option netbios-name-servers 192.168.10.61;
24 #--- Selects point-to-point node (default is hybrid). Don't hange this unless
25 #-- you understand Netbios very well
26 # option netbios-node-type 2;
27
28 range dynamic-bootp 192.168.10.70 192.168.10.80; # IP 地址池 10.70~80
29
30 host pc1 {
31 hardware ethernet 00:1a:a0:2b:38:62;
32 fixed-address 192.168.10.72;} # 保留IP,他机不可用
33
34 host pc2 {
35 hardware ethernet 00:1a:a0:2b:38:63;
36 fixed-address 192.168.10.73;} # 保留IP,他机不可用
37 }
# 保存退出
>>> 1.3 关闭防火墙 iptables 和 selinux
由于刚开始学习,对 iptables 不熟悉,所以先关闭防火墙和 selinux
> 1.3.1 关闭 iptables
[root@ ~]#: /etc/init.d/iptables status # 查看 iptables 状态
表格:filter
Chain INPUT (policy ACCEPT) # 正在运行
...
方法1: [root@ ~]#: /etc/init.d/iptables stop # 临时关闭,重启失效
方法2: [root@ ~]#: chkconfig iptables off # 永久关闭,重启后生效. 如需重新开启 chkconfig iptables on
[root@ ~]#: reboot 重启
[root@ ~]#: /etc/init.d/iptables status
iptables:未运行防火墙。
> 1.3.2 关闭 selinux
[root@ ~]#: /usr/sbin/sestatus # 查看 selinux 状态
SELinux status: enabled # 正在运行
SELinuxfs mount: /selinux
...
方法1: [root@ ~]#: setenforce 0 # 临时关闭,重启失效
方法2: [root@ ~]#: vi /etc/selinux/config # 修改配置,永久关闭,重启后生效
2 # This file controls the state of SELinux on the system.
....
7 SELINUX=disabled # 改 enforcing 为 disabled
...
# 保存退出.
[root@ ~]#: init 6 # 重启
[root@ ~]#: /usr/sbin/sestatus
SELinux status: disabled # 已关闭
>>> 1.4 启动 DHCP 服务
[root@ ~]#: /etc/init.d/dhcpd start
正在启动 dhcpd: [确定]
2 客户端配置
>>> 2.1 修改网络配置为 DHCP 方式获得 IP
[root@ ~]#: vi /etc/sysconfig/network-scripts/ifcfg-eth0 # 修改配置
1 DEVICE=eth0 # 网卡名字
2 HWADDR=00:0C:29:BF:19:C0 # 网卡物理地址
3 TYPE=Ethernet # 网卡类型:百兆
4 UUID=d51c16f4-a4d0-4e02-b09a-18bb52bd08fc # 唯一识别号
5 ONBOOT=yes # 开机启动网卡:yes
6 NM_CONTROLLED=yes # 实时启用配置:yes
7 BOOTPROTO=dhcp # 获取IP方式 DHCP
#保存退出
[root@ ~]#: /etc/init.d/network restart # 重启网络服务
Shutting down interface eth0: [ OK ]
...
Determining IP information for eth0... done. # 已重启
>>> 2.2 查看 IP 地址
[root@ ~]#: ifconfig
eth0 Link encap:Ethernet HWaddr 00:1a:a0:2b:38:62
inet addr:192.168.10.72 Bcast:192.168.10.255 Mask:255.255.255.0
...
新的 IP 地址是:192.168.10.72,不是之前的 10.62, 说明客户端可以正常从服务器获取 IP.
这个 IP 即是之前在服务器配置中, IP 地址池里面的保留 IP.