Linux network in-depth DHCP, FTP principle and configuration method (detailed diagram)

1. DHCP service

1. Understand the DHCP service

  • DHCP (Dynamic Host Configuration Protocol, dynamic host configuration protocol)
  • Designed and developed by the Internet Task Force
  • A protocol dedicated to self-distribution of ТСР/IP parameters for computers in a TCP/IP network

2. Benefits of using DHCP

  • Reduce the workload of administrators
  • Avoid the possibility of input errors
  • Avoid IP address conflicts
  • When changing the IP address segment, there is no need to reconfigure each user's IP address
  • Improved utilization of IP addresses
  • Convenient client configuration

3. DHCP allocation method

  • Automatic allocation: the client is permanently used after being allocated an IP address from the DHCP server
  • Manual allocation: the IP address is specified by the DHCP server administrator
  • Dynamic allocation: release the IP after the client is used up, for other clients to use

4. DHCP lease process

■The process by which the client obtains an IP address from the DHCP server is called the DHCP lease process

■Four steps
Insert picture description here

1. The client requests an IP address

  • When a DHCP client is started, the client does not yet have an IP address, so the client must obtain a legal address through DHCP.
  • At this time, the DHCP client broadcasts the DHCP Discover information to find the DHCP server

2. Server response

  • When the DHCP server receives the information requesting the IP address from the client, it searches its own IP address pool to find out whether there is a legal IP address provided to the client.
  • If so, the DHCP server will mark the IP address, add it to the DHCP Offer message, and then broadcast a DHCP Offer message

3. The client selects the IP address

  • The DHCP client extracts the IP address from the first DHCP Offer message it receives, and the DHCP server that issued the IP address reserves the address so that the address can no longer be assigned to another DHCP client.

4. The server determines the lease

  • After receiving the DHCP Request message, the DHCP server broadcasts a successful confirmation to the client in the form of a DHCP ACK message, which contains a valid lease of the IP address and other configurable information
  • When the client receives the DHCP ACK message, it configures the IP address and completes the initialization of TCP/IP

5. Log in again

  • Each time the DHCP client logs on to the network again, it does not need to send DHCP Discover information, but directly sends the DHCP Request request information containing the IP address assigned the previous time.

6. Renew the lease

  • When the lease period of the IP address leased by the DHCP server to the client reaches 50%, the lease must be renewed
  • The client directly sends a DHCP Request packet to the server that provides the lease, requesting to renew the existing address lease

5. The address information that can be allocated mainly includes

  • The IP address and subnet mask of the network card
  • Corresponding network address, broadcast address
  • Default gateway address
  • DNS server address

Two, install the DHCP server

1. Configuration in ensp

dhcp enable									#开启DHCP功能

interface Vlanif10
 ip address 192.168.1.254 255.255.255.0
 dhcp select relay							#开启DHCP中继功能
 dhcp relay server-ip 192.168.100.20			#指向DHCP服务器的地址

interface Vlanif20
 ip address 192.168.2.254 255.255.255.0
 dhcp select relay
 dhcp relay server-ip 192.168.100.20

interface Vlanif100
 ip address 192.168.80.254 255.255.255.0
 dhcp select relay
 dhcp relay server-ip 192.168.100.20

2. Configure the DHCP server

■DHCP server software

  • Dhcp-4.2.5-58.el7.centos.x86_64.rpm in the CentOS 7 CD
  • The main files of the DHCP software package
    • Main configuration file: /etc//dhcp/dhcpd.conf
    • Execution program: /usr/sbin/dhcpd, /usr/sbin/dhcrelay
配置DHCP服务器
yum install -y dhcp

cd /etc/dhcp/
ls
less dhcpd.conf

cd /usr/share/doc/dhcp-4.2.5/
ls
less dhcpd.conf.example

cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

vim /etc/dhcp/dhcpd.conf
#设置全局配置参数
default-lease-time 600; 									#默认租约为 10 分钟,单位为秒
max-lease-time 7200; 										#最大租约为 2 小时,单位为秒
option domain-name "benet.com"; 							#指定默认域名
option domain-name-servers 202.106.0.20, 202.106.148.1; 	#指定 DNS 服务器地址
ddns-update-style none; 									#禁用 DNS 动态更新

#subnet网段声明(作用于整个子网段,部分配置参数优先级高于全局配置参数)
subnet 192.168.80.0 netmask 255.255.255.0 {
    
    			        #声明要分配的网段地址
  range 192.168.80.10 192.168.80.20;				        #设置地址池
  option routers 192.168.80.254;					        #指定默认网关地址
}

subnet 192.168.1.0 netmask 255.255.255.0 {
    
    
  range 192.168.1.100 192.168.1.200;
  option routers 192.168.1.254;
}

subnet 192.168.2.0 netmask 255.255.255.0 {
    
    
  range 192.168.2.20 192.168.2.30;
  option routers 192.168.2.254;
}

#后面内容可都删除

systemctl start dhcpd
systemctl stop firewalld
setenforce 0

netstat -anpu | grep ":67"

