天天看點

FTP

FTP

(File Transfer Protocol) 檔案傳輸協定,在網際網路上進行檔案傳輸的協定,用于在Internet上控制檔案的雙向傳輸,FTP服務端通常部署在企業内網,具有容易搭建,友善管理,安全穩定的特性。

TCP20端口 FTP資料連接配接 用于資料傳輸:上傳、下載下傳資料
TCP21端口 FTP控制連接配接 用于接受用戶端發出的相關FTP指令與參數
'網絡檔案共享服務主流:ftp、nfs、samba'
           

FTP登入使用者

匿名使用者--->系統使用者
系統使用者--->一般關閉
虛拟使用者--->系統使用者           

FTP傳輸模式

'主動模式:服務端通過20端口主動向用戶端連接配接,傳輸資料。主動模式防火牆需要打開21和20端口
被動模式:服務端啟動随機端口,等待用戶端發起連接配接,進行資料傳輸 防火牆隻打開21和20端口,被動模式FTP不能下載下傳資料'

總結:
   FTP服務端啟用防火牆,開啟21/20端口,這時FTP隻能工作在主動模式
     FTP用戶端開啟防火牆,這時FTP隻能選擇工作在被動模式
     FTP用戶端和FTP服務端都有防火牆,那麼将無法使用FTP主動和被動模式           

FTP服務安裝配置

