Last Update: 2023-09-03
- mode
- networking
- network resource
- timeout
- authenticate
- chroot configure
- userlist
- local_user
- limit speed
- per user config
- anonymous
- log
- xferlog
- message
- ASCII MODE
- Security Setting
- Other Configure
- SSL Setting
- My Setting
- Virtual User
- Troubleshoot
- Doc
Check version
vsftpd -v
vsftpd: version 3.0.2
我的設定
以下是小弟用了幾年的設定, 希望對大家有參考價值 ^, ^
P.S.
在 Centos 6 的設定上, "=" 的兩邊不可以有空格
################### My VSFTPD CONFIG FILE ############
###
### 有關目錄及檔案
###
# /etc/vsftpd.conf
# vsftpd的主要設定檔
# /etc/vsftpd_user_conf
# 存放個別用戶設定檔的目錄, 設定檔檔案與用戶名相同.
# 它是由 user_config_dir 去指定位置的
# 此功能相當強大, 不容忽視 !!!
# 沒有效的 user_config 設定如下:
# listen_address, banner_file, max_per_ip, max_clients, xferlog_file
# /etc/vsftpd.chroot_list
# 用來設定 (准許/限制) 離開 (家目錄/特定目錄) 的使用者名單
# 效果受 chroot_local_user 影響 (詳見下文)
# /etc/vsftpd.user_list
# 用來設定 (能 或 不能) 用 ftp 的本地用戶名單
# 如果那用戶不能用 ftp 的話, 那會在詢問密碼前就出現失敗訊息
# 效果受 userlist_deny 影響 (詳見下文)
# 修改後不用 reload vsftpd
# /etc/ftpusers
# 不能用 ftp 的本地用戶名單, login 時仍會問密碼, 但就算密碼中都唔可以登入.
# 當 ftpusers, user_list 同時存在時, 那最後處理的是 ftpusers
# /var/www/datahunter.org
# 本地用戶 chroot 的目錄, 我是用它來存放網頁的 ^^
# !!! 注意 !!!
# 在等號兩邊不能有空格
# 井號不可用在設定項值後 ( 亦即每注解都要新的一行, 不可在行尾 )
#################################
# mode
#################################
# Default: NO
# YES => 以 standalone 的形式運行
listen=YES
# Default: NO
# 不霸佔當前的 shell , 用 'ps -e' 會看到進程,
# 與 listen=YES 不相溶
background=NO
# Default: NO
# 在 shell 執行 server 時才有用
run_as_launching_user=no
#################################
# networking
#################################
# 不啟用 IPv6
listen_ipv6=NO
# 只監聽 192.168.1.1 這 ip (當有多 IP 時用)
listen_address=192.168.1.1
# 命令通道的 port
listen_port=21
# 啟動主動連線的 port 20
port_enable=YES
connect_from_port_20=YES
# 如果不是用 port 20, 就用 ftp_data_port 去修改它
# PASV Port
pasv_enable=YES
pasv_min_port=9001
pasv_max_port=9050
# pasv mode 的相關設定, 找出 datahunter.org 的 extenal ip address
pasv_addr_resolve=YES
pasv_address=datahunter.org
#################################
# network resource
#################################
# 每粒 ip 只可有 3 條連線 Default: 0 (unlimited)
max_per_ip=3
# 另外本機同時只接受 8 個帳戶登入 (只在 standalone mode 有效). Default: 0 (unlimited)
# 至於單一個帳戶的登入次數限制, 則由 PAM 的來完成.
# (/etc/security/limits.conf 的 maxlogins)
max_clients=8
#################################
# timeout
#################################
idle_session_timeout=300
accept_timeout=30
connect_timeout=30
data_connection_timeout=120
# 使用者在 300s 內都沒有命令動作, 則強制離線!
# passive port 等待 client 超過 30s 仍未有連線, 那就強制斷線
# 響應PORT 方式的數據連線的逾時設定, 多於 30s 就斷
# 數據連接空閒 120s 就斷
#################################
# authenticate
#################################
# 驗證方式
pam_service_name=vsftpd
# login 時要 5s 後才報告 login 失敗
delay_failed_login=5
max_login_fails=3
# 所有的非匿名(本地用戶)登入者都視為 guest
# Default: NO
guest_enable=no
# 定義guest 的使用者名稱, (Default: ftp)
guest_username=ftp
#################################
# chroot configure
#################################
# 是否啟用 chroot
# Default: NO
chroot_local_user=YES
# Yes => chroot_list 內的帳戶不會被限制在 chroot 目錄內
# Default: NO
chroot_list_enable=YES
# 更新後直接有效, 不用 reload vsftpd
# Default: /etc/vsftpd.chroot_list
chroot_list_file=/etc/vsftpd.chroot_list
# chroot 的地方是根據 passwd 的 home directory 而定
# "/backup/./upload/" => chroot 在 /backup 內, 而不是 /backup/upload 內
passwd_chroot_enable=Yes
#############################
# userlist
#############################
userlist_enable=YES userlist_file=/etc/vsftpd.user_list userlist_deny=NO
# 啟用 userlist 功能
# "userlist_deny=NO" => 只有在 vsftpd.user_list 內的帳戶才可登入 ftp server
# "userlist_deny=YES => vsftpd.user_list 內的帳戶 "不可以" 登入 ftp
#################################
# local_user
#################################
# 啟用 /etc/passwd 的帳戶
local_enable=YES
# 本地帳戶是否需要有有效的 shell 才比登入 (Default: YES) <-- 在 /etc/shells 內的 shell (valid user shell)
# This option only has an effect for non-PAM builds of vsftpd.
check_shell=YES
FTP only 的 valid shell
grep shells /etc/pam.d/vsftpd
auth required pam_shells.so
解決方法
cp -a /bin/false /bin/ftpshell
echo "/bin/ftpshell" >> /etc/shells
usermod ftpgetbak -s /bin/ftpshell
P.S.
false 與 nologin 的分別
# /bin/false - Exit with a status code indicating failure.
# /usr/sbin/nologin - displays a message that an account is not available and exits non-zero.
# 本地用戶可以刪除及修改文件
write_enable=YES
# 設定 user upload 完 file 後那 file 的 permission
local_umask=007
# Umasks are applied on top of this value.
file_open_mode=666
# Default: YES
chmod_enable=YES
#################################
# limit speed
#################################
# local 帳戶的速度限制 (bytes/sec). 它是 per connection 的.
# Default: 0 (unlimited)
# 當限速是 1M 時, 4 個 connection 同時下載就會有 4M
# 1 Mbyte/s
local_max_rate=1048576
# anon 帳戶的速度限制
anon_max_rate=0
#################################
# per user config
#################################
# 存放個別用戶設定的目錄
# 在目錄內一個帳戶一個檔案, 用以用戶名作檔案名,
user_config_dir=/etc/vsftpd/per_user_conf
* 更改設定後, 不用 reload vsftpd, 新 settings 會套用在新 session
* 無效設定:
- listen_address,
- banner_file,
- max_per_ip,
- max_clients,
- xferlog_file,
- ...
* FileZilla stop 了 "Process Queue" 後仍有連線
Checking on Server
ps aux | grep vsftpd | grep USERNAME USERNAME 14508 0.2 0.0 78504 1368 ? S 13:17 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
Filezilla GUI MSG
Error: File transfer aborted by user after transferring 99,614,720 bytes in 93 seconds # 一段時間後 Status: Disconnected from server
#################################
# anonymous
#################################
anonymous_enable=NO
# 不允許匿名登陸
anon_upload_enable=yes
# 放上傳權限
anon_mkdir_write_enable=NO
# 不可創建目錄
anon_other_write_enable=NO
# 不能刪除目錄及檔案
anon_world_readable_only=yes
# 只可 download 抱有者係 ftp 或 777 權限 的檔案
no_anon_password=YES
#anon_root=
#chown_uploads=YES
#chown_username=whoever
# 所有匿名上傳數據的擁有者將被更換
#################################
# log
#################################
使用 log 設定時的考慮次序如下
- dual_log_enable
- syslog_enable
- xferlog_enable
Notes
由於 dual_log_enable, syslog_enable, xferlog_enable 的 Default 都是 NO,
所以 Default vsftpd 是沒有寫 log 的.
如果係用 System A/C Login (/etc/passwd),
那就會使用 pam_unix 去 log (/var/log/secure).
dual_log_enable
# 這是指同時使用 /var/log/vsftpd.log 及 /var/log/xferlog 這兩個 log 的"機制"
# Default: NO
dual_log_enable=NO
syslog_enable
# YES: 去 /var/log/vsftpd.log 的 log 會到左 rsyslog 的 FTPD facility <-- (ftp.*)
# Default: NO
syslog_enable=YES
* syslog_enable 優先於 xferlog_enable, 所以當 "syslog_enable=YES" 時, 那 xferlog_enable 是沒有效果的
* syslog_enable 決定了 log 去 vsftpd.log 還是去 syslog
xferlog_enable
# Default: NO
xferlog_enable=YES
當 "syslog_enable=YES" 時, xferlog_enable 就沒有意義了
當 "xferlog_enable=YES" 時, login 資訊會到 /var/log/secure
# Log format
# YES = 用 standard xferlog format => used by wu-ftpd => log 到 xferlog_file <= (只 log UPLOAD 及 DOWNLOAD)
# NO (Default) = vsftpd style log file => Format 清楚好睇好多 => log 到 vsftpd_log_file <= ( 會 log CONNECT, UPLOAD, DELETE, DOWNLOAD )
xferlog_std_format=NO
# 兩個 log file 的 Default 位置
vsftpd_log_file=/var/log/vsftpd.log
xferlog_file=/var/log/xferlog
# log 中文會亂碼
vsftp ( 2.2.2 左右 )自身的 log 機制顯示中文是會亂碼的 ...
i.e.
"/public_html/??????.jpg"
解決: 改用 syslog
syslog_enable=yes
/etc/rsyslog.conf
ftp.* -/var/log/vsftpd.log
Remark
service rsyslog reload
在 /etc/logrotate.d/syslog 加入
/var/log/vsftpd.log
P.S.
/var/log/vsftpd.log 會在 ftp connect 一次後出現
column:
current-time
transfer-time # is the total time in seconds for the transfer.
remote-host
file-size
filename
transfer-type # a (for an ascii transfer), b (for a binary transfer)
special-action-flag # "_" no action was taken
direction # o outgoing(download), i incoming(upload), d deleted
access-mode # a (anonymous), g (guest), r (real)
username
service-name # ftp
authentication-method # 0 none, 1 RFC931 Authentication
authenticated-user-id # * is used if an authenticated user id is not available
completion-status # c complete transfer, i incomplete transfer
upload log exmaple:
Thu Jun 25 10:26:03 2015 166 x.x.x.x 80294728 /folder/file.zip b _ i r TestUser ftp 0 * c Thu Jun 25 10:29:05 2015 189 x.x.x.x 86504160 /folder/file.zip b _ i r TestUser ftp 0 * c
#################################
# message
#################################
# 切換目錄時, 是否顯示目錄下 "message_file" 的內容
dirmessage_enable=YES
message_file=.msg
# login 時顯示歡迎話語 "lamp FTP service"
# banner_file 是以檔案形式儲存歡迎話語
ftpd_banner= lamp FTP service.
#################################
# ASCII MODE
#################################
# 默認情況下, VSFTPD 是禁止使用ASCII傳輸模式
ascii_upload_enable=NO ascii_download_enable=NO
#################################
# Security Setting
#################################
hide_ids=no
# 用ls -al 之類的指令,所看到的檔案擁有者跟群組均為ftp
# default: no
text_userdb_names=YES
# 以字串代替數字的用戶名
# default: no
ls_recurse_enable=yes
force_dot_files=YES
# Default: NO
# 當 yes 時, 用戶可以看見以 "." 為首的目錄及檔案
tcp_wrappers=YES
# 受 /etc/hosts.allow 及 /etc/hosts.deny 影響
virtual_use_local_privs=no
# 虛擬用戶只有 anonymous 權限
pasv_promiscuous=YES
port_promiscuous=YES
# YES 時, 將關閉 pasv/port 模式的安全檢查
# 安全檢查=檢查確保數據連接和控制連接是來自同一個IP地址
# 默認值為NO
ftp_username=ftp
# (default: ftp)
#nopriv_user=
# 一個在本系統上沒有權限的用戶 (default: nobody)
#secure_chroot_dir=
# 不個 ftp 用戶的入不到的空目錄 (default: /var/run/vsftpd)
#################################
# Other Configure
#################################
# 'ps -e' 時會顯示每個進程的 ip 地址
setproctitle_enable=YES
# 隱藏特定的目錄及檔案
hide_file={*.org, *.~, *.private}
# 見到但觸不到的目錄及檔案
deny_file={*.~, *.bak}
# NO => Login 得會快 D
# transform the ip address into the hostname, before pam authentication.
# Default: YES
reverse_lookup_enable=NO
# If true, any failed upload files are deleted. Default: NO
delete_failed_uploads=YES
# Default: GMT
use_localtime=YES
* 用 syslog 不用考慮此問題, 因為 syslog 本身會 write 時間
SSL Setting
- ftps port 990 #
- sftp port 21 # explicit FTP over TLS
Configure SSL with vsftpd
1. Create Cert
mkdir -p /etc/vsftpd/ssl
cd /etc/vsftpd/ssl
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout vsftpd.key -out vsftpd.crt
chmod 440 /etc/vsftpd/ssl/vsftpd.key
2. /etc/vsftpd.conf
# 設定 Certicate
rsa_cert_file=/etc/vsftpd/ssl/vsftpd.crt rsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key
# 設定什麼人可以用 SSL
ssl_enable=YES allow_anon_ssl=NO
# 強制本地用戶要用 ssl 來 login , 及用 ssl 來傳送資料
# Response: 530 Non-anonymous sessions must use encryption.
force_local_logins_ssl=YES force_local_data_ssl=YES
# 只比用 tls 加密
ssl_tlsv1=YES ssl_sslv2=NO ssl_sslv3=NO
# 加密程度
ssl_ciphers=HIGH
# Default: YES
# all SSL data connections are required to exhibit SSL session reuse
# it may break many FTP clients
require_ssl_reuse=NO
3. 設定好後, restart service 即得
service vsftpd restart
Troubleshoot
Error 1:
Received TLS alert from the server: Handshake failed (40)
Fix 1:
# Default: DES-CBC3-SHA
# HIGH / MEDIUM / LOW
# manual of ciphers (part of openssl)
ssl_ciphers=HIGH
seccomp_sandbox - U14.04
telnet 127.0.0.1 21
Output
500 OOPS: priv_sock_get_cmd Connection closed by foreign host.
原因:
seccomp filter sanboxing with vsftpd 3.0.x
更改 setting
seccomp_sandbox=NO
My Setting
listen=YES
listen_ipv6=NO
connect_from_port_20=YES
pasv_enable=YES
pasv_min_port=9000
pasv_max_port=9049
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
userlist_enable=YES
userlist_deny=NO
chroot_local_user=YES
allow_writeable_chroot=YES
dirmessage_enable=YES
# log
syslog_enable=YES
max_clients=8
idle_session_timeout=300
delay_failed_login=5
pam_service_name=vsftpd
tcp_wrappers=YES
delete_failed_uploads=YES
# Centos 6 才有這 setting
reverse_lookup_enable=NO
Troubleshoot
Debug Protocol
setting
log_ftp_protocol=YES
log
====================================================== Client Status: Retrieving directory listing of "/"... Status: Directory listing of "/" successful ====================================================== Server Sep 7 17:43:48 mail vsftpd[1472]: [T-Sales] FTP command: Client "x.x.x.x", "PASV" Sep 7 17:43:48 mail vsftpd[1472]: [T-Sales] FTP response: Client "x.x.x.x", "227 Entering Passive Mode (x,x,x,x,35,57)." Sep 7 17:43:48 mail vsftpd[1472]: [T-Sales] FTP command: Client "x.x.x.x", "LIST -a" Sep 7 17:43:48 mail vsftpd[1472]: [T-Sales] FTP response: Client "x.x.x.x", "150 Here comes the directory listing." Sep 7 17:43:48 mail vsftpd[1472]: [T-Sales] FTP response: Client "x.x.x.x", "226 Directory send OK." ..... Sep 7 17:43:14 mail vsftpd[1472]: [T-Sales] FTP command: Client "x.x.x.x", "CWD Product Window (第四期)" Sep 7 17:43:14 mail vsftpd[1472]: [T-Sales] FTP response: Client "x.x.x.x", "550 Failed to change directory."
當 ls "Product Window (第四期) " 所在的 Folder 有 "?" 時, 那 CWD 會失敗
Issue 1: start without listen port
Fix 1:
/etc/vsftpd.conf
background=NO
# Check config file
vsftpd /etc/vsftpd.conf
500 OOPS: unrecognised variable in config file: reverse_lookup_enable
Issue 2: U16 local user login fail
Fix 2:
pam_service_name=ftp
Issue 3:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
原因:
users root directory is writable
解決
[1]
chmod a-w /home/user
[2]
# version 3
allow_writeable_chroot=YES
[3]
# Specifies the directory vsftpd changes to after a local user logs in. Default: no value
local_root=/home
[4]
# based on the occurrence of the "/./" in the home directory field, Default: NO
passwd_chroot_enable=yes
/etc/passwd
home field:
/backup/upload/ -> /backup/./upload/
那 User 會被 chroot 在 /backup 內, 而不是 /backup/upload !!
Doc
man 5 vsftpd.conf