天天看點

在CentOS7下搭建vsftpd伺服器

作者:198兜兜裡有糖
在CentOS7下搭建vsftpd伺服器

一、關閉SELINUX并配置yum源

sed -i 's/enforcing/disabled/g' /etc/selinux/config
setenforce 0
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo 
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo            

二、上傳腳本,一鍵執行

chmod 755 vsftpd_install_virtualuser.sh 
./vsftpd_install_virtualuser.sh            
在CentOS7下搭建vsftpd伺服器
#!/bin/sh
#1、安裝vsftpd服務
yum install vsftpd -y
#2、備份預設配置檔案,并重新生成配置檔案
cd /etc/vsftpd/
cp vsftpd.conf vsftpd.conf_default
echo > vsftpd.conf
cat > /etc/vsftpd/vsftpd.conf  << \EOF
#設成NO,不允許匿名使用者登陸
anonymous_enable=NO
#允許/禁止本地使用者登陸 注意:主要是為虛拟宿主使用者,如果該項目設定為NO那麼所有虛拟使用者将無法通路。
local_enable=YES
#設定可以進行寫操作
write_enable=YES
#設定啟用虛拟使用者功能
guest_enable=YES
#指定虛拟使用者的宿主使用者
guest_username=ftpvuser
#設定虛拟使用者的權限符合他們的宿主使用者
#virtual_use_local_privs=YES
#設定上傳後檔案的權限掩碼,檔案644,檔案夾755
local_umask=022
#設定開啟目錄智語功能
dirmessage_enable=YES
#設定開啟日志記錄功能
xferlog_enable=YES
#設定端口20進行資料連接配接
connect_from_port_20=YES
#設定日志使用标準的記錄格式
xferlog_std_format=YES
#開啟獨立程序vsftpd,不使用超級程序xinetd。設定該Vsftpd服務工作在StandAlone模式下。
listen=YES
#設定userlist_file中的使用者将不得使用FTP
userlist_enable=YES
#設定支援TCP Wrappers
tcp_wrappers=YES
#限制所有使用者在主目錄
chroot_local_user=YES
allow_writeable_chroot=YES
#匿名使用者上傳檔案的 umask 值
anon_umask=077
#設定,啟用pam認證,并指定認證檔案名/etc/pam.d/vsftpd.vu
pam_service_name=vsftpd.vu
#設定虛拟使用者個人vsftp的配置檔案存放路徑。也就是說,這個被指定的目錄裡,将存放每個vsftp虛拟使用者個性的配置檔案,
#一個需要注意的地方就是這些配置檔案名必須和虛拟使用者名相同。
user_config_dir=/etc/vsftpd/vusers_dir
EOF
#3、設定被動傳輸時的資料傳輸端口
echo "pasv_enable=YES">> /etc/vsftpd/vsftpd.conf
echo "pasv_min_port=30001">> /etc/vsftpd/vsftpd.conf
echo "pasv_max_port=30010">> /etc/vsftpd/vsftpd.conf
#4、建立用于進行 FTP 認證的使用者資料庫檔案
#這裡使用文本檔案進行使用者認證
#資料庫檔案中奇數行為賬戶名,偶數行為密碼
cat > /etc/vsftpd/vuser.list << \EOF
ftpuser
vsFtp2022
EOF
#4、建立 vsftpd 服務程式用于存儲檔案的根目錄以及虛拟使用者映射的系統本地使用者
mkdir /data
useradd -d /data/FTP -s /bin/nologin ftpvuser
cd /etc/vsftpd/
db_load -T -t hash -f vuser.list vuser.db
chmod 600 vuser.db 
#5、為虛拟使用者設定通路權限
#隻需建立一個目錄,在裡面分别建立以虛拟使用者名稱命名的檔案
#在每個檔案中,對使用者分别進行配置,達到管理不用使用者權限的效果
mkdir /etc/vsftpd/vusers_dir/
cd /etc/vsftpd/vusers_dir/
touch ftpuser 
cat > ftpuser  << \EOF
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
dirmessage_enable=YES
anon_world_readable_only=NO
EOF
#6、建立支援虛拟使用者的 PAM 認證檔案
cat > /etc/pam.d/vsftpd.vu << \EOF
auth       required     pam_userdb.so    db=/etc/vsftpd/vuser
account    required     pam_userdb.so    db=/etc/vsftpd/vuser
EOF
#7、防火牆設定及vsftpd服務開啟
firewall-cmd --add-service=ftp --zone=public --permanent
firewall-cmd --permanent --zone=public --add-port=30001-30010/tcp
firewall-cmd --reload
systemctl restart vsftpd
systemctl enable vsftpd           

三、測試

在CentOS7下搭建vsftpd伺服器
在CentOS7下搭建vsftpd伺服器

四、中文亂碼的問題

在CentOS7下搭建vsftpd伺服器
在CentOS7下搭建vsftpd伺服器