'基礎環境準備'
[root@yaoxiaorong ~]# uname -r
3.10.0-693.el7.x86_64
[root@yaoxiaorong ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 
           
'關閉防火牆以及selinux'
[root@yaoxiaorong ~]# systemctl stop firewalld
[root@yaoxiaorong ~]# systemctl disable firewalld
[root@yaoxiaorong ~]# setenforce 0
           
'安裝vsftpd服務并啟動服務'
[root@yaoxiaorong ~]# yum install vsftpd db4-utils -y
[root@yaoxiaorong ~]# systemctl start vsftpd

[root@yaoxiaorong ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

'注意:預設使用匿名使用者通路共享目錄站點為/var/ftp僅擁有下載下傳權限'           

/etc/vsftpd/vsftpd.conf配置檔案注釋

[root@yaoxiaorong ~]# cat /etc/vsftpd/vsftpd.conf 
anonymous_enable=YES      '是否啟用匿名使用者'
local_enable=YES                  '是否啟動本地使用者'
write_enable=YES                  '開啟全局上傳檔案'
local_umask=022                    '控制本地使用者上傳檔案預設權限,通過ftp上傳檔案的預設遮罩碼'
anon_umask=022    '控制匿名使用者上傳檔案的預設權限'
anon_upload_enable=YES      '允許匿名使用者上傳'
anon_mkdir_write_enable=YES  '允許建立目錄'
dirmessage_enable=YES            '配置使用者目錄顯示資訊'
xferlog_enable=YES                    '啟動日志'
xferlog_file=/var/log/vsftpd.log     '指定日志位置,配置xferlog_enable使用'
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES          '是否作為一個獨立守護程序運作'
chroot_local_user=YES     '禁锢所有使用者在使用者目錄下'
chroot_lost_enable=YES   '配合chroot_list_file使用'
chroot_list_file=/etc/vsftpd/chroot_list   '禁锢使用者清單'
pam_service_name=vsftpd   'PAM認證檔案'
userlist_enable=YES     'ftpusers檔案下的使用者都被拒絕登陸通路'
tcp_wrappers=YES       
guest_enable=YES      '開啟虛拟使用者'
guest_username=www      'FTP虛拟使用者對應的系統使用者'
user_config_dir=/etc/vsftpd/ftplogin   '授權FTP虛拟使用者所在目錄'
           

權限控制

local_root=/data/ftp   指定本地使用者通路目錄站點
anon_root=/data/ftp   指定匿名使用者通路目錄站點           

資源控制

anon_max_rate=500000   匿名使用者限速
local_max_rate=800000   本地使用者限速
max_clients=100              同時能接收多少請求
max_per_ip=2                  一個IP同時能有多少連接配接           

vsftpd虛拟使用者配置

準備環境,關閉防火牆和selinux

[root@yaoxiaorong ~]# setenforce 0
[root@yaoxiaorong ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@yaoxiaorong ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
[root@yaoxiaorong ~]# systemctl stop firewalld
[root@yaoxiaorong ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.           

vsftpd虛拟使用者的配置步驟如下:

'安裝依賴的程式'
[root@yaoxiaorong ~]# cd /etc/yum.repos.d/
[root@yaoxiaorong ~]# yum -y install wget
[root@yaoxiaorong ~]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@yaoxiaorong yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yaoxiaorong yum.repos.d]# sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yaoxiaorong ~]# yum -y install epel-release
[root@yaoxiaorong ~]# yum -y install vsftpd
[root@yaoxiaorong ~]# yum -y install db4*  '安裝db4工具'           
建立文本格式的使用者名、密碼清單,例如添加兩個使用者tom,yao,密碼分别為123、456

[root@yaoxiaorong ~]# echo "tom" >> /etc/vsftpd/vu.list
[root@yaoxiaorong ~]# echo "123" >> /etc/vsftpd/vu.list
[root@yaoxiaorong ~]# echo "yao" >> /etc/vsftpd/vu.list
[root@yaoxiaorong ~]# echo "456" >> /etc/vsftpd/vu.list
[root@yaoxiaorong ~]# cat /etc/vsftpd/vu.list 
456
tom
123
yao
456      '使用者名和密碼都是對應的,前面輸入的是賬戶,後面是密碼'           
'将剛建立的文本格式使用者名、密碼檔案使用db4工具轉換成資料庫檔案'
-T表示轉換,-t表示加密方式使用hash算法加密
[root@yaoxiaorong ~]# db_load -T -t hash -f /etc/vsftpd/vu.list /etc/vsftpd/vu.db           
'将檔案權限設定為600,提高虛拟使用者賬戶檔案的安全性,避免資料外洩'
[root@yaoxiaorong ~]# chmod 600 /etc/vsftpd/vu.*
[root@yaoxiaorong ~]# ll /etc/vsftpd/vu.*
-rw-------. 1 root root 12288 Aug  8 17:26 /etc/vsftpd/vu.db
-rw-------. 1 root root    20 Aug  8 17:18 /etc/vsftpd/vu.list           
[root@yaoxiaorong ~]# useradd -d /var/ftproot -s /sbin/nologin vftp
[root@yaoxiaorong ~]# chmod 755 /var/ftproot
[root@yaoxiaorong ~]# ll -d /var/ftproot
drwxr-xr-x. 3 vftp vftp 78 Aug  8 17:50 /var/ftproot
           
'為虛拟使用者建立PAM認證'
[root@yaoxiaorong ~]# cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
[root@yaoxiaorong ~]# vim /etc/pam.d/vsftpd
#%PAM-1.0
auth  required  pam_userdb.so db=/etc/vsftpd/vu
account  required pam_userdb.so db=/etc/vsftpd/vu
           
[root@yaoxiaorong ~]# echo 'guest_enable=YES' >> /etc/vsftpd/vsftpd.conf    '允許其他人可以登陸'
[root@yaoxiaorong ~]# echo 'guest_username=vftp' >> /etc/vsftpd/vsftpd.conf     '添加其他使用者名字為vftp'
[root@yaoxiaorong ~]# echo 'user_config_dir=/etc/vsftpd/vusers_dir' >> /etc/vsftpd/vsftpd.conf
[root@yaoxiaorong ~]# echo 'allow_writeable_chroot=YES' >>/etc/vsftpd/vsftpd.conf 
           
'建立/etc/vsftpd/vusers_dir目錄,使tom可以上傳檔案和建立目錄,而yao隻能預設的下載下傳權限'
[root@yaoxiaorong ~]# mkdir /etc/vsftpd/vusers_dir
[root@yaoxiaorong ~]# ll /etc/vsftpd
total 36
-rw-------. 1 root root   125 Aug  3  2017 ftpusers
-rw-------. 1 root root   361 Aug  3  2017 user_list
-rw-------. 1 root root  5116 Aug  8 18:08 vsftpd.conf
-rwxr--r--. 1 root root   338 Aug  3  2017 vsftpd_conf_migrate.sh
-rw-------. 1 root root 12288 Aug  8 17:26 vu.db
-rw-------. 1 root root    20 Aug  8 17:18 vu.list
drwxr-xr-x. 2 root root     6 Aug  8 18:21 vusers_dir
'使tom使用者可以上傳檔案,建立目錄'
[root@yaoxiaorong ~]# echo 'anon_upload_enable=YES' >> /etc/vsftpd/vusers_dir/tom     允許匿名使用者上傳
[root@yaoxiaorong ~]# echo 'anon_mkdir_write_enable=YES' >>/etc/vsftpd/vusers_dir/tom   允許匿名使用者建立目錄,但是不能删除
[root@yaoxiaorong ~]# echo 'anon_umask=022' >> /etc/vsftpd/vusers_dir/tom   控制匿名使用者上傳檔案檔案的預設權限
[root@yaoxiaorong ~]# echo 'anon_other_write_enable=YES' >> /etc/vsftpd/vusers_dir/tom      允許匿名使用者建立和删除目錄
'使yao使用者有預設的下載下傳權限,建立一個名叫yao的空檔案'
[root@yaoxiaorong ~]# touch /etc/vsftpd/vusers_dir/yao
           
'啟動服務'
[root@yaoxiaorong ~]# systemctl start vsftpd
[root@yaoxiaorong ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
root@yaoxiaorong ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      32     :::21                 :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*               
[root@yaoxiaorong ~]# vim ftp.sh
#!/bin/bash
yonghu=tom
yonghu2=jerry
STOREAGE=/etc/vsftpd/vusers_dir

setenforce 0
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld

cd /etc/yum.repos.d/
mv * /tmp/
curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
sed -i 's/\$releasever/7/g' /etc/yum.repos.d/163.repo
sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/163.repo

yum -y install epel-release &>/dev/null
yum -y install vsftpd  &>/dev/null
yum -y install db4* &>/dev/null

echo "$yonghu" > /etc/vsftpd/vu.list
echo '123' >> /etc/vsftpd/vu.list
echo "$yonghu2" >> /etc/vsftpd/vu.list
echo '456' >> /etc/vsftpd/vu.list

db_load -T -t hash -f /etc/vsftpd/vu.list /etc/vsftpd/vu.db
chmod 600 /etc/vsftpd/vu.*
useradd -d /var/ftproot -s /sbin/nologin vftp
chmod 755 /var/ftproot/

mv /etc/pam.d/vsftpd{,.bak}
cat > /etc/pam.d/vsftpd <<EOF
#%PAM-1.0
auth required pam_userdb.so db=/etc/vsftpd/vu
account required pam_userdb.so db=/etc/vsftpd/vu
EOF

echo 'guest_enable=YES' >> /etc/vsftpd/vsftpd.conf
echo 'guest_username=vftp' >> /etc/vsftpd/vsftpd.conf
echo 'user_config_dir=/etc/vsftpd/vusers_dir' >> /etc/vsftpd/vsftpd.conf
echo 'allow_writeable_chroot=YES' >>/etc/vsftpd/vsftpd.conf
if [ ! -d $STOREAGE ];then
        mkdir -p $STOREAGE
fi
echo 'anon_upload_enable=YES' >> /etc/vsftpd/vusers_dir/$yonghu
echo 'anon_mkdir_write_enable=YES' >> /etc/vsftpd/vusers_dir/$yonghu
echo 'anon_umask=022' >> /etc/vsftpd/vusers_dir/$yonghu
echo 'anon_other_write_enable=YES' >> /etc/vsftpd/vusers_dir/$yonghu
touch /etc/vsftpd/vusers_dir/$yonghu1

systemctl start vsftpd
systemctl enable vsftpd           
上一篇: haproxy
下一篇: ansible-playbook