root@remote:/# uname -r 3.16.0-4-amd64 root@remote:/e# lsb_release No LSB modules are available. root@remote:/# lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 8.6 (jessie) Release: 8.6 Codename: jessie
hubery@remote:~$ sudo apt-get install vsftpd ftp -y
html
重啓vsftpd後才能使得更改的配置生效sudo systemctl restart vsftpd
默認配置:linux
hubery@remote:~$ cat /etc/vsftpd.conf | grep -v "^#" | grep -v "^$" listen=NO listen_ipv6=YES anonymous_enable=NO local_enable=YES dirmessage_enable=YES use_localtime=YES xferlog_enable=YES connect_from_port_20=YES secure_chroot_dir=/var/run/vsftpd/empty pam_service_name=vsftpd rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key ssl_enable=NO
此時vsftpd已經在運行:shell
hubery@remote:~$ sudo systemctl status vsftpd.service ● vsftpd.service - vsftpd FTP server Loaded: loaded (/lib/systemd/system/vsftpd.service; enabled) Active: active (running) since 四 2016-12-29 02:55:14 EST; 5min ago Main PID: 17653 (vsftpd) CGroup: /system.slice/vsftpd.service └─17653 /usr/sbin/vsftpd /etc/vsftpd.conf 12月 29 02:55:14 remote systemd[1]: Started vsftpd FTP server. hubery@remote:~$ sudo netstat -lpn | grep vsftpd tcp6 0 0 :::21 :::* LISTEN 17653/vsftpd
vsftp使用系統用戶來登錄,任何能夠登陸系統的用戶均可以登錄vsftpd瀏覽器
hubery@remote:~$ ftp localhost Connected to localhost. 220 (vsFTPd 3.0.2) Name (localhost:hubery): 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> 221 Goodbye.
可是爲了安全,咱們建立普通用戶,這個用戶能夠登陸vsfptd,可是不能SSH登錄系統安全
hubery@remote:~$ sudo bash -c "echo '/bin/false' >> /etc/shells" hubery@remote:~$ sudo useradd -m jack -s /bin/false hubery@remote:~$ sudo passwd jack Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully hubery@remote:~$ ls ~jack hubery@remote:~$ sudo dd if=/dev/zero of=~jack/file.64MB bs=1M count=64 64+0 records in 64+0 records out 67108864 bytes (67 MB) copied, 0.0278935 s, 2.4 GB/s hubery@remote:~$ ls -lh ~jack total 64M -rw-r--r-- 1 root root 64M 12月 29 03:14 file.64MB hubery@remote:~$ su jack Password: hubery@remote:~$ echo $? 1
hubery@remote:~$ ftp -d -v localhost Connected to localhost. 220 (vsFTPd 3.0.2) ftp: setsockopt: Bad file descriptor Name (localhost:hubery): jack ---> USER jack 331 Please specify the password. Password: ---> PASS XXXX 500 OOPS: vsftpd: refusing to run with writable root inside chroot() Login failed. ---> SYST 421 Service not available, remote server has closed connection ftp> ---> QUIT 221 Goodbye.
配置文件/etc/vsftpd.conf添加一行allow_writeable_chroot=YES
,重啓vsftpdbash
hubery@remote:~$ cd tmp/ hubery@remote:~/tmp$ dd if=/dev/zero of=file.32MB bs=1M count=16 16+0 records in 16+0 records out 16777216 bytes (17 MB) copied, 0.0147825 s, 1.1 GB/s hubery@remote:~/tmp$ ls file.32MB hubery@remote:~/tmp$ ftp -d -v localhost Connected to localhost. 220 (vsFTPd 3.0.2) ftp: setsockopt: Bad file descriptor Name (localhost:hubery): jack ---> USER jack 331 Please specify the password. Password: ---> PASS XXXX 230 Login successful. ---> SYST 215 UNIX Type: L8 Remote system type is UNIX. Using binary mode to transfer files. ftp> ls ftp: setsockopt (ignored): Permission denied ---> EPRT |2|::1|59080| 200 EPRT command successful. Consider using EPSV. ---> LIST 150 Here comes the directory listing. -rw-r--r-- 1 0 0 67108864 Dec 29 03:14 file.64MB 226 Directory send OK. ftp> !ls /bin/bash file.32M ftp> ---> QUIT 221 Goodbye.
FTP兩種模式的區別:
(1)PORT(主動)模式服務器
所謂主動模式,指的是FTP服務器「主動」去鏈接客戶端的數據端口來傳輸數據,其過程具體來講就是:客戶端從一個任意的非特權端口N(N>1024)鏈接到FTP服務器的命令端口(即tcp 21端口),緊接着客戶端開始監聽端口N+1,併發送FTP命令「port N+1」到FTP服務器。而後服務器會從它本身的數據端口(20)「主動」鏈接到客戶端指定的數據端口(N+1),這樣客戶端就能夠和ftp服務器創建數據傳輸通道了。網絡
(2)PASV(被動)模式併發
所謂被動模式,指的是FTP服務器「被動」等待客戶端來鏈接本身的數據端口,其過程具體是:當開啓一個FTP鏈接時,客戶端打開兩個任意的非特權本地端口(N >1024和N+1)。第一個端口鏈接服務器的21端口,但與主動方式的FTP不一樣,客戶端不會提交PORT命令並容許服務器來回連它的數據端口,而是提交PASV命令。這樣作的結果是服務器會開啓一個任意的非特權端口(P > 1024),併發送PORT P命令給客戶端。而後客戶端發起從本地端口N+1到服務器的端口P的鏈接用來傳送數據。(注意此模式下的FTP服務器不須要開啓tcp 20端口了)app
兩種模式的比較:
(1)PORT(主動)模式模式只要開啓服務器的21和20端口,而PASV(被動)模式須要開啓服務器大於1024全部tcp端口和21端口。
(2)從網絡安全的角度來看的話彷佛ftp PORT模式更安全,而ftp PASV更不安全,那麼爲何RFC要在ftp PORT基礎再製定一個ftp PASV模式呢?其實RFC制定ftp PASV模式的主要目的是爲了數據傳輸安全角度出發的,由於ftp port使用固定20端口進行傳輸數據,那麼做爲黑客很容使用sniffer等探嗅器抓取ftp數據,這樣一來經過ftp PORT模式傳輸數據很容易被黑客竊取,所以使用PASV方式來架設ftp server是最安全絕佳方案。
所以:若是隻是簡單的爲了文件共享,徹底能夠禁用PASV模式,解除開放大量端口的威脅,同時也爲防火牆的設置帶來便利。
不幸的是,FTP工具或者瀏覽器默認使用的都是PASV模式鏈接FTP服務器,所以,必需要使vsftpd在開啓了防火牆的狀況下,也可以支持PASV模式進行數據訪問。
iptables配置: 主動模式使用20端口來傳輸數據,須要同時打開
hubery@remote:~$ sudo iptalbes -I INPUT 4 -m state --state NEW -p tcp --dport 21 -j ACCEPT hubery@remote:~$ sudo iptalbes -I INPUT 4 -m state --state NEW -p tcp --dport 20 -j ACCEPT
儘管如此,因爲vsftpd須要主動鏈接ftp客戶端監聽的端口,致使部分操做失敗。這樣就須要關閉客戶端所在機器的防火牆www@localhost:~$ sudo iptables -F
ftp> ls ftp: setsockopt (ignored): Permission denied ---> PORT 192,168,0,156,86,3 200 PORT command successful. Consider using PASV. ---> LIST 425 Failed to establish connection.
由於主動模式下須要關閉客戶機的防火牆,加上使用固定端口20傳輸數據易被抓取,被動模式纔是被普遍採用的模式。默認狀態下,vsftpd使用任意非特權端口傳輸數據,這裏給它限定一個範圍7000~800,在配置文件後添加下面兩句。
pasv_enable=yes pasv_min_port=7000 pasv_max_port=8000
並配置防火牆sudo iptables -I INPUT 4 -p tcp --dport 7000:8000 -j ACCEPT' 加載內核模塊(可能不是必選項), ```bash root@remote:/# modprobe ip_conntrack_ftp root@remote:/# modprobe ip_nat_ftp ``` 永久加載須要在
/etc/modules-load.d/modules.conf文件尾添加 ``` ip_conntrack_ftp ip_nat_ftp ``` ftp被動模式鏈接
ftp -p remoteIP####(6)配置SSL安全鏈接 編輯
/etc/vsftpd.conf`去註釋或添加
pam_service_name=vsftpd rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key ssl_enable=YES force_local_data_ssl=NO force_local_logins_ssl=NO require_cert=NO
配置以後:
root@remote:/# cat /etc/vsftpd.conf | grep -v "^#" | grep -v "^$" listen=NO listen_ipv6=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES use_localtime=YES xferlog_enable=YES connect_from_port_20=YES ascii_upload_enable=YES ascii_download_enable=YES ftpd_banner=Welcome to blah FTP service. chroot_local_user=YES secure_chroot_dir=/var/run/vsftpd/empty pam_service_name=vsftpd rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key ssl_enable=YES force_local_data_ssl=NO force_local_logins_ssl=NO require_cert=NO allow_writeable_chroot=YES pasv_enable=yes pasv_min_port=7000 pasv_max_port=8000
參考:
vsftpd doc
vsftpd的主動模式與被動模式
vsftpd被動模式配置
vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()錯誤的解決方法
Installing and configuring FTP server vsftpd.
vsftpd與/bin/false、/sbin/nologin