实现DHCP服务和TFTP服务

DHCP:

 实现DHCP服务前,先将网络已有DHCP服务,如:vmware中的DHCP关闭,防止冲突

[root@cent8_yzil ~]# yum install -y dhcp-server

[root@cent8_yzil ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)

[root@cent8_yzil ~]# systemctl enable --now dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code.
See "systemctl status dhcpd.service" and "journalctl -xe" for details.

#服务起不来,查看一下配置文件
[root@cent8_yzil ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp-server/dhcpd.conf.example    #dhcp服务配置范例文件
#   see dhcpd.conf(5) man page
#

[root@cent8_yzil ~]# cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
cp: overwrite '/etc/dhcp/dhcpd.conf'? yes
[root@cent8_yzil ~]# 
#有了配置文件,服务还是起不来,说明配置还是不符合要求
[root@cent8_yzil ~]# systemctl enable --now dhcpd
Job for dhcpd.service failed because the control process exited with error code.
See "systemctl status dhcpd.service" and "journalctl -xe" for details.
#查看错误日志,没有对应的子网定义
[root@cent8_yzil ~]# cat /var/log/messages
Apr 28 22:05:17 cent8_yzil dhcpd[37755]: No subnet declaration for ens33 (10.0.0.133).

[root@cent8_yzil ~]# vi /etc/dhcp/dhcpd.conf 
subnet 10.0.0.0 netmask 255.255.255.0 {
}

[root@cent8_yzil ~]# systemctl enable --now dhcpd
[root@cent8_yzil ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-04-28 22:12:50 EDT; 40s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 37947 (dhcpd)
   Status: "Dispatching packets..."
    Tasks: 1 (limit: 11251)
   Memory: 5.5M
   CGroup: /system.slice/dhcpd.service
           └─37947 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
[root@cent8_yzil ~]# 

#监听端口udp67
[root@cent8_yzil ~]# ss -ntul
Netid        State         Recv-Q        Send-Q                Local Address:Port                Peer Address:Port       
udp          UNCONN        0             0                     192.168.122.1:53                       0.0.0.0:*          
udp          UNCONN        0             0                           0.0.0.0:67                       0.0.0.0:*    
[root@cent8_yzil ~]# 
#修改配置文件
[root@cent8_yzil ~]# vi /etc/dhcp/dhcpd.conf 
option domain-name-servers 180.76.76.76, 223.5.5.5;   #DNS

default-lease-time 86400;     #默认租期,修改为一天的时间,以秒为单位
max-lease-time 106400;        #最大周期
subnet 10.0.0.0 netmask 255.255.255.0 {
   range 10.0.0.150 10.0.0.160;    #分配的IP区间
   option routers 10.0.0.2;        #网关

}

[root@cent8_yzil ~]# systemctl restart dhcpd

测试: forever 自动获取
[root@cent7_yzil ~]# systemctl restart network
[root@cent7_yzil ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d3:72:51 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.150/24 brd 10.0.0.255 scope global dynamic ens33
       valid_lft 85893sec preferred_lft 85893sec
    inet6 fe80::7e7e:c681:465:5aa6/64 scope link 
       valid_lft forever preferred_lft forever
[root@cent7_yzil ~]# 

[root@y_zilong ~]# systemctl restart network
[root@y_zilong ~]# hostname  -I
10.0.0.151 
[root@y_zilong ~]# 

#客户端打开udp68端口
[root@cent7_yzil ~]# ss -ntul
udp   UNCONN     0      0                              *:68                                         *:*   
[root@cent7_yzil ~]#  
#可以从dhcp服务器上看到分配出去的IP地址
[root@cent8_yzil ~]# cat /var/lib/dhcpd/dhcpd.leases
authoring-byte-order little-endian;

lease 10.0.0.150 {
  starts 4 2021/04/29 02:31:21;
  ends 5 2021/04/30 02:31:21;
  tstp 5 2021/04/30 02:31:21;
  cltt 4 2021/04/29 02:31:21;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:d3:72:51;
  client-hostname "cent7_yzil";
}
lease 10.0.0.151 {
  starts 4 2021/04/29 02:32:00;
  ends 5 2021/04/30 02:32:00;
  tstp 5 2021/04/30 02:32:00;
  cltt 4 2021/04/29 02:32:00;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:0c:29:74:76:e8;
  client-hostname "y_zilong";
}
server-duid "\000\001\000\001(\034\322\242\000\014)M\207\005";

[root@cent8_yzil ~]# 

#查看客户端如何获取地址,通过广播自动获取
[root@cent7_yzil ~]# dhclient -d
DHCPDISCOVER on ens33 to 255.255.255.255 port 67 interval 8 (xid=0x7eed4116)
DHCPREQUEST on ens33 to 255.255.255.255 port 67 (xid=0x7eed4116)
[root@cent8_yzil ~]# vi /etc/dhcp/dhcpd.conf

option domain-name-servers 180.76.76.76, 223.5.5.5;   #DNS

default-lease-time 86400;     #默认租期,修改为一天的时间,以秒为单位
max-lease-time 106400;        #最大周期

subnet 10.0.0.0 netmask 255.255.255.0 {
   range 10.0.0.150 10.0.0.160;    #分配的IP区间
   option routers 10.0.0.2;        #网关
   next-server 10.0.0.133;         #dhcp服务器的IP地址,下载服务器
   filename "pxelinux.0";          #grub文件
}

指定固定IP地址,可以不在上面的分配IP区间之内,只要在10.0.0.0这个网段内的IP地址都可以
host testhost{
  hardware ethernet 00:0c:29:d3:72:51;
  fixed-address 10.0.0.123;
}


[root@cent7_yzil ~]# dhclient -d
DHCPACK from 10.0.0.133 (xid=0x71c8e586)
bound to 10.0.0.123 -- renewal in 36677 seconds.

[root@cent7_yzil ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d3:72:51 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.123/24 brd 10.0.0.255 scope global dynamic ens33
       valid_lft 86202sec preferred_lft 86202sec
    inet6 fe80::7e7e:c681:465:5aa6/64 scope link 
       valid_lft forever preferred_lft forever
[root@cent7_yzil ~]# 

TFTP:提供小文件的下载

[root@cent8_yzil ~]# yum -y install tftp-server

[root@cent8_yzil ~]# systemctl enable --now tftp

#监听69端口
[root@cent8_yzil ~]# ss -ntul
udp          UNCONN        0             0                                 *:69                             *:*   
[root@cent8_yzil ~]#

#tftp共享目录:/var/lib/tftpboot/
[root@cent8_yzil ~]# ls /var/lib/tftpboot/
[root@cent8_yzil ~]# cp /etc/centos-release /var/lib/tftpboot/
[root@cent8_yzil ~]# ll /var/lib/tftpboot/
total 4
-rw-r--r--. 1 root root 30 Apr 28 23:19 centos-release
[root@cent8_yzil ~]# mv /var/lib/tftpboot/centos-release /var/lib/tftpboot/ver.txt
[root@cent8_yzil ~]# 


#下载tftp客户端:
[root@cent7_yzil ~]# yum install tftp

[root@cent7_yzil ~]# tftp 10.0.0.133
tftp> get ver.txt

[root@cent7_yzil ~]# ls
ver.txt
[root@cent7_yzil ~]# ll ver.txt 
-rw-r--r--. 1 root root 0 Apr 28 20:22 ver.txt
[root@cent7_yzil ~]# 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

y_zilong

一分钱的肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值