#如果DHCP服务启动失败,可以查看日志文件
tail -f /var/log/messages

3. The Linux client uses DHCP to dynamically obtain an IP

#方法一:
vim /etc/sysconfig/network-scripts/ifcfg-ens33
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=dhcp

ifdown ens33 ; ifup ens33

#方法二:
dhclient -d ens33

查看租约文件 
less /var/lib/dhcpd/dhcpd.lease

Three, DHCP experiment example

Insert picture description here
1. Cloud1 settings in
Insert picture description here
ensp 2.sw1 configuration
Insert picture description here
3.sw2 configuration
Insert picture description here

4. Modify the virtual machine network type
Insert picture description here
5. Modify the virtual network editor
Insert picture description here
6. Modify the VMnet1 network card
Insert picture description here
Insert picture description here
7. Configure the network card IP
Insert picture description here
Insert picture description here
8. Install dhcp, view and find the configuration file.
Insert picture description here
This file has a template file in /usr/share/doc/dhcp- 4.2.5 The dhcpd.conf.example file in this directory, we can copy the file through cp
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
and get it by pc2
Insert picture description here

Assign a fixed IP address to the PC

We first need to find out the physical network card of the win10 virtual machine
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Four, FTP file transfer experiment

■FTP service-protocol used to transfer files

■FTP server uses TCP protocol port 20 and 21 to communicate with the client by default

  • Port 20 is used to establish a data connection and transfer file data
  • Port 21 is used to establish a control connection and transmit FTP control commands

■FTP data connection is divided into active mode and passive mode

  • Active mode: the server actively initiates a data connection
  • Passive mode: The server passively waits for a data connection

1. Install FTP and backup files

yum install -y vsftpd
cd /etc/vsftpd/
cp vsftpd.conf vsftpd.conf.bak

Insert picture description here
Insert picture description here

2. Set up the FTP service accessed by anonymous users (maximum permissions)

#修改配置文件
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES            #开启匿名用户访问。默认已开启
write_enable=YES                #开放服务器的写权限(若要上传,必须开启)。默认已开启
anon_umask=022                  #设置匿名用户所上传数据的权限掩码(反掩码)。默认已开启
anon_upload_enable=YES          #允许匿名用户上传文件。默认已注释,需取消注释
anon_mkdir_write_enable=YES     #允许匿名用户创建(上传)目录。默认已注释,需取消注释
anon_other_write_enable =YES    #允许删除、重命名、覆盖等操作。需添加

#为匿名访问ftp的根目录下的 pub子目录设置最大权限,以便匿名用户上传数据
chmod 777 /var/ftp/pub/

#开启服务,关闭防火墙和增强型安全功能
systemctl start vsftpd
systemctl stop firewalld
setenforce 0

Insert picture description here
Insert picture description here

3. Anonymous access test

在Windows系统打开 开始 菜单,输入 cmd 命令打开命令提示符
#建立ftp连接
ftp 192.168.163.20
#匿名访问,用户名为ftp,密码为空,直接回车即可完成登录
ftp> pwd			#匿名访问ftp的根目录为Linux系统的/var/ftp/目录
ftp> ls				#查看当前目录
ftp> cd pub			#切换到pub 目录
ftp> get 文件名		#下载文件到当前Windows本地目录
ftp> put 文件名		#上传文件到ftp目录
ftp> quit			#退出

Insert picture description here
Let's switch to Linux to see if there is this file
Insert picture description here
Insert picture description here
Insert picture description here

4. Set up local user authentication to access ftp, and prohibit switching to directories other than ftp (the root directory of the default login is the home directory of the local user)

#修改配置文件
vim /etc/vsftpd/vsftpd.conf
local_enable=Yes        		 #启用本地用户
anonymous_enable=NO     		 #关闭匿名用户访问
write_enable=YES        		 #开放服务器的写权限(若要上传,必须开启)
anon_umask=077          		 #可设置仅宿主用户拥有被上传的文件的权限(反掩码)
chroot_local_user=YES   		 #将访问禁锢在用户的宿主目录中
allow_writeable_chroot=YES		 #允许被限制的用户主目录具有写权限

重启服务
systemctl restart vsftpd

ftp 192.168.80.10
或
ftp://[email protected]
修改匿名用户、本地用户登录的默认根目录(这个在配置文件里填上即可,这就不演示了)
anon_root=/var/www/html			#anon_root 针对匿名用户
local_root=/var/www/html		#local_root 针对系统用户

使用user_list 用户列表文件
vim /etc/vsftpd/user_list
//末尾添加zhangsan用户
zhangsan

vim /etc/vsftpd/vsftpd.conf
userlist_enable=YES    #启用user_list用户列表文件
userlist_deny=NO       #设置白名单,仅对user_list用户列表文件的用户访问。默认为YES,为黑名单,禁用

Modify the configuration file
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5. Use user_list user list file to restrict user login

Insert picture description here

Insert picture description here
Insert picture description here
Here we try to connect to the ftp service in win10
Insert picture description here

Guess you like

Origin blog.csdn.net/IHBOS/article/details/113871671