Centos7搭建FTP、匿名和虛擬用戶訪問和各種報錯

安裝配置

安裝

xshell連接服務器,下載安裝vsftpd

[root@VM_5_17_centos ~]# yum -y install vsftpd

匿名喲靈活-不能刪除修改

vsftpd.conf
vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
# 若配置成000,則權限drwxr-xr-x -> drwxrwxrwx,匿名模式關係不大
anon_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
# 修改、刪除權限
anon_other_write_enable=NO
# 匿名、虛擬的目錄 
anon_root=/data/vsftpd/root/anon

local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
目錄權限

根目錄755,下面的數據data目錄777權限

chmod -R 755 /data/vsftpd/root/anon
[root@VM_5_17_centos anon]# mkdir /data/vsftpd/root/anon/data
[root@VM_5_17_centos anon]# chmod -R 777 /data/vsftpd/root/anon/data

根目錄只能下載,但/data/vsftpd/root/anon/data可以上傳、修改下載;

防火牆策略
firewall-cmd --zone=public --add-port=20/tcp --permanent
firewall-cmd --zone=public --add-port=21/tcp --permanent
被動模式

默認的主動模式,開了防火牆後還是不行,所有嘗試被動模式

# 配置文件添加
port_enable=NO
pasv_enable=YES
pasv_min_port=65100
pasv_max_port=65200
// 開放端口號,再重啓防火牆、vsftpd
[root@VM_5_17_centos anon]# firewall-cmd --zone=public --add-port=65100-65200/tcp --permanent
[root@VM_5_17_centos anon]# firewall-cmd --reload
[root@VM_5_17_centos anon]# firewall-cmd --list-ports
20/tcp 21/tcp 65100-65200/tcp
[root@VM_5_17_centos anon]# systemctl stop vsftpd
[root@VM_5_17_centos anon]# systemctl start vsftpd

虛擬用戶-可刪除修改

配置文件添加

# 虛擬用戶
chroot_local_user=YES
ascii_upload_enable=YES
ascii_download_enable=YES
guest_enable=YES
guest_username=vsftpd
user_config_dir=/etc/vsftpd/userconf
allow_writeable_chroot=YES
# 虛擬用戶刪除、修改權限
chmod_enable=YES
virtual_use_local_privs=YES

其他修改

[root@VM_5_17_centos /]# useradd vsftpd -d /data/vsftpd/root/ -s /bin/false
[root@VM_5_17_centos /]# vi /etc/vsftpd/loginusers.conf

vsftpd1
123456

[root@VM_5_17_centos userconf]# db_load -T -t hash -f /etc/vsftpd/loginusers.conf /etc/vsftpd/loginusers.db
[root@VM_5_17_centos userconf]# chmod 600 /etc/vsftpd/loginusers.db
[root@VM_5_17_centos /]# vi /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       sufficient   /lib64/security/pam_userdb.so db=/etc/vsftpd/loginusers
account    sufficient   /lib64/security/pam_userdb.so db=/etc/vsftpd/loginusers

[root@VM_5_17_centos /]# mkdir /etc/vsftpd/userconf
[root@VM_5_17_centos /]# vi /etc/vsftpd/userconf/vsftpd1

local_root=/data/vsftpd/root/anon
write_enable=YES

重啓

systemctl restart vsftpd

日誌

# 日誌
use_localtime=YES
dual_log_enable=YES
vsftpd_log_file=/data/vsftpd/log/vsftpd.log
## 上傳下載日誌
xferlog_enable=YES
xferlog_std_format=YES
xferlog_file=/data/vsftpd/log/xferlog

試驗

文件夾中分別用ftp://ip/、ftp://vsftpd1:123456@ip/登錄
最終用戶名登錄的可以刪除、修改文件夾

其他

用戶類型

  • 匿名用戶。名爲anonymous
  • 本地用戶
  • 虛擬用戶。安全,FTP服務器的專有用戶,只能訪問FTP服務器所提供的資源

被動與主動模式

  • PORT主動。客戶端向服務端連接後,客戶端在本地打開了一個端口在等着服務器連接,數據連接通了。要開啓21和20端口。
  • PASV被動。客戶端向服務端連接後,服務端在本地打開了一個端口在等着客戶端連接,數據連接通了。要開啓21端口和數據端口範圍。
