1 Ftp服務器的安裝
如果之前配置過ftp
服務器,但無法啓動服務,那麼本是配置出現了錯誤,那麼可先完全卸載後再進行安裝。
sudo apt-get install vsftpd
sudo vsftpd -v // 檢查是否安裝
如果安裝會出現:
vsftpd: version 3.0.3
2 Ftp服務器的配置
sudo vim /etc/vsftpd.conf // 修改配置文件
vsftpd.conf
文件如下:
listen=NO //是否開啓監聽ipv4和ipv6數據
listen_ipv6=YES //是否開啓監聽ipv6數據
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO //是否允許匿名登陸,無需密碼
# Uncomment this to allow local users to log in.
local_enable=YES //是否允許本地用戶登錄
# Uncomment this to enable any form of FTP write command.
#write_enable=YES //是否允許登陸者上傳文件, 如果需要上傳文件可開啓
# 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 //設置本地用戶默認要減免的權限
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES //目錄消息,能夠給遠程登陸的用戶發送目錄
#
# If enabled, vsftpd will display directory listings with the time
# in your local time zone. The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES //服務器所展示的目錄將隨着本地時間而改變
#
# Activate logging of uploads/downloads.
xferlog_enable=YES //開啓上傳下載的日誌記錄
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES //確認連接傳輸的端口號爲20
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log //日誌文件存放位置
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES //日誌文件採用標準格式
# You may fully customise the login banner string:
ftpd_banner=Welcome to FTP service. //在使用shell時登陸那麼會發送歡迎語
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
#chroot_local_user=YES //對本地用戶是否實施限制
#chroot_list_enable=YES //開啓限制白名單
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list //白名單路徑,若無這個文件需要自己創建
# This option should be the name of a directory which is empty. Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
# pam_service_name=vsftpd
pam_service_name=ftp //此處ubuntu的系統需要改爲ftp
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES //編碼統一爲utf8編碼,可以識別中文,防止亂碼
local_root=/home/ftpuser //本地用戶默認訪問的目錄
pasv_enable=YES //Ftp服務器的工作模式,此時爲被動模式, 如果設置port_enable=YES,就表示主動模式
pasv_min_port=6000 //在PASV模式下,建立數據傳輸所可以使用port範圍的下界
pasv_max_port=7000 //在PASV模式下,建立數據傳輸所可以使用port範圍的上界,把端口範圍設在比較高的一段範圍內,比如50000-60000,將有助於安全性的提高。
補充:(未出現在vsftpd.conf
配置文件中的常用參數)
guest_enable=YES
guest_username=ftp
guest用戶名,即登陸不是匿名用戶的用戶,具有guest用戶身份
local_root=/var/ftp
anon_root=/var/ftp
以上兩個選項爲本地用戶和匿名用戶默認訪問的目錄
#pasv_enable=YES
#port_enable=YES
以上兩個選項是FTP服務器的工作模式,兩者只能出現一個,而且另一個必須註釋掉。
use_localtime=YES
是否使用本機時間,若設置NO時,僅使用格林尼治時間。由於北京時間和格林尼治時間有8小時時差,所以建議設置爲YES
Idle_session_timeout=300
客戶端若在300秒之內沒有任何操作,則服務器自動斷開。
max_clinet=0
最大連接數量(stand-alone模式下)
max_per_ip=0
每個客戶端最大連接ftp服務器的連接數
local_max_rate=0
本地用戶登陸FTP服務器最大傳輸速率,單位爲字節/秒
anon_max_rate=0
匿名用戶登陸FTP服務器最大傳輸速率,單位爲字節/秒
allow_writeable_chroot=YES
解決500 OOPS: vsftpd: refusing to run with writable root inside chroot (),也可以使用命令sudo chmod a-w /home/user去除用戶主目錄的寫權限,注意把目錄替換成你自己的
3 添加並設置Ftp用戶
sudo mkdir /home/ftpuser
sudo useradd ftpuser -d /home/ftpuser -m
sudo passwd ftpuser
sudo chmod 777 -R /home/ftpuser
sudo usermod -s /sbin/nologin ftpuser
4 開啓或重啓vsftp服務
sudo service vsftpd stop //停止vsftpd服務
sudo service vsftpd start //開啓vsftpd服務
sudo service vsftpd restart //重啓vsftpd服務
5 服務器開啓Ftp端口範圍
如果設置pasv_min_port
與pasv_max_port
,需要在服務器配置一下安全組的入口規則,否則通過瀏覽器的ftp
請求是無法訪問的。注意端口號要和自己設置的範圍保持一直,圖片中顯示沒有一致。
-
阿里雲服務器配置安全組
-
騰訊雲服務器配置安全組
6 Ftp服務器測試
-
瀏覽器訪問
-
windows資源管理器訪問
-
windows終端cmd訪問
7 遇到的各種坑
有的猿友可能會遇到奇葩的問題,我個人都遇到過(浪費三天時間),同樣的部署在不同類型的服務器部署,會存在被動模式死活連接不上的問題。這是因爲如果購買的服務器使用的是專有網絡,是不區分公網和內網,修改安全組也沒有這個區分選項,但採用經典網絡的是由公網和內網的區分,所以就會存在這樣的差異。比如:
分析之後發現,採用ftp被動模式登錄成功,服務端映射給客戶端的ip地址是私有的,所以客戶端無法和服務器建立連接關係,也就意味着ftp命令無法使用,只能採取主動模式。解決方案如下:
listen=NO ===》 修改爲listen=YES
listen_ipv6=YES ===》 修改爲listen_ipv6=NO
pasv_address=111.231.34.12 // 說明一定要是公網IP,如果不添加,返回被動模式的ip是0.0.0.0