网络配置
静态指定
DHCP: ( Dynamic Host Configuration Protocol)
DHCP工作原理:基于广播形式。
主要用途:
使用场景
DHCP的八种报文
DHCP服务简介
同网段多DHCP服务
跨网段
路由器中的接口可以启动中继代理的功能。
相关协议:Arp /rarp
Linux DHCP协议的实现程序: dhcp,或者dnsmasq软件都可以充当dhcp服务器( dhcp,dns)
Dhcp Server
/usr/sbin/dhcpd 存放dhcp发放IP等数据库信息
/etc/dhcp/dhcpd.conf --> /etc/rc.d/init.d/dhcpd
/etc/dhcp/dhcpd6.conf--> /etc/rc.d/init.d/dhcpd6
/usr/sbin/dhcrelay
/etc/rc.d/init.d/dhcrelay
dhcp server:67/udp
dhcp client: 68/udp
dhcpv6 client:546/udp
DHCP客户端client
dhclient
自动获取的IP信息: /var/lib/dhclient 系统中自身就有
dhcp的dhclient 客户端需要自动获取IP地址才会启动dhcp服务;也可以手动触发dhclient 客户端来生成自动获取IP
[root@localhost ~]# dhclient
[root@localhost ~]# ip a
dhclient -d 跟踪查看dhclinet 执行的过程
killall dhclient 可以看到信息。
查看客户端主机从哪个dhcp服务器中获取地址和有关信息在 /var/lib/dhclient目录中
[root@localhost network-scripts]# cd /var/lib/dhclient/
[root@localhost dhclient]# ls
dhclient.leases
[root@localhost dhclient]# cat dhclient.leases
DHCP服务器 服务端 server
[root@centos7 ~]# yum info dhcp 查看dhcp说明
[root@centos7 ~]# yum install dhcp 安装dhcp服务包
[root@centos7 ~]# rpm -ql dhcp 查看包的文件列表
/usr/lib/systemd/system/dhcpd.service 服务
/etc/dhcp/dhcpd.conf 配置文件 默认为空
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example 配置范例
/usr/sbin/dhcpd 工作目录
注意:dhcp在空配置文件的情况下启动服务是起不来的 systemctl start dhcpd
拷贝范例文件来覆盖 .conf的配置文件
[root@centos7 ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@centos7~]# vim ifcfg-eth0
BOOTPROTO=dhcp
[root@centos7~]# systemctl restart network.service
虚拟主机网卡设置:
当dhcp服务器给别人分配地址的同时,必须先给dhcp服务器本身的地址网段分配地址,然后在给另外网段的主机分配IP地址。
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
option domain-name "example.org"; 原本域名
option domain-name "zxbblog.org"; 可以任意修改
option domain-name-servers ns1.example.org, ns2.example.org; DNS服务
option domain-name-servers 114.114.114.144,1.1.1.1; 建议修改
default-lease-time 600; 默认租期时长,时长根据环境来定
max-lease-time 7200; 最大租期
修改配置文件中的一下内容;
修改配置文件之前
# This is a very basic subnet declaration.
subnet 10.254.239.0 netmask 255.255.255.224 {
range 10.254.239.10 10.254.239.20;
option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
修改配置文件后
cp /etc/dhcp/dhcpd.conf /data/ ##修改配置前先备份原来的配置文件
# DHCP server to understand the network topology.
subent 192.168.161.0 netmask 255.255.255.0 {
ranage 192.168.161.10 192.168.161.100; 地址池
option routers 192.168.161.254; 网关
}
先定义本网段地址:
subnet 192.168.161.0 netmask 255.255.255.0 {
}
[root@localhost dhcp]# systemctl restart dhcpd ##重启服务
[root@localhost dhcp]# systemctl restart dhcpd
[root@localhost dhcp]# systemctl start dhcpd
[root@localhost dhcp]# systemctl status dhcpd
Active: active (running)
[root@localhost dhcp]# vim dhcpd.conf
subent 192.168.161.0 netmask 255.255.255.0 {
ranage 192.168.161.10 192.168.161.100; 地址池
option routers 192.168.161.254; 网关
}
[root@localhost dhcp]# systemctl restart dhcpd
客户端配置:
[[email protected]]#cd /etc/sysconfig/network-scripts
[[email protected]]#vim ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.161.128
PREFIX=24
BOOTPROTO=dhcp
#HWADDR=00:0C:29:75:B6:98
#TYPE=Ethernet
#UUID=a9cb5202-a938-492c-ad69-638e21cd6ac5
ONBOOT=yes
#NM_CONTROLLED=yes
[[email protected]]#service network restart
[[email protected]]#ip a
inet 192.168.161.10/24
查看客户端获取地址过程的详细信息:
[root@localhost ~]# cd /var/lib/dhclient/
[root@localhost ~]# ls
[root@localhost dhclient]# cat dhclient-5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03-eth0.lease
lease {
interface "eth0";
fixed-address 192.168.161.10;
option subnet-mask 255.255.255.0;
option routers 192.168.161.253;
option dhcp-lease-time 60000;
option dhcp-message-type 5;
option domain-name-servers 114.114.114.114,1.1.1.1;
option dhcp-server-identifier 192.168.161.131;
option domain-name "magedu.org";
renew 6 2018/09/22 13:53:13;
rebind 6 2018/09/22 20:35:24;
expire 6 2018/09/22 22:40:24;
}
DHCP如何做到给指定的主机分配指定IP地址
方法如下:
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
要修改的内容
host fantasia {
hardware ethernet 0f:00:07:58:45;
fixed-192.168.161.100;
}
[root@localhost ~]# service network restart
[root@localhost ~]# ip a
eth0:inet 192.168.161.100/24