# 開啓被動模式
port_enable=NO
pasv_enable=YES
pasv_min_port=6000
pasv_max_port=6100
// 防火牆策略
firewall-cmd --zone=public --add-port=6000-6100/tcp --permanent

應該默認的是主動模式

默認目錄:/var/ftp

umask、local_umask

  • umask=022。文件夾777-022=755,文件的話,則是用 666-022=644.
  • umask=077。新建的目錄 權限是700,文件的權限時 600
  • 默認情況下vsftp上傳之後文件的權限是600,目錄權限是700。本地用戶local_umask,虛擬用戶anon_umask

常用命令

systemctl restart/start/stop vsftpd
systemctl status vsftpd -l
# 修改配置文件
vim /etc/vsftpd/vsftpd.conf

local_root=/var/www/html
chroot_local_user=YES
anon_root=/var/www/html

注:local_root 針對系統用戶;anon_root 針對匿名用戶。

重新啓動服務:

service vsftpd restart
任何一個用戶ftp登錄到這個服務器上都會chroot到/var/www/html目錄下。

報錯

1.centos7 550 create directory operation failed

vim /etc/selinux/config

# 關閉SELINUX
SELINUX=disabled
// 給目錄賦予權限
chmod -R 777 目錄

2.Job for vsftpd.service failed because the control process exited with error code. See “systemctl status vsftpd.service” and “journalctl -xe” for details.

原因比較多,systemctl status vsftpd -l查看詳細

情況1:unrecognised variable in config file: nonymous_enable
[root@VM_5_17_centos network-scripts]# systemctl status vsftpd -l
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2019-09-24 09:53:07 CST; 6s ago
  Process: 26014 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=2)
 Main PID: 2535 (code=killed, signal=TERM)

Sep 24 09:53:07 VM_5_17_centos systemd[1]: Starting Vsftpd ftp daemon...
Sep 24 09:53:07 VM_5_17_centos systemd[1]: vsftpd.service: control process exited, code=exited status=2
Sep 24 09:53:07 VM_5_17_centos vsftpd[26014]: 500 OOPS: unrecognised variable in config file: nonymous_enable
Sep 24 09:53:07 VM_5_17_centos systemd[1]: Failed to start Vsftpd ftp daemon.
Sep 24 09:53:07 VM_5_17_centos systemd[1]: Unit vsftpd.service entered failed state.
Sep 24 09:53:07 VM_5_17_centos systemd[1]: vsftpd.service failed.

nonymous_enable無法識別,應該爲anonymous_enable;

情況2:can only support ipv4 and ipv6 currently
[root@VM_5_17_centos network-scripts]# systemctl status vsftpd -l
...
Sep 24 09:53:56 VM_5_17_centos vsftpd[26102]: 500 OOPS: can only support ipv4 and ipv6 currently
...

listen=、listen_ipv6需要有一個參數爲YES

3.開啓了匿名模式,但訪問一直提示輸入用戶及密碼

情況1 原因參數不對造成,報錯配置
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

多加了anon_umask=022就成功了

# 多加了下面參數
anon_umask=022
# anon_upload_enable=YES
# anon_mkdir_write_enable=YES
# anon_other_write_enable=YES

# 設置匿名目錄後也不成功
# anon_root=/data1/vsftpd/root/annon
情況2 把根目錄/var/ftp權限不對

用chmod命令將數據根目錄權限改爲755

文件夾登錄上傳 部分文件名亂碼問題

用xftp上傳沒問題

// 下面行不通,僅做記錄
[root@VM_5_17_centos data]# yum -y groupinstall chinese-support
[root@VM_5_17_centos data]# echo $LANG
[root@VM_5_17_centos data]# vim /etc/locale.conf

LANG=zh_CN.utf8
# LANG=en_US.utf8
# LANG="zh_CN.GB18030" 
# LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN" 
# SUPPORTED="zh_CN.UTF-8:zh_CN:zh:en_US.UTF-8:en_US:en" 
# SYSFONT="lat0-sun16"

[root@VM_5_17_centos data]# vim /etc/sysconfig/i18n

LANG="zh_CN.utf8"
LC_ALL="zh_CN.utf8"

4.開防火牆後,客戶端連接超時ERR_CONNECTION_TIMED_OUT

默認配置

# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
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
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=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
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# 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 change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# 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
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章