搭建FTP服务器步骤
CentOS Linux 7.6 部署FTP教程
今天给大家介绍下在CentOS 7.6操作系统上部署FTP的过程,有需要的朋友参考。
墨天轮:https://www.modb.pro/u/15854
公众号:Hello DBA
一、前言
FTP是一个用于在网络中传输文件的协议,可用于文件传输、网站管理、文件备份和软件发布等多种用途。
二、安装前准备
1、环境介绍
类目 | 详情 | |
OS | CentOS Linux release 7.6 | |
HOSTNAME | hellodba | |
IP | 192.168.0.52 | |
CPU | 2C | |
Memory | 4G | |
Disk | 80G | / |
4G | /swap | |
30G | /FTP |
2、前期准备
2.1、挂载光驱
[root@hellodba ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
2.2、配置YUM源
[root@hellodba ~]# cd /etc/yum.repos.d
[root@hellodba yum.repos.d]# mkdir bak
[root@hellodba yum.repos.d]# mv *.repo bak
[root@hellodba yum.repos.d]# vi hellodba.repo
[EL]
name=Linux 7.6 DVD
baseurl=file:///mnt
gpgcheck=0
enabled=1
[root@hellodba yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: EL
Cleaning up list of fastest mirrors
2.3、关闭SELINUX
[root@hellodba ~]# vi /etc/selinux/config
//将
SELINUX=enforcing
//改为
SELINUX=disabled
[root@hellodba ~]# setenforce 0
[root@hellodba ~]# getenforce
Permissive
2.4、关闭防火墙
//查看防火墙状态
[root@hellodba ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-09-01 13:16:41 CST; 9s ago
Docs: man:firewalld(1)
Main PID: 77721 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
└─77721 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Sep 01 13:16:41 hellodba systemd[1]: Starting firewalld - dynamic firewall daemon...
Sep 01 13:16:41 hellodba systemd[1]: Started firewalld - dynamic firewall daemon.
//关闭防火墙
[root@hellodba ~]# systemctl stop firewalld
//禁止防火墙开机自动启动
[root@hellodba ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//查看防火墙状态
[root@hellodba ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
Sep 01 13:16:41 hellodba systemd[1]: Starting firewalld - dynamic firewall daemon...
Sep 01 13:16:41 hellodba systemd[1]: Started firewalld - dynamic firewall daemon.
Sep 01 13:17:06 hellodba systemd[1]: Stopping firewalld - dynamic firewall daemon...
Sep 01 13:17:07 hellodba systemd[1]: Stopped firewalld - dynamic firewall daemon.
3、添加用户并限制目录及指定用户访问指定目录
3.1、创建 FTP专用目录
//创建FTP用户访问目录
[root@hellodba ~]# mkdir -p /FTP/data/ceshi
[root@hellodba ~]# chmod -R 775 /FTP/data/ceshi
[root@hellodba ~]# ls -ld /FTP/data/ceshi/
drwxrwxr-x. 2 root root 6 Sep 2 17:44 /FTP/data/ceshi/
3.2、建立FTP用户并指定目录
[root@hellodba ~]# useradd -g root -M -d /FTP/data/ceshi -s /sbin/nologin ceshi
[root@hellodba ~]# passwd ceshi
Changing password for user ceshi.
New password: ceshi
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: ceshi
passwd: all authentication tokens updated successfully.
3.3、修改 /etc/shells
[root@hellodba ~]# vi /etc/shells
/sbin/nologin
--说明 /etc/shells文件的作用:
1) 系统某些服务在运行过程中,会去检查用户使用的shells,而这些shell查询就是借助/etc/shells这个文件。
2) 上面创建的FTP用户解释器为 /sbin/nologin,系统默认的 /etc/shells 文件里没有添加 /sbin/nologin,所以需将 /sbin/nologin 加入到 /etc/shells 文件中,否则FTP用户登录不了FTP服务。
三、安装vsftpd服务
1、通过YUM 安装vsftpd软件
[root@hellodba ~]# yum install -y vsftpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-25.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
vsftpd x86_64 3.0.2-25.el7 EL 171 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package
Total download size: 171 k
Installed size: 353 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : vsftpd-3.0.2-25.el7.x86_64 1/1
Verifying : vsftpd-3.0.2-25.el7.x86_64 1/1
Installed:
vsftpd.x86_64 0:3.0.2-25.el7
Complete!
[root@hellodba ~]# yum install -y ftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
ftp x86_64 0.17-67.el7 EL 61 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package
Total download size: 61 k
Installed size: 96 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ftp-0.17-67.el7.x86_64 1/1
Verifying : ftp-0.17-67.el7.x86_64 1/1
Installed:
ftp.x86_64 0:0.17-67.el7
Complete!
2、配置vsftpd服务
[root@hellodba ~]# cd /etc/vsftpd/
[root@hellodba vsftpd]# ll
-rw-------. 1 root root 125 Oct 31 2018 ftpusers
-rw-------. 1 root root 361 Oct 31 2018 user_list
-rw-------. 1 root root 5116 Oct 31 2018 vsftpd.conf
-rwxr--r--. 1 root root 338 Oct 31 2018 vsftpd_conf_migrate.sh
[root@hellodba vsftpd]# cp vsftpd.conf vsftpd.conf_bak
[root@hellodba vsftpd]# vi vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=000
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
allow_writeable_chroot=YES
//创建 chroot_list 文件并添加FTP用户
[root@hellodba vsftpd]# vi chroot_list
ceshi
3、重启FTP服务
[root@hellodba ~]# systemctl restart vsftpd
[root@hellodba ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2023-09-02 13:21:31 CST; 12s ago
Process: 19364 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 19365 (vsftpd)
Tasks: 1
CGroup: /system.slice/vsftpd.service
└─19365 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
Sep 02 13:21:31 hellodba systemd[1]: Starting Vsftpd ftp daemon...
Sep 02 13:21:31 hellodba systemd[1]: Started Vsftpd ftp daemon.
四、登录测试
[root@hellodba ~]# touch /FTP/data/ceshi/test.log
[root@hellodba ~]# ftp 192.168.0.52
Connected to 192.168.0.52 (192.168.0.52).
220 (vsFTPd 3.0.2)
Name (192.168.0.52:root): ceshi
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,0,52,55,10).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Sep 02 10:12 test.log
226 Directory send OK.
ftp> pwd
257 "/"
五、常用命令
// 启动FTP
[root@hellodba ~]# systemctl start vsftpd.service
//设置开机自动启动
[root@hellodba ~]# systemctl enable vsftpd.service
//停止FTP
[root@hellodba ~]# systemctl stop vsftpd.service
//查看FTP状态
[root@hellodba ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2023-09-23 19:56:17 CST; 5s ago
Process: 20167 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 20168 (vsftpd)
Tasks: 1
CGroup: /system.slice/vsftpd.service
└─20168 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
Sep 23 19:56:17 hellodba systemd[1]: Starting Vsftpd ftp daemon...
Sep 23 19:56:17 hellodba systemd[1]: Started Vsftpd ftp daemon.
//查看FTP端口
[root@hellodba ~]# netstat -antup | grep ftp
tcp6 0 0 :::21 :::* LISTEN 20168/vsftpd
六、写在后面
到此,基于CentOS Linux 7.6 部署FTP部署完毕。由于本人有限的能力和知识储备,如有错误敬请批评指正!
##################################################################################
使用到的命令
479 2023-10-11 17:05:13 root mdkir -p /FTP/data/ceshi
480 2023-10-11 17:05:21 root mkdir -p /FTP/data/ceshi
481 2023-10-11 17:05:30 root ls /FTP/data/ceshi
482 2023-10-11 17:05:46 root chown -R 755 /FTP/data/ceshi
483 2023-10-11 17:05:52 root ls -ld /FTP/data/ceshi
484 2023-10-11 17:07:14 root useradd -g root -M -d /FTP/data/ceshi/ -s /sbin/nologin ceshi
485 2023-10-11 17:07:31 root passwd ceshi
486 2023-10-11 17:07:47 root cat /etc/shells
487 2023-10-11 17:08:01 root echo "/sbin/nologin" >>/etc/shells
488 2023-10-11 17:08:07 root cat /etc/shells
489 2023-10-11 17:08:29 root yum -y install vsftpd
490 2023-10-11 17:10:14 root yum -y install ftp
491 2023-10-11 17:10:26 root cd /etc/vsftpd
492 2023-10-11 17:10:32 root ll
493 2023-10-11 17:10:54 root cp vsftpd.conf vsftpd.conf.bak
494 2023-10-11 17:10:56 root ls
495 2023-10-11 17:10:57 root ll
496 2023-10-11 17:11:06 root vi vsftpd.conf
497 2023-10-11 17:14:47 root vi chroot_list
498 2023-10-11 17:15:00 root systemctl restart vsftpd
499 2023-10-11 17:15:08 root systemctl status vsftpd
500 2023-10-11 17:15:29 root touch /FTP/data/ceshi/test.log
501 2023-10-11 17:15:33 root cat /FTP/data/ceshi/test.log
502 2023-10-11 17:15:42 root echo "111" >> /FTP/data/ceshi/test.log
503 2023-10-11 17:15:44 root cat /FTP/data/ceshi/test.log
504 2023-10-11 17:15:55 root ftp 192.168.8.230
505 2023-10-11 17:19:04 root sl
506 2023-10-11 17:19:06 root ls
507 2023-10-11 17:19:07 root ll
508 2023-10-11 17:19:12 root history
[root@zabbix-agent vsftpd]# cat vsftpd.conf 以下是添加到此文件下面去:
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=000
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
allow_writeable_chroot=YES
实现效果:
ceshi 123
THE END