簡介ftp
- File Transfer Protocol 早期 的三個應(yīng)用級協(xié)議之一
- 基于C/S
- 雙通道協(xié)議:數(shù)據(jù)和命令連接
- 數(shù)據(jù)傳輸格式:二進制(默認)和文本
- 兩種模式:服務(wù)器角度
(1)主動(PORT style)服務(wù)器主動連接
命令(控制):客戶端:隨機port ---> 服務(wù)器:tcp21
數(shù)據(jù): 客戶端:隨機port+1 <--- 服務(wù)器:tcp20
(2)被動(PASV style)客戶端主動連接
命令 (控制 ):客戶端:隨機port ---> 服務(wù)器:tcp21
數(shù)據(jù):客戶端:隨機port+1 ---> 服務(wù)器:隨機port - 狀態(tài)碼
1XX :信息 125 :數(shù)據(jù)連接打開
2XX :成功類狀態(tài) 200 :命令OK 230 :登錄成功
3XX :補充類 331 :用戶名OK
4XX :客戶端錯誤 425 :不能打開數(shù)據(jù)連接
5XX :服務(wù)器錯誤 530 :不能登錄 - 用戶認證:
匿名用戶:ftp,anonymous, 對應(yīng)Linux 用戶ftp
系統(tǒng)用戶:Linux 用戶, 用戶/etc/passwd, 密碼/etc/shadow
虛擬用戶:特定服務(wù)的專用用戶,獨立的用戶/ 密碼文件
nsswitch:network service switch 名稱解析框架
pam:pluggable authentication module 用戶認證
/lib64/security /etc/pam.d/ /etc/pam.conf - 由vsftpd 包提供
- 不再由xinetd管理
- 用戶認證配置文件:/etc/pam.d/vsftpd
- 服務(wù)腳本: /usr/lib/systemd/system/vsftpd.service 7
/etc/rc.d/init.d/vsftpd 6 - 配置文件:/etc/vsftpd/vsftpd.conf
- 幫助 man 5 vsftpd.conf
- 匿名用戶(映射為系統(tǒng)用戶ftp )共享文件位置:/var/ftp
- 系統(tǒng)用戶共享文件位置:用戶家目錄
- 虛擬用戶共享文件位置:為其映射的系統(tǒng)用戶的家目錄
配置
1更改命令端口
更改命令端口 listen_port=端口號如90 ;默認配置文件無此行,表示監(jiān)聽21 端口 添加此行并要監(jiān)聽得問端口;修改后關(guān)閉selinux策略;setenforce 0臨時關(guān)閉
重啟服務(wù)
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
listen_port=28
#更改ftp的命令端口為非標準28端口加listen_port=28 ;如果不加此行表示使用標準21端口
[root@localhost ~]# systemctl restart vsftpd.service
客戶端測試
[root@root ~]# ftp 192.168.175.130 28
Connected to 192.168.175.130 (192.168.175.130).
220 (vsFTPd 3.0.2)
Name (192.168.175.130:root): ftp
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,175,130,190,237).
150 Here comes the directory listing.
drwxr-xr-x 2 0 0 16 Aug 10 21:24 pub
226 Directory send OK.
ftp>
服務(wù)器端查看連接狀態(tài)
[root@localhost pub]# ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 192.168.175.130:22 192.168.175.1:13991
ESTAB 0 0 ::ffff:192.168.175.130:28 ::ffff:192.168.175.129:33670
[root@localhost pub]#
更改主動端口
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
connect_from_port_20=YES
ftp_data_port=39 更改主動模式端口為39
客戶端測試
ftp> get 4.txt 注意要在服務(wù)器端給該文件授權(quán)
local: 4.txt remote: 4.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for 4.txt (4294967296 bytes).
服務(wù)器端查看結(jié)果
[root@localhost pub]# ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.175.130:22 192.168.175.1:12213
ESTAB 0 0 192.168.175.130:22 192.168.175.1:13991
ESTAB 0 3569320 ::ffff:192.168.175.130:39 主動端口 ::ffff:192.168.175.129:59343
ESTAB 0 0 ::ffff:192.168.175.130:28 ::ffff:192.168.175.129:33692
更改服務(wù)器端的被動端口
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
connect_from_port_20=YES
pasv_min_port=6000
#數(shù)字指定隨機端口的最小值為6000;如果是0表示系統(tǒng)隨便分配
pasv_max_port=6010
#數(shù)字指定最大端口值為6010
重啟服務(wù)
[root@localhost pub]# systemctl restart vsftpd.service
客戶端測試
ftp> get 4.txt
local: 4.txt remote: 4.txt
227 Entering Passive Mode (192,168,175,130,23,114).
150 Opening BINARY mode data connection for 4.txt (4294967296 bytes).
[root@localhost pub]# ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 52 192.168.175.130:22 192.168.175.1:13991
ESTAB 0 0 ::ffff:192.168.175.130:28 ::ffff:192.168.175.129:33694
ESTAB 0 3276720 ::ffff:192.168.175.130:6002 端口范圍之內(nèi)的值 ::ffff:192.168.175.129:55467
默認權(quán)限設(shè)置
[root@localhost ftp]# ll -d /var/ftp/pub/
drwxr-xr-x. 2 root root 42 8月 11 06:32 /var/ftp/pub/
[root@localhost ftp]# ll -d /var/ftp/
drwxr-xr-x. 3 root root 17 8月 11 06:16 /var/ftp/
[root@localhost ftp]#
更改為當?shù)貢r間
客戶端ls顯示為UTC時間
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,117).
150 Here comes the directory listing.
-rwxrwxrwx 1 0 0 716042 Aug 10 2017 2.txt
-rwxrwxrwx 1 0 0 4294967296 Aug 10 2017 4.txt
-rwxrwxrwx 1 0 0 0 Aug 10 2017 kk
226 Directory send OK.
ftp>
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
connect_from_port_20=YES
#use_localtime=YES
#默認是no表示使用GMT
pasv_min_port=6000
#數(shù)字指定隨機端口的最小值為6000;如果是0表示系統(tǒng)隨便分配
pasv_max_port=6010
use_localtime=YES
#使用當?shù)貢r間;默認是no表示使用GMT
重啟服務(wù)
[root@localhost pub]# systemctl restart vsftpd.service
服務(wù)器上的文件時間
[root@localhost ftp]# ll
總用量 0
drwxr-xr-x. 2 root root 42 8月 11 06:32 pub
[root@localhost ftp]#
測試客戶端
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,117).
150 Here comes the directory listing.
-rwxrwxrwx 1 0 0 716042 Aug 11 06:12 2.txt
-rwxrwxrwx 1 0 0 4294967296 Aug 11 06:34 4.txt
-rwxrwxrwx 1 0 0 0 Aug 11 05:24 kk
226 Directory send OK.
ftp>
設(shè)置允許匿名用戶上傳文件
客戶端默認匿名用戶不允許
ftp> put 6.txt
local: 6.txt remote: 6.txt
227 Entering Passive Mode (192,168,175,130,23,114).
550 Permission denied.
ftp>
服務(wù)器端設(shè)置
[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES
#啟用這一項把注釋去掉就支持匿名用戶上傳文件了
賦予共享目錄下子文的寫權(quán)限;必須是子文件ftp不許共享目錄直接有寫權(quán)限
[root@localhost ftp]# chmod o+w /var/ftp/pub/
重啟服務(wù)
[root@localhost pub]# systemctl restart vsftpd.service
客戶端測試
ftp> put 6.txt
local: 6.txt remote: 6.txt
227 Entering Passive Mode (192,168,175,130,23,121).
150 Ok to send data.
226 Transfer complete.
8 bytes sent in 0.000209 secs (38.28 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,118).
150 Here comes the directory listing.
-rwxrwxrwx 1 0 0 716042 Aug 11 06:12 2.txt
-rwxrwxrwx 1 0 0 4294967296 Aug 11 06:34 4.txt
-rw------- 1 14 50 8 Aug 11 09:02 6.txt
-rwxrwxrwx 1 0 0 0 Aug 11 05:24 kk
226 Directory send OK.
ftp>
- 設(shè)置匿名用戶登錄不檢查口令
客戶端
[root@root ~]# ftp 192.168.175.130 28
Connected to 192.168.175.130 (192.168.175.130).
220 (vsFTPd 3.0.2)
Name (192.168.175.130:root): ftp用戶
331 Please specify the password.
Password:密碼
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
服務(wù)器
[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf
no_anon_password=YES
#設(shè)置匿名用戶登錄不檢查口令;( 默認NO) 匿名用戶略過口令 檢查
重啟ftp服務(wù)
[root@localhost ftp]# systemctl restart vsftpd.service
客戶端測試
[root@root ~]# ftp 192.168.175.130 28
Connected to 192.168.175.130 (192.168.175.130).
220 (vsFTPd 3.0.2)
Name (192.168.175.130:root): ftp用戶
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
- 設(shè)置允許客戶端創(chuàng)建文件
[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf
anon_mkdir_write_enable=YES
#設(shè)置允許用戶創(chuàng)建文件
重啟服務(wù);注意文件的權(quán)限
[root@localhost ftp]# systemctl restart vsftpd.service
客戶端測試
ftp> ?
Commands may be abbreviated. Commands are:
! debug mdir sendport site
$ dir mget put size
account disconnect mkdir pwd status
append exit mls quit struct
ascii form mode quote system
bell get modtime recv sunique
binary glob mput reget tenex
bye hash newer rstatus tick
case help nmap rhelp trace
cd idle nlist rename type
cdup image ntrans reset user
chmod lcd open restart umask
close ls prompt rmdir verbose
cr macdef passive runique ?
delete mdelete proxy send
ftp> mkdir hh 創(chuàng)一個文件夾hh
257 "/pub/hh" created
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,114).
150 Here comes the directory listing.
-rwxrwxrwx 1 0 0 716042 Aug 11 06:12 2.txt
-rwxrwxrwx 1 0 0 4294967296 Aug 11 06:34 4.txt
-rw------- 1 14 50 8 Aug 11 09:02 6.txt
drwx------ 2 14 50 6 Aug 11 09:17 hh
-rwxrwxrwx 1 0 0 0 Aug 11 05:24 kk
226 Directory send OK.
ftp>
- 設(shè)置用戶可刪除和修改上傳的文件、
服務(wù)器
vim /etc/vsftpd/vsftpd.conf
anon_other_write_enable=YES
#可刪除和修改上傳的文件
[root@localhost ftp]# systemctl restart vsftpd.service
客戶測試
ftp> delete kk刪除文件
250 Delete operation successful.
ftp> mdelete ll 刪除目錄或多個文件
更改匿名賬戶上傳文件的目錄
更改運行vstpd家目錄就可以了

設(shè)置用戶上傳文件的權(quán)限
anon_umask=077
#指定匿名上傳umask
指定上傳文件的默認的所有者和權(quán)限
chown_uploads=YES
#啟用上傳文件默認所有者
chown_username=chenxi
#指定所有者
chown_upload_mode=0644
#設(shè)置權(quán)限
將所有系統(tǒng)賬戶映射成指定賬戶
系統(tǒng)賬戶
[root@localhost pub]# getent passwd chenxi1
chenxi1:x:1000:1000::/home/chenxi1:/bin/bash
客戶端用系統(tǒng)賬戶測試
[root@root ~]# ftp 192.168.175.130 28
Connected to 192.168.175.130 (192.168.175.130).
220 (vsFTPd 3.0.2)
Name (192.168.175.130:root): chenxi1 系統(tǒng)
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/home/chenxi1"
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,120).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd /etc/ 將會特別危險
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,175,130,23,122).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 5090 Nov 05 2016 DIR_COLORS
-rw-r--r-- 1 0 0 5725 Nov 05 2016 DIR_COLORS.256color
服務(wù)器端
[root@localhost pub]# vim /etc/vsftpd/vsftpd.conf
guest_enable=YES
#所有系統(tǒng)用戶都映射成guest 用戶
guest_username=ftp
#配合上面選項才生效,指定guest 用戶guest_enable=YES 所有系統(tǒng)用戶都映射成guest 用戶而guest用戶映射成ftq
重啟
[root@localhost pub]# systemctl restart vsftpd.service
客戶端測試
[root@root ~]# ftp 192.168.175.130 28
Connected to 192.168.175.130 (192.168.175.130).
220 (vsFTPd 3.0.2)
Name (192.168.175.130:root): chenxi1
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,175,130,23,119).
150 Here comes the directory listing.
drwxr-xrwx 4 0 0 65 Aug 11 09:32 pub
226 Directory send OK.
ftp> pwd
257 "/"
ftp>
服務(wù)器
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#設(shè)置是否允許系統(tǒng)賬戶登錄YSE允許NO是不允許
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#設(shè)置是否允許系統(tǒng)用戶上傳文件YES允許NO拒絕
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#設(shè)置系統(tǒng)用戶上文件的umask權(quán)限
chroot_local_user=YES
#(默認NO ,不禁錮)禁錮系統(tǒng) 用戶
hroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
當chroot_local_user=YES 時,則chroot_list 中用戶不禁錮
當chroot_local_user=NO 時,則chroot_list中 用戶禁錮
更改記得重啟
注意重啟是吧配置文件沒有錯的前提下;應(yīng)該是選項位置不對
- 其他相關(guān)的配置
日志相關(guān)配置
xferlog_enable=YES
#(默認) 啟用 記錄上傳下載日志
xferlog_std_format=YES
#(默認 )使用wu-ftp 日志格式
xferlog_file=/var/log/xferlog
#/var/log/xferlog (默認)可自動生成
目錄訪問提示信息
dirmessage_enable=YES ( 默認)
message_file=.message( 默認) 信息 存放在 指定目錄下.message
echo chenxi>/var/ftp/pub/.message 用戶切到該目錄下就會提示chenxi字樣
- 用戶訪問控制的相關(guān)設(shè)置
使用pam(Pluggable Authentication Modules)
[root@localhost pub]# vim /etc/vsftpd/vsftpd.conf
pam_service_name=vsftpd
#默認指明驗證用戶調(diào)用pam模塊
pam 配置文件:/etc/pam.d/vsftpd #pam 模塊關(guān)于vsftpd的配置文件
pam模塊對用戶的登錄設(shè)置
[root@localhost pub]# vim /etc/pam.d/vsftpd
#%PAM-1.0
session optional pam_keyinit.so force revoke
auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth required pam_shells.so
auth include password-auth
account include password-auth
session required pam_loginuid.so
session include password-auth
#auth required pam_listfile.so item=user sense=deny(拒絕)后面文件里用戶登錄 file=/etc/vsftpd/ftpusers onerr=succeed
sense=allow(允許)只允許后面文件里用戶登錄 file=/etc/vsftpd/ftpusers onerr=succeed
/etc/vsftpd/ftpusers 默認文件中用戶拒絕登錄
vsftp配置對用戶登錄限制的相關(guān)設(shè)置
是否啟用控制用戶登錄的列表文件
[root@localhost pub]# vim /etc/vsftpd/vsftpd.conf
userlist_enable=YES
# 默認有此設(shè)置
userlist_deny=YES
#( 默認值 值) 黑名單, 不提示口令,NO 為 白名單
userlist_file=/etc/vsftpd/users_list
#此文件的用戶
連接 限制
[root@localhost pub]# vim /etc/vsftpd/vsftpd.conf
max_clients=0
#最大并發(fā)連接數(shù)
max_per_ip=0
#每個IP 同時發(fā)起的最大連接 數(shù)
nopriv_user=nobody
#vsftpd 服務(wù)指定用戶身份運行
傳輸速率: 字節(jié)/秒 秒
anon_max_rate=0 匿名用戶的最大傳輸速率
local_max_rate=0 本地用戶的最大傳輸速率
連接時間:秒為單位
connect_timeout=60 主動模式 數(shù)據(jù) 連接超時時長
accept_timeout=60 被動模式 數(shù)據(jù) 連接超時時長
data_connection_timeout=300 數(shù)據(jù)連接無數(shù)據(jù)輸超時時長
idle_session_timeout=60 無命令操作超時時長
優(yōu)先以 文本方式傳輸
ascii_upload_enable=YES
ascii_download_enable=YES
重啟后生效
客戶端
ftp> binary 表示以二進制格式傳輸
200 Switching to Binary mode
ftp> ascii 表示以文本格式傳輸
200 Switching to ASCII mode.
- 設(shè)置FTP為非獨立服務(wù)6
安裝xinetd.d服務(wù),由于telnet-server依賴于xinetd.d該服務(wù)所以安裝telnet-server就自動把xinetd.d服務(wù)裝上
yum -y install telnet-server
vim /etc/vsftpd/vsftpd.conf
# with the listen_ipv6 directive.
listen=NO
#默認是YES表示獨立服務(wù);改成NO就是非獨立服務(wù)
退出后直接把ftp服務(wù)關(guān)閉
service vsftpd stop
讓ftp受xinetd服務(wù)管理
vim /etc/xinetd.d/vsftpd
service ftp
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/vsftpd
log_on_failure += USERID
disable = no
}
[root@root xinetd.d]# chkconfig --list
auditd 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
blk-availability 0:關(guān)閉 1:啟用 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
crond 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
ip6tables 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
iptables 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
kdump 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:啟用 4:啟用 5:啟用 6:關(guān)閉
lvm2-monitor 0:關(guān)閉 1:啟用 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
mdmonitor 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
messagebus 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
netconsole 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
netfs 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:啟用 4:啟用 5:啟用 6:關(guān)閉
network 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
nfs-rdma 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
ntpdate 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
postfix 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
rdisc 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
rdma 0:關(guān)閉 1:啟用 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
restorecond 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
rsyslog 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
saslauthd 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
sshd 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
svnserve 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
udev-post 0:關(guān)閉 1:啟用 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
vsftpd 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
xinetd 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:啟用 4:啟用 5:啟用 6:關(guān)閉
基于 xinetd 的服務(wù):
chargen-dgram: 關(guān)閉
chargen-stream: 關(guān)閉
daytime-dgram: 關(guān)閉
daytime-stream: 關(guān)閉
discard-dgram: 關(guān)閉
discard-stream: 關(guān)閉
echo-dgram: 關(guān)閉
echo-stream: 關(guān)閉
rsync: 關(guān)閉
tcpmux-server: 關(guān)閉
telnet: 關(guān)閉
time-dgram: 關(guān)閉
time-stream: 關(guān)閉
vsftpd: 啟用 #表示ftp受xinetd服務(wù)管理
直接啟動xinetd服務(wù)
service xinetd start
端口查看
[root@root xinetd.d]# ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 64 :::21 ftp端口已經(jīng)啟動 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 ::1:6011 :::*
LISTEN 0 128 127.0.0.1:6012 *:*
LISTEN 0 128 ::1:6012 :::*
[root@root xinetd.d]#