linux 操作系统下dhcpd命令介绍和案例应用
DHCP(动态主机配置协议)在Linux操作系统中用于自动为网络中的设备分配IP地址和其他网络配置
DHCP的基本概念
DHCP协议通过UDP工作,主要有两个用途:
- 自动分配IP地址给网络中的设备。
- 提供中央管理手段,便于网络管理员管理所有计算机的网络配置。
使用DHCP可以减少手动配置的工作量,避免因用户错误配置而导致的IP地址冲突等问题
dhcpd
命令介绍
在Linux中,dhcpd
是DHCP服务器的守护进程。其主要职责是监听客户端请求,并根据配置文件的设定分配IP地址。配置文件通常位于/etc/dhcp/dhcpd.conf
,该文件定义了网络参数、地址池等信息。
安装部署
root@meng:~# which dhcpd
root@meng:~# which dhcp
root@meng:~# apt-get install dhcp-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package dhcp-server
root@meng:~# apt-get install dhcp-server^C
root@meng:~# apt-get install isc-dhcp-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
cups-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libevent-2.1-7
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
libirs-export161 libisccfg-export163
Suggested packages:
isc-dhcp-server-ldap policycoreutils
The following NEW packages will be installed:
isc-dhcp-server libirs-export161 libisccfg-export163
0 upgraded, 3 newly installed, 0 to remove and 226 not upgraded.
Need to get 529 kB of archives.
After this operation, 1546 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://cn.archive.ubuntu.com/ubuntu jammy/main amd64 libisccfg-export163 amd64 1:9.11.19+dfsg-2.1ubuntu3 [53.0 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu jammy/main amd64 libirs-export161 amd64 1:9.11.19+dfsg-2.1ubuntu3 [20.0 kB]
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 isc-dhcp-server amd64 4.4.1-2.3ubuntu2.4 [456 kB]
Fetched 529 kB in 10s (52.0 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libisccfg-export163.
(Reading database ... 75251 files and directories currently installed.)
Preparing to unpack .../libisccfg-export163_1%3a9.11.19+dfsg-2.1ubuntu3_amd64.deb ...
Unpacking libisccfg-export163 (1:9.11.19+dfsg-2.1ubuntu3) ...
Selecting previously unselected package libirs-export161.
Preparing to unpack .../libirs-export161_1%3a9.11.19+dfsg-2.1ubuntu3_amd64.deb ...
Unpacking libirs-export161 (1:9.11.19+dfsg-2.1ubuntu3) ...
Selecting previously unselected package isc-dhcp-server.
Preparing to unpack .../isc-dhcp-server_4.4.1-2.3ubuntu2.4_amd64.deb ...
Unpacking isc-dhcp-server (4.4.1-2.3ubuntu2.4) ...
Setting up libisccfg-export163 (1:9.11.19+dfsg-2.1ubuntu3) ...
Setting up libirs-export161 (1:9.11.19+dfsg-2.1ubuntu3) ...
Setting up isc-dhcp-server (4.4.1-2.3ubuntu2.4) ...
Generating /etc/default/isc-dhcp-server...
Created symlink /etc/systemd/system/multi-user.target.wants/isc-dhcp-server.service → /lib/systemd/system/isc-dhcp-server.service.
Created symlink /etc/systemd/system/multi-user.target.wants/isc-dhcp-server6.service → /lib/systemd/system/isc-dhcp-server6.service.
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...
查看配置文件:
cat /etc/default/isc-dhcp-server
以下是一个简单的/etc/dhcp/dhcpd.conf
配置示例:
ddns-update-style interim;
ignore client-updates;
subnet 192.168.10.0 netmask 255.255.255.0 {
option routers 192.168.10.1;
option subnet-mask 255.255.255.0;
option time-offset -18000; # Eastern Standard Time
range dynamic-bootp 192.168.10.200 192.168.10.230;
default-lease-time 21600; # 6 hours
max-lease-time 43200; # 12 hours
}
在这个配置中:
subnet
定义了一个子网及其掩码。option routers
指定了默认网关。range
定义了可分配的IP地址范围。default-lease-time
和max-lease-time
设置了租约时间。