vsftpd搭建FTP服务器 - 虚拟用户

90 篇文章 1 订阅

命令记录

$ sudo apt install vsftpd db-util
$ sudo nano /etc/vsftpd.conf
$ sudo nano /etc/vsftpd/vsftpd-virtual-users.txt
$ sudo db_load -T -t hash -f /etc/vsftpd/vsftpd-virtual-users.txt /etc/vsftpd/vsftpd-virtual-users.db
ls /etc/vsftpd/vsftpd-virtual-users.db -l
-rw-r--r-- 1 root root 12288  7月 24 09:00 /etc/vsftpd/vsftpd-virtual-users.db
sudo chmod 600 /etc/vsftpd/vsftpd-virtual-users.db
ls /etc/vsftpd/vsftpd-virtual-users.db -l
-rw------- 1 root root 12288  7月 24 09:00 /etc/vsftpd/vsftpd-virtual-users.db
$ ls /etc/pam.d/vsftpd
/etc/pam.d/vsftpd
$ sudo nano /etc/pam.d/vsftpd
$ ls /etc/vsftpd/vusers
$ sudo mkdir /etc/vsftpd/vusers
mkdir: 无法创建目录 "/etc/vsftpd/vusers": 文件已存在
$ sudo nano /etc/vsftpd/vusers/ftpuser1
$ sudo mkdir -p /home/ftp/ftpuser1
$ ls -l /home/ftp
总计 4
drwxr-xr-x 2 root root 4096  7月 24 09:12 ftpuser1
$ sudo chmod -R 755 /home/ftp
$ ls -l /home/ftp
总计 4
drwxr-xr-x 2 root root 4096  7月 24 09:12 ftpuser1
$ sudo chown -R ftp:ftp /home/ftp
$ ls -l /home/ftp
总计 4
drwxr-xr-x 2 ftp ftp 4096  7月 24 09:12 ftpuser1
$ sudo chmod a-w /home/ftp/ftpuser1
$ ls -l /home/ftp
总计 4
dr-xr-xr-x 2 ftp ftp 4096  7月 24 09:12 ftpuser1
$ sudo mkdir /home/ftp/ftpuser1/upload
$ sudo chown ftp:ftp /home/ftp/ftpuser1/upload
$ ls -l /home/ftp/ftpuser1
总计 4
drwxr-xr-x 2 ftp ftp 4096  7月 24 09:34 upload
$ sudo chmod 755 /home/ftp/ftpuser1/upload
$ ls -l /home/ftp/ftpuser1
总计 4
drwxr-xr-x 2 ftp ftp 4096  7月 24 09:34 upload
$ sudo systemctl restart vsftpd

/etc/vsftpd.conf文件:

# Example config file /etc/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.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
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.
listen_ipv6=YES
#
# 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
#
# 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
#
# 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
#
# 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/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 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.
# 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 restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
chroot_local_user=YES
#
# 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
#
# Customization

# Enable virtual users
guest_enable=YES
guest_username=ftp

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers

#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# 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
#
# 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

allow_writeable_chroot=YES
virtual_use_local_privs=YES

/etc/pam.d/vsftpd文件

# Standard behaviour for ftpd(8).
#auth	required	pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.

# Standard pam includes
#@include common-account
#@include common-session
#@include common-auth
#auth	required	pam_shells.so

# 添加独立用户数据库的配置
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users

/etc/vsftpd/vsftpd-virtual-users.txt文件

ftpuser1
password1
ftpuser2
password2

/etc/vsftpd/vusers/ftpuser1文件

local_root=/home/ftp/ftpuser1
write_enable=YES

如何在ubuntu22.04系统上搭建一个ftp服务器,用户名不使用系统用户名,用户名的创建类似filezilla服务器,可以直接在ftp服务器创建用户。

在Ubuntu 22.04 上搭建一个FTP服务器,并实现FTP用户独立于系统用户的方式,实现类似FileZilla服务器的功能,可以使用`vsftpd`和`db-util`工具。以下是详细步骤:
安装vsftpd和db-util
首先,安装`vsftpd`(非常安全的FTP程序)和`db-util` (用于创建独立的FTP用户数据库):
sh
sudo apt update
sudo apt install vsftpd db-util
配置vsftpd
配置`vsftpd`,允许使用独立的FTP用户:
1. 打开vsftpd配置文件:
sh
sudo nano /etc/vsftpd.conf
2. 修改或添加以下配置项:
sh
# Enable anonymous login (disable if not needed)
anonymous_enable=NO

# Enable local user login
local_enable=YES

# Enable write permissions
write_enable=YES

# Enable local user chroot
chroot_local_user=YES

# Enable virtual users
guest_enable=YES
guest_username=ftp

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers

# Use PAM service for authentication
pam_service_name=vsftpd
3. 保存并退出编辑器(在Nano中,按 CTRL+X,然后按 Y 确认保存,最后按 Enter 退出)。
创建独立的FTP用户数据库
1. 首先,为虚拟用户创建数据库文件:
sh
sudo nano /etc/vsftpd/vsftpd-virtual-users.txt
2. 输入虚拟用户名和密码:
plaintext
ftpuser1
password1
ftpuser2
password2
3. 保存并退出编辑器。
4. 生成db文件:
sh
sudo db_load -T -t hash -f /etc/vsftpd/vsftpd-virtual-users.txt /etc/vsftpd/vsftpd-virtual-users.db
5. 确保db文件权限为`root`:
sh
sudo chmod 600 /etc/vsftpd/vsftpd-virtual-users.db
配置PAM文件
1. 创建或修改PAM文件以使用新的用户数据库:
sh
sudo nano /etc/pam.d/vsftpd
2. 确保文件内容如下:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
3. 保存并退出编辑器。
配置虚拟用户个性化目录
1. 创建用户配置目录:
sh
sudo mkdir /etc/vsftpd/vusers
2. 为每个FTP用户创建个人配置文件,例如`ftpuser1`:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
3. 添加文件内容,指定用户的个性化配置:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
4. 保存并退出编辑器。
创建FTP目录结构
1. 创建绑定用户名的主目录,并配置权限:
sh
sudo mkdir -p /home/ftp/ftpuser1
sudo chmod -R 755 /home/ftp
sudo chown -R ftp:ftp /home/ftp
重启vsftpd服务
sh
sudo systemctl restart vsftpd
测试FTP连接
现在可以通过FTP客户端连接FTP服务器,并使用你在`vsftpd-virtual-users.txt`中配置的用户名和密码进行登录。
如果需要其他奇百或其他疑问,请随时提问。 (已编辑) 

PAM文件是什么?

PAM (Pluggable Authentication Modules,可插拔认证模块) 是一组共享库,它为 Linux 和 Unix 系统中的身份验证任务提供了灵活和可扩展的机制。 PAM 允许系统管理员通过配置文件定制用户身份验证的方式,而无需修改程序源代码。
PAM 文件用途
PAM 文件的主要功能包括:
1. 身份验证 (Authentication):验证用户身份。例如,密码校验、使用双因素认证。
2. 账户管理 (Account Management):控制帐号的访问权限。例如,用户账户的有效期检查。
3. 会话管理 (Session Management):管理用户会话的设置。例如,自动挂载用户主目录。
4. 密码管理 (Password Management):处理用户密码的修改和强制多因素认证。
PAM 配置文件的位置
PAM 配置文件通常位于`/etc/pam.d/目录下,每个需要身份验证的服务都有对应的配置文件。例如,对于FTP服务,通常对应一个名为vsftpd`的文件。
典型的 PAM 配置文件示例
例如,之前提到的 /etc/pam.d/vsftpd 文件,示例如下:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
含义解释如下:
- auth:这行用于用户身份认证,`required` 表示该行必须被成功验证。
- account:这行用于账户管理,同样使用 required。
- pam_userdb.so:这是 PAM 模块,它支持基于用户数据库的认证。
- db=/etc/vsftpd/vsftpd-virtual-users:指定要使用的用户数据库文件。
PAM 模块基本组件
1. 模块类型:
    - auth:负责验证用户身份。
    - account:负责检查用户的账户状态,如账户是否过期。
    - password:用于更新用户密码。
    - session:管理和设置会话,例如登录时的环境设置。
2. 控制标志:
    - required:此模块必须成功才能继续,但即使失败也会继续执行后续模块。
    - requisite:此模块必须成功,如果失败,则立即返回失败。
    - sufficient:此模块成功则立即返回成功,不再执行后续模块;失败则继续。
    - optional:此模块的成功或失败无关紧要,除非没有其他“必需”的模块。
3. 模块路径和参数:
    - 通常是某个具体的 PAM 模块及其配置参数,例如:`pam_unix.so` 或 pam_userdb.so。
总结
PAM 文件通过定义一系列规则和模块来配置系统中各个应用程序的身份验证方式。通过灵活运用 PAM,系统管理员可以很好地管理系统的安全策略并满足不同的安全需求。在设置`vsftpd`或其他服务时,理解和正确配置 PAM 文件是至关重要的。
如有进一步疑问,请随时提问。 (已编辑) 

# Enable virtual users
guest_enable=YES
guest_username=ftp
为什么这样设置能够使能虚拟用户?

vsftpd(非常安全的FTP守护程序)在配置上支持虚拟用户,这允许您使用不在系统用户数据库中的用户来登录FTP服务器。通过配置文件中的以下选项可以启用虚拟用户功能。
配置选项解释
1. guest_enable=YES
    
sh
    guest_enable=YES
    
    启用`guest_enable`选项后,`vsftpd`将所有通过PAM认证的用户都视为“访客用户”。
2. guest_username=ftp
    
sh
    guest_username=ftp
    
    将虚拟用户映射到实际系统用户名。设置`guest_username`为`ftp`意味着所有已认证成功的虚拟用户将以系统用户`ftp`的身份运行。
虚拟用户工作原理
1. 身份验证步骤:
    - 当一个用户尝试登录FTP服务器时,`vsftpd`通过PAM(可插拔认证模块)检查用户凭据。它尝试通过`/etc/pam.d/vsftpd`中指定的PAM配置文件进行验证。
2. 虚拟用户映射:
    - 如果用户凭据通过认证并且`guest_enable`设置为`YES`,那么这个用户将被视为“访客用户”。
    - guest_username指定所有虚拟用户被映射成哪一个实际系统用户,在本例中是`ftp`用户。因此,所有虚拟用户实际在系统中以`ftp`用户的权限和身份运行。
3. 用户目录和权限:
    - 通过PAM验证的虚拟用户可以拥有独立的FTP目录与权限(通过`user_config_dir`和对应的用户配置文件来实现独立的目录配置)。
    - 因为它们实际工作在一个独立的系统用户下,而不是各自独立的系统用户。
总结
总的来说,`guest_enable`与`guest_username`组合使用,可以让您创建和管理独立于系统账户的虚拟FTP用户。这提供了更大的灵活性,并且避免了将FTP用户名添加为系统用户的复杂性及潜在安全风险。
通过这种方式,您可以拥有独立的虚拟用户账户,同时确保它们通过一个安全的实际系统用户来控制他们的权限和资源访问。
如有任何其他疑问或需要更多帮助,请随时告知! (已编辑) 

# Enable local user chroot
chroot_local_user=YES
这个设置有什么用处?

chroot_local_user=YES 是一个在 vsftpd 配置文件中的选项,它的作用是将本地用户(即登录系统的用户)限制在其主目录(Home Directory)内。具体来说,启用该选项后,登录的用户将无法访问其主目录以外的文件系统,从而增强系统的安全性。
具体作用
1. 安全隔离:
   - 当用户登录FTP服务器时,`chroot_local_user=YES` 会将用户的根目录设置为他们的主目录。这样可以防止用户通过FTP访问系统的其他部分,保护系统其他文件和目录不被误操作或者恶意访问。
2. 减少误操作:
   - 限制用户在其主目录内活动,减少用户误删、误改系统关键文件的可能性。
3. 简化权限管理:
   - 系统管理员可以更容易管理用户权限,因为用户只能访问和操作其主目录内的文件和目录。
配置示例
以下是`vsftpd`配置文件中启用 chroot_local_user 的一段示例:
plaintext
# Enable local user login
local_enable=YES

# Enable write permissions
write_enable=YES

# Enable local user chroot
chroot_local_user=YES
注意事项
- 目录结构:为了确保安全,当启用 chroot_local_user 时,对用户主目录的权限需要特别注意。例如,用户主目录不能给予用户写权限,通常需要是由root拥有并具备打开/登录的权限。
- 例外情况:如果你希望某些用户不受此限制,可以使用 chroot_list_enable=YES 和 chroot_list_file=/etc/vsftpd.chroot_list 来定义一个例外列表。在 /etc/vsftpd.chroot_list 文件中列出你不想强制`chroot`的用户。
plaintext
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
创建 /etc/vsftpd.chroot_list 文件
1. 创建并编辑 vsftpd.chroot_list 文件:
sh
sudo nano /etc/vsftpd.chroot_list
2. 在文件中添加不受限制的本地用户名称(每个用户名占一行):
plaintext
allowed_user1
allowed_user2
3. 保存并退出编辑器。
总结
chroot_local_user=YES 是 vsftpd 提供的一个很有用的安全特性,用于隔离用户访问,防止他们超出其主目录的范围。这个设置增强了系统的安全性,同时减少了配置管理的复杂度。在配置和使用时需要注意目录权限的问题,确保实际效果符合预期。 (已编辑) 

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers
系统没有/etc/vsftpd目录

如果你的系统上没有 /etc/vsftpd 目录,可以自己创建。这里是详细步骤,包括确保必要的目录结构存在并设置正确的权限:
创建 /etc/vsftpd 目录
1. 手动创建 vsftpd 配置目录:
sh
sudo mkdir -p /etc/vsftpd
2. 设置适当的权限:
sh
sudo chmod 755 /etc/vsftpd
配置vsftpd
再次配置 vsftpd,如下步骤确保我们使用独立的FTP用户:
1. 打开 vsftpd 配置文件,如果不存在就创建:
sh
sudo nano /etc/vsftpd/vsftpd.conf
2. 修改或添加以下配置项:
plaintext
# Enable anonymous login (disable if not needed)
anonymous_enable=NO

# Enable local user login
local_enable=YES

# Enable write permissions
write_enable=YES

# Enable local user chroot
chroot_local_user=YES

# Enable virtual users
guest_enable=YES
guest_username=ftp

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers

# Use PAM service for authentication
pam_service_name=vsftpd
3. 保存并退出编辑器。
创建独立的FTP用户数据库
1. 创建文件存放虚拟用户名和密码:
sh
sudo nano /etc/vsftpd/vsftpd-virtual-users.txt
2. 输入虚拟用户名和密码:
plaintext
ftpuser1
password1
ftpuser2
password2
3. 保存并退出编辑器。
4. 生成数据库文件:
sh
sudo db_load -T -t hash -f /etc/vsftpd/vsftpd-virtual-users.txt /etc/vsftpd/vsftpd-virtual-users.db
5. 确保数据库文件的权限只允许 root 访问:
sh
sudo chmod 600 /etc/vsftpd/vsftpd-virtual-users.db
配置PAM文件
1. 继续配置 PAM 文件:
sh
sudo nano /etc/pam.d/vsftpd
2. 保证文件内容如下:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
3. 保存并退出编辑器。
配置虚拟用户个性化目录
1. 创建虚拟用户配置目录:
sh
sudo mkdir -p /etc/vsftpd/vusers
2. 为每个FTP用户创建个人配置文件,例如 ftpuser1:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
3. 添加配置内容,指定用户的个性化设置:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
4. 保存并退出编辑器。
创建FTP目录结构
1. 创建用户绑定的主目录,并配置权限:
sh
sudo mkdir -p /home/ftp/ftpuser1
sudo chmod -R 755 /home/ftp
sudo chown -R ftp:ftp /home/ftp
重启vsftpd服务
完成配置后,重启 vsftpd 服务:
sh
sudo systemctl restart vsftpd
测试FTP连接
使用FTP客户端连接FTP服务器,并使用你在 vsftpd-virtual-users.txt 中配置的虚拟用户名和密码进行登录。
故障排除
如果在连接或配置过程中遇到任何问题,可以查看 vsftpd 的日志文件:
sh
sudo tail -f /var/log/vsftpd.log
这样有助于查找和解决潜在的配置问题。如果还有其他问题或需要进一步的帮助,请随时提问。 (已编辑) 

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers
/etc/vsftpd/vusers目录也要手动创建吗?

是的,您需要手动创建 /etc/vsftpd/vusers 目录,以便存储每个虚拟用户的个性化配置。您可以按照以下步骤操作:
1. 创建 /etc/vsftpd/vusers 目录:
sh
sudo mkdir -p /etc/vsftpd/vusers
2. 为每个虚拟用户创建个人配置文件。例如,假设您有一个虚拟用户 ftpuser1,您需要创建对应的配置文件:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
3. 在配置文件中添加相关配置。比如:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
4. 保存并退出编辑器窗口。
确保 /etc/vsftpd/vusers 目录的正确权限设置:
sh
sudo chmod 755 /etc/vsftpd/vusers
这些步骤完成后,`vsftpd`服务器将能够加载并读取每个虚拟用户的个性化设置。
示例
完整的创建多个用户配置文件的过程,可以按以下步骤来操作:
1. 创建用户配置目录:
sh
sudo mkdir -p /etc/vsftpd/vusers
2. 为每个FTP用户创建个人配置文件,例如 ftpuser1 和 ftpuser2:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
在文件中添加:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
保存并退出。然后再为 ftpuser2 创建配置文件:
sh
sudo nano /etc/vsftpd/vusers/ftpuser2
在文件中添加:
plaintext
local_root=/home/ftp/ftpuser2
write_enable=YES
3. 创建FTP用户相应的目录结构和设置权限,这里以 ftpuser1 和 ftpuser2为例:
sh
# 创建用户home目录
sudo mkdir -p /home/ftp/ftpuser1
sudo mkdir -p /home/ftp/ftpuser2

# 设置目录权限
sudo chmod -R 755 /home/ftp
sudo chown -R ftp:ftp /home/ftp
4. 最后,重启 vsftpd 服务以应用新配置:
sh
sudo systemctl restart vsftpd
至此,您应该能够使用虚拟FTP用户进行认证并访问其个性化的FTP目录。如果有更多问题,请随时提出! (已编辑) 

secure_chroot_dir=/var/run/vsftpd/empty
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`的安全和SSL相关的配置,具体保留与否和你的实际需求有关。让我解释一下每一项以及保留它们的建议:
1. secure_chroot_dir
plaintext
secure_chroot_dir=/var/run/vsftpd/empty
  
这个选项定义了一个安全的chroot环境,当用户被chroot锁定时会用到。如果你打算让用户chroot(锁定在其主目录里),该选项是保留的,默认值通常是好的。
2. rsa_cert_file 和 rsa_private_key_file
plaintext
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
这些选项用于配置SSL证书和私钥文件路径。如果你打算启用SSL/TLS的话,这些配置项是需要保留和正确配置的。不过如果你不打算使用SSL/TLS,可以暂时忽略它们。
3. ssl_enable
plaintext
ssl_enable=NO
该选项决定是否启用SSL支持。默认值是`NO`(即禁用SSL)。如果你不打算使用SSL/TLS进行加密传输,可以保持`NO`。但在生产环境中,启用SSL(即将其值设为`YES`)是推荐的,以确保传输数据的安全性。
总结
保留这些配置项的建议如下:
- secure_chroot_dir: 保留。
- rsa_cert_file 和 rsa_private_key_file: 如果打算使用SSL/TLS,请保留并正确配置证书路径;否则,可以忽略。
- ssl_enable: 根据你的需求决定。如果安全性重要并且为了保护传输数据,建议设置为`YES`;否则,保持`NO`。
完整配置示例
在结合独立的FTP用户配置和保留上述配置项的基础上,完整的`/etc/vsftpd.conf`配置文件可能如下:
plaintext
# General Settings
listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty

# Virtual User Settings
guest_enable=YES
guest_username=ftp
user_sub_token=$USER
local_root=/home/ftp/$USER
user_config_dir=/etc/vsftpd/vusers
pam_service_name=vsftpd

# Logging
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES

# Security
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO  # Set to YES if you want to enable SSL/TLS

# Other settings
file_open_mode=0666
确保在最佳实践和安全要求下配置合适的SSL证书,并根据需求调整配置。如果有其他问题或需要进一步的帮助,随时提出。 (已编辑) 

connect_from_port_20=YES
xferlog_enable=YES
use_localtime=YES
dirmessage_enable=YES
listen_ipv6=YES
listen=NO
这几项也是原有的配置,保留吗?

是的,您提到的这些配置项是 vsftpd 配置文件中的一些常见选项,它们可以保留,因为它们设置了服务器的一些基本行为和功能。以下是这些选项的具体说明:
1. connect_from_port_20=YES:使用FTP数据端口20进行主动的FTP数据连接。这是FTP主动模式的标准端口。
2. xferlog_enable=YES:启用传输日志记录,记录每次FTP会话的信息到日志文件中。通常日志文件位于 /var/log/vsftpd.log。
3. use_localtime=YES:使用服务器的本地时间,而不是UTC时间。
4. dirmessage_enable=YES:当用户进入某个目录时,显示目录中的 .message 文件内容。
5. listen_ipv6=YES:启用 vsftpd 以监听IPv6网络。如果你的服务器使用IPv6,这个选项是必须的。
6. listen=NO:禁用IPv4监听。这选项通常和 listen_ipv6=YES 结合使用,以确保服务器同时支持IPv4和IPv6。
这些选项可以在配置文件中保留,确保服务器的功能和行为符合预期。不过,您需要确保整体配置没有冲突,特别是在IPv4和IPv6相关设置上。
最后,以下是一个包含上述选项的和一些必要虚拟用户配置选项的完整 vsftpd.conf 示例:
conf
# Existing settings
connect_from_port_20=YES
xferlog_enable=YES
use_localtime=YES
dirmessage_enable=YES
listen_ipv6=YES
listen=NO

# Enable anonymous login (disable if not needed)
anonymous_enable=NO

# Enable local user login
local_enable=YES

# Enable write permissions
write_enable=YES

# Enable local user chroot
chroot_local_user=YES

# Enable virtual users
guest_enable=YES
guest_username=ftp

# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers

# Use PAM service for authentication
pam_service_name=vsftpd
完成配置后,记得重新启动 vsftpd 服务:
sh
sudo systemctl restart vsftpd
这样,FTP服务器将会按照您的配置项正常运行。如果有任何进一步的问题,欢迎继续提问! (已编辑) 

Standard behaviour for ftpd(8).
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed
# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.
# Standard pam includes
@include common-account
@include common-session
@include common-auth
auth    required        pam_shells.so
上面是/etc/pam.d/vsftpd原有内容,需要删除,再替换为
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
还是在原有内容基础上添加:
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users

在配置 /etc/pam.d/vsftpd 文件时,为了使用独立用户数据库进行身份验证,通常需要根据具体需求来决定是否替换原有内容。
替换原有内容:
如果你希望完全使用独立用户数据库进行身份验证,并且完全禁用现有的身份验证机制,可以删除原有内容并替换为:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
保留原有内容并添加新的配置:
如果你希望在保留原有身份验证机制基础上,添加独立用户数据库的配置,可以选择在原有内容基础上添加新的配置。这种做法有助于在不破坏现有功能的前提下,实现新功能:
plaintext
# 原有内容
auth    required        pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.

# Standard pam includes
@include common-account
@include common-session
@include common-auth
auth    required        pam_shells.so

# 添加独立用户数据库的配置
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
推荐操作:
为了确保权限管理和身份验证逻辑简单明了,同时避免与系统用户混淆,我建议替换原有的配置,除非你明确知道需要保留现有身份验证机制。
所以,以下是我推荐的操作:
1. 编辑文件 /etc/pam.d/vsftpd:
sh
sudo nano /etc/pam.d/vsftpd
2. 删除全部内容并替换为以下配置:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
3. 保存并退出编辑器(在Nano中,按 CTRL+X,然后按 Y 确认保存,最后按 Enter 退出)。
重新启动vsftpd服务
确认配置之后,重新启动vsftpd服务:
sh
sudo systemctl restart vsftpd
这些步骤将确保FTP服务器只使用你配置的独立用户数据库进行身份验证,从而实现与系统用户分离的登录方式。如果有任何疑问或需要进一步帮助,请告诉我。 (已编辑) 

如何查看有哪些用户,特别查看有没有ftp用户?

在Ubuntu系统中,要查看系统上有哪些用户,包括检查是否存在特定的FTP用户,可以通过以下几种方法实现:
查看系统用户
1. 查看/etc/passwd文件:
   /etc/passwd 文件中包含了系统上所有用户的信息,可以通过阅读该文件来了解系统用户:
   
sh
   cat /etc/passwd
   
   每一行代表一个用户,格式类似这样:
   
plaintext
   username:x:UID:GID:User Description:Home Directory:Shell
   
2. 使用`getent`命令:
   getent passwd 命令也可以列出系统上的所有用户:
   
sh
   getent passwd
   
查看虚拟FTP用户
由于虚拟FTP用户独立于系统用户,它们存储在自定义的数据库文件中。例如,如果你使用了`db_load`创建了用户数据库文件,那么可以通过以下方法查看和验证这些用户:
1. 查看创建虚拟用户时的源文件:
   你可能已经创建了一个包含用户名和密码的源文件,例如 /etc/vsftpd/vsftpd-virtual-users.txt:
   
sh
   cat /etc/vsftpd/vsftpd-virtual-users.txt
   
   这个文件中的内容是以用户名和密码成对出现的格式,例如:
   
plaintext
   ftpuser1
   password1
   ftpuser2
   password2
   
2. 查看生成的数据库文件:
   由于虚拟用户是存储在数据库文件中,可以通过工具来检查:
   
sh
   db_dump /etc/vsftpd/vsftpd-virtual-users.db
   
   这会显示数据库文件的内容,其中包含存储的用户信息。
总结
- 系统用户 可以通过查看 /etc/passwd 或 使用 getent passwd 命令。
- 虚拟FTP用户 根据配置,通常在创建用户时的源文件(例如 /etc/vsftpd/vsftpd-virtual-users.txt) 或生成的数据库文件中(可以使用`db_dump`命令查看)。
通过上述方法,你可以查看系统用户和检查虚拟FTP用户的存在。如有任何其他问题或需要进一步解释,请随时询问。 (已编辑) 

sudo chown -R ftp:ftp /home/ftp中的ftp:ftp表示什么?

在Linux系统中,`chown`命令用于更改文件或目录的所有者和组。命令格式如下:
sh
chown [OPTIONS] OWNER[:GROUP] FILE...
在你的问题中,命令`sudo chown -R ftp:ftp /home/ftp`的具体含义如下:
- sudo:以超级用户权限运行命令。
- chown:更改文件的拥有者和组。
- -R:递归地更改指定目录及其所有子目录和文件。
- ftp:ftp:指定用户和组,`ftp`是用户(OWNER),第二个`ftp`是组(GROUP)。
- /home/ftp:目标目录。
所以,`ftp:ftp`表示将目标目录`/home/ftp`及其所有子目录和文件的拥有者设置为用户`ftp`,并将其组设置为`ftp`组。
举例
假设你有以下文件结构:
/home/ftp
    ├── ftpuser1
    ├── ftpuser2
执行 sudo chown -R ftp:ftp /home/ftp 后,目录 /home/ftp 及其子目录 ftpuser1 和 ftpuser2 的拥有者和组都将被设定为用户 ftp 和组 ftp。
为什么要这样做?
vsftpd服务器配置时要求用户目录的拥有者和组权限正确。通过将文件目录的拥有者和组设置为`ftp`用户和`ftp`组,可以确保虚拟用户的文件操作权限在FTP服务器内部可控,并避免使用实际系统用户权限,从而提高安全性。
如果你有进一步需求或疑问,请随时提问。 (已编辑) 

如何查看系统有哪些用户和组?

在Ubuntu以及其他Linux发行版中,可以通过以下命令来查看系统中的用户和组。
查看系统用户
系统用户信息存储在文件 /etc/passwd 中,你可以通过以下命令来查看所有用户:
sh
cat /etc/passwd
如果只需要查看用户名,使用以下命令:
sh
cut -d: -f1 /etc/passwd
或者更简洁地使用 awk:
sh
awk -F: '{print $1}' /etc/passwd
查看系统组
系统组信息存储在文件 /etc/group 中,你可以通过以下命令来查看所有组:
sh
cat /etc/group
如果只需要查看组名,使用以下命令:
sh
cut -d: -f1 /etc/group
或者使用 awk:
sh
awk -F: '{print $1}' /etc/group
增强的用户和组管理工具
getent命令是一个更高级的方式来查看用户和组,适用于系统集成其他身份目录如LDAP时:
- 查看系统用户:
sh
getent passwd
- 查看系统组:
sh
getent group
具体示例
1. 查看系统用户:
sh
cat /etc/passwd
输出示例:
plaintext
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
yourusername:x:1000:1000:Your Name,,,:/home/yourusername:/bin/bash
2. 只查看用户名:
sh
cut -d: -f1 /etc/passwd
输出示例:
plaintext
root
daemon
...
yourusername
3. 查看系统组:
sh
cat /etc/group
输出示例:
plaintext
root:x:0:
daemon:x:1:
...
sudo:x:27:yourusername
4. 只查看组名:
sh
cut -d: -f1 /etc/group
输出示例:
plaintext
root
daemon
...
sudo
通过这些命令,你可以方便地查看系统中有哪些用户和组。 (已编辑) 

按照以上配置,ftp客户端登陆失败:
(base) yong@yong-System:~$ ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 3.0.5)
Name (127.0.0.1:yong): ftpuser1
331 Please specify the password.
Password:
530 Login incorrect.
ftp: Login failed
ftp>
(base) yong@yong-System:~$ cat /etc/vsftpd.conf
# Example config file /etc/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.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
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.
listen_ipv6=YES
#
# 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
#
# 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
#
# 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
#
# 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/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 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.
# 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 restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
chroot_local_user=YES
#
# 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
#
# Customization
# Enable virtual users
guest_enable=YES
guest_username=ftp
# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# 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
#
# 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

根据你提供的错误信息和配置文件,虚拟用户登录失败的问题主要可能出现在以下几个方面:
- PAM配置文件(`/etc/pam.d/vsftpd`)配置不正确。
- 虚拟用户数据库的创建过程有误。
我们首先检查并确保这两个方面配置正确无误。
检查PAM配置文件
请确保`/etc/pam.d/vsftpd`文件内容如下:
1. 打开`/etc/pam.d/vsftpd`文件:
sh
sudo nano /etc/pam.d/vsftpd
2. 确保文件内容如下:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
3. 保存并退出编辑器。
验证虚拟用户数据库是否正确
1. 重新检查虚拟用户文件`/etc/vsftpd/vsftpd-virtual-users.txt`内容是否正确:
plaintext
ftpuser1
password1
ftpuser2
password2
2. 重新生成数据库文件:
sh
sudo db_load -T -t hash -f /etc/vsftpd/vsftpd-virtual-users.txt /etc/vsftpd/vsftpd-virtual-users.db
3. 确保数据库文件权限为`root`:
sh
sudo chmod 600 /etc/vsftpd/vsftpd-virtual-users.db
确保`vsftpd.conf`中的配置正确
从你提供的`vsftpd.conf`配置文件来看,相关配置似乎是正确的。请确保它包含以下内容:
plaintext
guest_enable=YES
guest_username=ftp
user_config_dir=/etc/vsftpd/vusers
pam_service_name=vsftpd
确保虚拟用户配置文件存在
1. 确保虚拟用户配置目录存在:
sh
sudo mkdir -p /etc/vsftpd/vusers
2. 为每个FTP用户创建个人配置文件,例如`ftpuser1`:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
3. 添加文件内容,指定用户的个性化配置:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
4. 保存并退出编辑器。
创建FTP目录结构
1. 创建绑定用户名的主目录,并配置权限:
sh
sudo mkdir -p /home/ftp/ftpuser1
sudo chmod -R 755 /home/ftp
sudo chown -R ftp:ftp /home/ftp
重启vsftpd服务
sh
sudo systemctl restart vsftpd
检查系统日志
如果仍然无法登录,请检查vsftpd系统日志获取更多信息:
sh
sudo tail -f /var/log/auth.log
尝试再次登录,查看日志中是否有进一步提示。
这些步骤应该可以帮你解决FTP客户端登录失败的问题。如果还有问题,请继续提供具体的错误信息及日志内容。 (已编辑) 

重新配置 /etc/pam.d/vsftpd 文件
替换原有内容:
删除原有内容并替换为:
plaintext
auth required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
account required pam_userdb.so db=/etc/vsftpd/vsftpd-virtual-users
出现另一个错误:
(base) yong@yong-System:~$ ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 3.0.5)
Name (127.0.0.1:yong): ftpuser1
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
ftp: Login failed

出现“500 OOPS: vsftpd: refusing to run with writable root inside chroot()”错误,是因为vsftpd对于chroot用户的安全限制,要求用户目录不可写。这是vsftpd出于安全考虑的一项限制。为了让FTP用户能够在他们的家目录下有写权限,我们需要调整配置:
解决方法:创建独立的可写子目录
首先,确保用户的根目录(/home/ftp/ftpuser1)是可读的,但并不可写。在目录下创建一个可写的子目录,例如`/upload`。
操作步骤:
1. 修改目录权限
确保用户根目录不可写:
sh
sudo chmod a-w /home/ftp/ftpuser1
然后,在用户目录下创建一个可写的子目录:
sh
sudo mkdir /home/ftp/ftpuser1/upload
sudo chown ftp:ftp /home/ftp/ftpuser1/upload
sudo chmod 755 /home/ftp/ftpuser1/upload
这样,`ftpuser1`用户的根目录不可写,但`/home/ftp/ftpuser1/upload`目录可写。
2. 更新用户配置
打开用户配置文件:
sh
sudo nano /etc/vsftpd/vusers/ftpuser1
确保包含以下内容:
plaintext
local_root=/home/ftp/ftpuser1
write_enable=YES
3. 配置vsftpd.conf
修改vsftpd的配置文件:
sh
sudo nano /etc/vsftpd.conf
确保加入或更新以下配置项,来允许用户写入子目录:
plaintext
allow_writeable_chroot=YES
保存并退出编辑器。
重启vsftpd服务
sh
sudo systemctl restart vsftpd
测试FTP连接
使用FTP客户端再次连接并测试登录。现在,FTP用户`ftpuser1`应该能够登录,并在其根目录下的`upload`子目录中拥有写权限。
注意
注意`allow_writeable_chroot=YES`选项在启用时,可以减少一些的安全性,如果安全性是首要任务,可以启用`secure_chroot_dir`,以增加安全性。
更高级的安全配置
根据需要,可以用更多的vsftpd配置选项及PAM策略来提升FTP服务器的安全性。例如,可以使用TLS来实现FTP的加密传输:
plaintext
ssl_enable=YES
以上就是如何解决“500 OOPS: vsftpd: refusing to run with writable root inside chroot()”错误的解决方法。如果你有更多问题,随时提问。 (已编辑) 

chroot用户是什么?

chroot(change root)的意思是更改根目录。它是一种操作系统技术,用于改变当前进程和它的子进程所看到的文件系统根目录。使用 chroot 后,进程将无法访问原根目录之外的文件,这提供了一种在受限环境中运行进程的方法。
在FTP服务器的上下文中,`chroot`用户指的是被限制在一个特定目录中的用户。这意味着,当用户连接到FTP服务器时,他们只能看到和访问这个特定目录及其子目录,而不能浏览或访问系统的其他部分。这对于安全性非常重要,因为它限制了FTP用户对系统文件和其他用户文件的访问权。
在`vsftpd`中使用`chroot`
在`vsftpd`配置文件中,通过设置`chroot_local_user=YES` 或 使用 chroot_list_enable等选项,可以启用或配置 chroot 用户。
以下是`vsftpd`配置文件中常用的与`chroot`相关的选项以及它们的含义:
1. chroot_local_user=YES:
    - 如果开启 (YES),所有本地用户将在连接登录后被限制在他们的家目录中。这是一个全局选项,适用于所有本地用户。
2. chroot_list_enable=YES:
    - 启用一个用户列表,以选择性地对某些用户应用 chroot。
3. chroot_list_file:
    - 指定包含用户名称的文件,列表中的用户将不会被 chroot 限制(如果 chroot_local_user=YES)。例如:
      
sh
      chroot_list_file=/etc/vsftpd/chroot_list
      
示例配置
1. 打开`vsftpd`配置文件进行编辑:
sh
sudo nano /etc/vsftpd.conf
2. 修改或添加以下配置项来启用 chroot:
plaintext
chroot_local_user=YES
# 取消 chroot_list_enable 以对所有用户启用 chroot
# 如果希望对部分用户启用 chroot,可启用以下两行
# chroot_list_enable=YES
# chroot_list_file=/etc/vsftpd/chroot_list
3. 如果使用 chroot_list_enable=YES:
    - 创建或编辑文件 /etc/vsftpd/chroot_list,并添加那些不需要被 chroot 限制的用户名称,每行一个用户名:
      
sh
      sudo nano /etc/vsftpd/chroot_list
      
    - 文件示例内容:
      
plaintext
      ftpuser1
      ftpuser2
      
4. 保存并退出编辑器。
5. 重启 vsftpd 服务使配置生效:
sh
sudo systemctl restart vsftpd
检查和验证
可以使用FTP客户端连接到FTP服务器,登录启用`chroot`的用户,并验证用户是否被限制在他们的家目录中。如果用户尝试浏览其他目录,他们将无法访问非授权目录。
总结
chroot用户在FTP服务器中的主要目的是出于安全考虑,限制用户只能在特定目录中操作,以保护系统的其他部分和其他用户的文件安全。在配置 vsftpd 时,启用 chroot 是一种增强安全性的常见做法。 (已编辑) 

$ ls -l /home/ftp/ftpuser1
总计 4
drwxr-xr-x 2 ftp ftp 4096  7月 24 09:34 upload
这里面的drwxr-xr-x解释一下。

当然,`ls -l` 命令输出的每一列都有特定的意义,我们可以分解一下 drwxr-xr-x 来解释它的含义。
drwxr-xr-x 解析
drwxr-xr-x 字符串可以分成几个部分,每个部分代表对象 upload 的不同属性:
1. 第一个字符:文件类型
   - d:表示这是一个目录(directory)。
   - -:表示这是一个常规文件。
   - l:表示这是一个符号链接。
2. 接下来的九个字符:权限
   按三个字符一组来解释:
   - rwx(第1-3个字符):表示所有者(owner,即用户 ftp)的权限。
     - r:表示读取(read)权限。
     - w:表示写(write)权限。
     - x:表示执行(execute)权限(对于目录,表示可以进入该目录)。
   - r-x(第4-6个字符):表示同组用户(group,即组 ftp)的权限。
     - r:表示读取权限。
     - -:表示没有写权限。
     - x:表示执行权限。
   - r-x(第7-9个字符):表示其他用户(others,除所有者和同组用户外,所有用户)的权限。
     - r:表示读取权限。
     - -:表示没有写权限。
     - x:表示执行权限。
总结
drwxr-xr-x 表示一个目录,其权限组合解释如下:
- 这是一个目录(因为第一个字符是 d)。
- 其所有者(owner,即 ftp 用户)可以读取、写入和执行这个目录(`rwx`)。
- 与所有者同组的用户(group,即 ftp 组)可以读取和执行这个目录,但不能写入(`r-x`)。
- 其他用户(others)可以读取和执行这个目录,但不能写入(`r-x`)。
这样,权限 drwxr-xr-x 赋予了某些用户完全访问权限,给同组用户和其他用户有限的访问能力,确保了文件系统的安全性和灵活性。 (已编辑) 

allow_writeable_chroot=YES 是什么意思?

allow_writeable_chroot=YES 是 vsftpd(非常安全的FTP服务器)中的一个配置选项,它允许FTP用户拥有对其 chroot (change root) 目录的写权限。
默认情况下,vsftpd出于安全考虑,不允许用户对他们的chroot目录拥有写权限。这是为了防止潜在的安全问题,因为允许用户写访问他们的chroot目录可能会让他们修改或破坏FTP服务器的安全性。
然而,在某些特定的使用场景下,您可能希望用户能够在其根目录内进行写操作,就像上传文件、创建子目录等等。在这种情况下,您可以启用 allow_writeable_chroot=YES 配置项。
不过需要注意的是,这样做存在潜在的安全隐患。所以,在启用此选项之前,一定要确认您的服务器和应用场景是安全的,并确保您信任所有有写权限的用户。
在配置文件 /etc/vsftpd/vsftpd.conf 中添加或修改这一行:
sh
allow_writeable_chroot=YES
保存文件并重启 vsftpd 服务以使更改生效:
sh
sudo systemctl restart vsftpd
注意事项:
- 确保您了解并理解启用这一选项带来的安全风险。
- 定期更新系统和 vsftpd 以防止已知的安全漏洞。
- 如果可能,尽量使用更加安全的传输协议(如 SFTP 或 FTPS)。
示例配置
完整的 vsftpd 配置文件示例:
sh
# 禁用匿名登录
anonymous_enable=NO

# 允许本地用户登录
local_enable=YES

# 允许写操作
write_enable=YES

# 启用chroot,用户被限制在其主目录
chroot_local_user=YES

# 允许对chroot目录的写权限
allow_writeable_chroot=YES

# 其他常用选项
listen=YES
listen_ipv6=NO
pam_service_name=vsftpd
tcp_wrappers=YES
通过上述配置,您可以实现对 FTP 用户的额外写权限,同时抑制一些可能的错误。当用户尝试上传或修改文件时,如果没有这一行配置而用户目录又是可写的,vsftpd 可能会拒绝写操作并返回错误信息。

ftp> put hipblas_sample.cpp
local: hipblas_sample.cpp remote: hipblas_sample.cpp
229 Entering Extended Passive Mode (|||62472|)
550 Permission denied.
ftp> ls
229 Entering Extended Passive Mode (|||41099|)
150 Here comes the directory listing.
drwxrwxrwx   2 131     141         4096 Jul 24 09:34 upload
226 Directory send OK.
ftp>
这个错误如何解决?

在使用 FTP 上传文件时,出现 "550 Permission denied" 错误,表示你当前的FTP用户对目标目录或文件没有写入权限。以下是一些解决此问题的方法:
1. 检查目录权限:
   确认你有权限在目标目录中进行写操作。你可以尝试使用 ls -l 命令查看目录权限。
   
shell
   ftp> ls -l
   
   如果没有权限,则需要联系服务器管理员来修改权限。
2. 切换目录:
   你可以尝试切换到不同的目录,看是否有权限将文件上传至其它目录。例如:
   
shell
   ftp> cd upload
   ftp> put hipblas_sample.cpp
   
3. 更改FTP用户:
   以管理员或其他具有写权限的用户登录FTP。如果你没有相应的用户凭证,联系服务器管理员获取。
4. 检查文件路径:
   确保你指定的目标路径是正确的,有时路径问题也会引起权限错误。
5. 联系管理员:
   如果你尝试了上述方法仍然无法解决问题,请联系服务器管理员,让他们检查服务器设置,确认你有适当的权限。
以上是常见的解决方法,希望能帮助你解决问题。如果还有其他问题,请提供更多详细信息。 (已编辑) 

$ cat /etc/vsftpd.conf
# Example config file /etc/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.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
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.
listen_ipv6=YES
#
# 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
#
# 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
#
# 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
#
# 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/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 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.
# 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 restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
chroot_local_user=YES
#
# 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
#
# Customization
# Enable virtual users
guest_enable=YES
guest_username=ftp
# Allow the use of the user_config_dir option
user_config_dir=/etc/vsftpd/vusers
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# 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
#
# 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
allow_writeable_chroot=YES
(base) yong@yong-System:/home/ftp/ftpuser1$ ls -l /home/ftp/ftpuser1
总计 4
drwxrwxrwx 2 ftp ftp 4096  7月 24 13:23 upload
(base) yong@yong-System:/home/ftp/ftpuser1$ ls -l /home/ftp
总计 4
drwxrwxrwx 3 ftp ftp 4096  7月 24 13:16 ftpuser1
以上有什么问题,上传时候提示:550 Permission denied.

从你的配置和目录权限来看,似乎一切都是正确的,但上传文件时仍然收到 "550 Permission denied" 错误。以下是一些检查和可能的解决方法:
1. 确认用户权限:
   确认你使用的FTP用户名在文件系统中有足够的权限。你已经将目录权限设置为 drwxrwxrwx,即所有用户都应该有写权限。但是需要确保登录的FTP用户确实有权访问这些目录。
2. 用户的实际身份:
   你配置了虚拟用户 guest_enable=YES 和 guest_username=ftp,这意味着所有登录的虚拟用户实际上都会用 ftp 用户的身份操作文件。需要确认 ftp 用户是否在系统中有足够的权限。
3. 检查上传路径:
   确保在上传文件时的路径是正确的。例如,我们看到 /home/ftp/ftpuser1/upload 是一个存在且可写的目录。所以在上传时需要明确指定这个路径。
4. 重启 vsftpd:
   每次修改配置文件后,需要重启 vsftpd 服务以使配置生效:
   
bash
   sudo systemctl restart vsftpd
   
5. Log文件检查:
   检查 vsftpd 日志文件,以获取更多错误信息。通常这些日志文件位于 /var/log/vsftpd.log 或 /var/log/xferlog。
6. 是否允许文件覆盖:
   确认 vsftpd.conf 中有配置项允许覆盖文件(如果目标文件已存在):
   
ini
   allow_writeable_chroot=YES
   
7. 提供系统权限:
   同时确认目录 /home/ftp 及其父目录对 ftp 用户也是可读可写的,父目录的权限可能影响其子目录:
   
bash
   sudo chown -R ftp:ftp /home/ftp/ftpuser1
   
8. SELinux / AppArmor:
   如果系统使用了 SELinux 或 AppArmor,请确保它们没有限制 vsftpd 操作文件。可以暂时禁用或配置它们来允许 FTP 访问。
通过上述步骤,希望能够找到问题所在并解决。如果仍然有问题,请提供更多的日志信息以及尝试的具体命令,以便进一步诊断。 (已编辑) 
仅对你可见


/home目录权限会影响ftp客户端上传文件吗?

是否能够通过 FTP 客户端上传文件主要取决于目标目录的权限,而不是 /home 目录的权限。`/home` 目录权限会影响到用户在其主目录下的操作权限,但具体到 FTP 服务,一般只会影响设置的FTP根目录或子目录。
然而,如果你的 FTP 账户对应的主目录在 /home 下,且你试图上传文件到那里,那么 /home 目录的权限配置当然会影响你能否成功上传文件。
以下是一些具体场景:
1. FTP 根目录或子目录:
   若你的 FTP 根目录或子目录没有写权限,即使 /home 目录有写权限,你仍然无法上传文件。例如:
   - 如果你的 FTP 根目录是 /var/ftp/,那么你需要确保 /var/ftp/ 及其子目录有写权限。
2. 用户主目录:
   若 FTP 用户的主目录设置在 /home/<username> 下,你上传文件时便需要 /home/<username> 具有相应的写权限。你可以使用命令检查目录权限:
   
shell
   ls -ld /home/<username>
   
   应该输出类似如下信息:
   
shell
   drwxr-xr-x  3 username group 4096 date /home/username
   
   其中,`drwx------,drwxr-xr-x` 表示有读写权限。如果权限不足,可以用 chmod 命令修改权限。例如:
   
shell
   chmod 755 /home/username
   
3. 上传路径的权限:
   确保上传目标路径具有适当的权限。例如:
   
shell
   chmod 755 /home/username/upload
   
总结
具体来说,FTP 上传文件的权限依赖于目标目录的权限。如果 /home 目录是目标路径的一部分,则它会影响权限。确保目标路径中的每个目录都有适当的权限,即可解决 "550 Permission denied" 错误。 (已编辑) 

配置了虚拟用户 guest_enable=YES 和 guest_username=ftp,这意味着所有登录的虚拟用户实际上都会用 ftp 用户的身份操作文件。
如何配置能让登录的虚拟用户实际上都会用 root 用户的身份操作文件?

使用虚拟用户通过 guest_enable 设置成 ftp 用户操作文件是一种常见的做法,确保了对文件系统的访问控制。但是,切换到 root 用户操作文件需要特别谨慎,因為這會影響到系統安全,使誤操作的風險大幅增加。
如果你仍需进行这样的配置,请参考以下步骤:
1. 配置 FTP 虚拟用户:
   确保已正确配置虚拟用户和 vsftpd。假设你已启用虚拟用户并配置了映射到 ftp 用户。
2. 配置 /etc/pam.d/vsftpd:
   确保 PAM 模块配置正确,类似于下面的配置:
   
bash
   auth    required    pam_userdb.so    db=/etc/vsftpd/virtusers
   account required    pam_userdb.so    db=/etc/vsftpd/virtusers
   
3. 修改 vsftpd.conf:
   编辑 /etc/vsftpd/vsftpd.conf 文件,确保包含以下行,并将 guest_username 设置为 root。
   
ini
   guest_enable=YES
   guest_username=root
   
4. 设置根用户权限(請謹慎操作):
   确保 vsftpd 进程和被动模式端口的规则允许根用户的操作。
5. 重新启动 vsftpd 服务:
   保存配置文件后,重新启动 vsftpd 服务以应用更改。
   
bash
   sudo systemctl restart vsftpd
   
请注意,使用 root 用户来操作 FTP 文件非常危险。以下是一些潜在风险和安全措施:
- 高风险:使用 root 权限意味着任何上传、下载或者删除操作都将直接影响系统根文件系统,非常容易导致系统关键文件被误删。
- 隔离机制:如果必须让虚拟用户以 root 用户的身份操作文件,建议使用 Docker 容器或者虚拟机,以隔离文件操作与主机系统的安全性。
- 日志记录和审计:强烈建议开启详细的日志记录和审计功能,确保所有文件操作都能够被追踪和审查。
一般情况下,不建议将 FTP 用户配置为 root 用户。应尽量使用最小权限原则,确保服务器的安全性。 (已编辑) 

virtual_use_local_privs=YES 这个设置以后就能上传文件了,什么原因?

virtual_use_local_privs=YES 是在一些 FTP 服务器软件(例如 vsftpd)中的一个配置选项。设置这个选项为 YES 后,虚拟用户将使用本地用户的权限,这样他们的权限将受到本地用户文件系统权限的控制。
下面是解释为什么设置 virtual_use_local_privs=YES 后你能够上传文件:
1. 统一权限控制:
   virtual_use_local_privs=YES 使得虚拟用户操作文件时使用与对应本地用户一样的权限。这意味着如果本地用户有权限写入特定目录,那么虚拟用户也将继承这些权限。
2. 更细致的权限管理:
   本地用户的权限通常由系统管理员进行更细致的管理,例如通过 chmod 和 chown 命令调整目录和文件的读写权限。启用该配置项后,可以利用文件系统的权限系统,以确保虚拟用户只在被授权的范围内操作。
3. 安全性考虑:
   虚拟用户通常没有系统级的账户,默认情况下权限可能受到限制。通过启用 virtual_use_local_privs=YES,可以根据本地用户的权限进行控制,避免给虚拟用户授予过多的权限,同时简化权限管理。
例如,原来 FTP 虚拟用户权限设置较严格,默认情况下没有写入权限。但是对应的本地用户具有写入权限。设置 virtual_use_local_privs=YES 后,虚拟用户会以本地用户的权限进行操作,所以能够上传文件。
你可以尝试以下步骤确认设置生效:
1. 编辑 vsftpd 配置文件 (/etc/vsftpd/vsftpd.conf),添加或修改以下行:
   
plaintext
   virtual_use_local_privs=YES
   
2. 重启 vsftpd 服务以应用配置更改:
   
bash
   sudo systemctl restart vsftpd
   
3. 重新尝试通过 FTP 上传文件。
通过 virtual_use_local_privs=YES,可以更好地统一管理用户权限,增强安全性和灵活性。如果你有进一步的疑问或需要更多帮助,请告诉我。 (已编辑) 

如何能确保ftp:ftp用户能够对目录/home/ftp/ftpuser1/upload/有写权限?

要确保用户 ftp:ftp 对目录 /home/ftp/ftpuser1/upload/ 有写权限,可以按照以下步骤操作:
1. 检查目录所有权与权限:
   首先,检查目录 /home/ftp/ftpuser1/upload/ 当前的所有权和权限。
   
shell
   ls -ld /home/ftp/ftpuser1/upload/
   
   输出示例:
   
shell
   drwxr-xr-x 2 ftp ftp 4096 Jul 24 09:34 /home/ftp/ftpuser1/upload/
   
   确认当前用户和组是否为 ftp:ftp,以及目录是否具有写权限。
2. 修改目录所有者和组:
   如果需要,可以使用 chown 命令更改目录的所有者和组。
   
shell
   sudo chown ftp:ftp /home/ftp/ftpuser1/upload/
   
3. 修改目录权限:
   修改目录权限以确保可写。使用 chmod 命令赋予写权限。
   
shell
   sudo chmod 775 /home/ftp/ftpuser1/upload/
   
   例如,`775` 表示所有者和组成员可以读取、写入和执行权限,而其他用户仅能读取和执行权限。
4. 验证权限:
   再次检查目录权限,确认更改已生效。
   
shell
   ls -ld /home/ftp/ftpuser1/upload/
   
   输出应类似于:
   
shell
   drwxrwxr-x 2 ftp ftp 4096 Jul 24 09:34 /home/ftp/ftpuser1/upload/
   
5. 测试写权限:
   可以尝试以 ftp:ftp 用户通过FTP上传文件,确保权限配置正确:
   
shell
   ftp> cd /home/ftp/ftpuser1/upload/
   ftp> put hipblas_sample.cpp
   
6. 配置FTP服务器(可选):
   根据运行的FTP服务器软件(如vsftpd、proftpd或pure-ftpd),可能需要检查和调整配置文件,确保 ftp 用户具有相应目录的写权限。配置文件通常位于 /etc/ 目录下。
   示例 (vsftpd):
   - 编辑配置文件 /etc/vsftpd.conf。
   - 确认 write_enable=YES 选项已启用。
   
shell
   sudo nano /etc/vsftpd.conf
   
   确认或添加以下行:
   
shell
   write_enable=YES
   
   修改完配置文件后,重启FTP服务。
   
shell
   sudo systemctl restart vsftpd
   
通过这些步骤,你应能确保 ftp:ftp 用户具备 /home/ftp/ftpuser1/upload/ 目录的写权限。 (已编辑) 

ftp用户可能对/home没有写权限,设置virtual_use_local_privs=YES,由于本地用户通常对/home目录有写权限,所以以ftp用户名义无法上传文件到/home/ftp,而ftp用户有了本地用户权限就能上传文件到/home/ftp了。如何能让ftp用户也能像本地用户一样有/home写权限?是加入root组吗?

在 FTP 服务器中直接赋予 FTP 用户对 /home 目录的写权限通常不是一个推荐的做法,因为这可能会引发安全问题。从安全性角度来看,应尽量避免给予高权限给FTP用户。
但是,如果你确定你需要这样做,可以考虑以下几个步骤,不过要格外小心,确保你了解其中的安全隐患:
1. 设置 vsftpd 用 local_root 指令更改用户根目录
如果你使用的是 vsftpd,可以配置使得某些 FTP 用户拥有更高权限。具体的,可以通过更改 vsftpd.conf 文件:
bash
sudo nano /etc/vsftpd.conf
2. 添加配置
在配置文件中,添加如下配置,使某些用户可以登录到他们的主目录,并具有写权限:
plaintext
chroot_local_user=YES
allow_writeable_chroot=YES
然后,为特定用户配置一个让他们有写权限的目录,比如 /home/ftp_users/username:
plaintext
user_sub_token=$USER
local_root=/home/ftp_users/$USER
3. 创建和设置目录权限
创建对应的目录,并设置权限。假设你要允许用户 ftpuser 对 /home/ftp_users/ftpuser 目录的写权限:
bash
sudo mkdir -p /home/ftp_users/ftpuser
sudo chown ftpuser:ftpuser /home/ftp_users/ftpuser
sudo chmod 755 /home/ftp_users/ftpuser
4. 重启 vsftpd 服务
最后,重启 vsftpd 服务以应用更改:
bash
sudo systemctl restart vsftpd
危险做法:将用户加入root组
虽然不推荐,但如果你确定你需要将FTP用户加入高权限组(如 root 组),你可以使用以下命令:
bash
sudo usermod -aG root ftpuser
注意这种方式极其危险,因为它将授予该用户超级用户权限,会带来巨大的安全风险。
5. 调整用户组和权限
作为一个安全性稍好的替代方案,是将你的 ftp 用户添加到一个拥有 /home 写权限的组,而不是直接添加到 root 组:
bash
sudo groupadd ftpwriters
sudo usermod -aG ftpwriters ftpuser 
然后修改 /home 目录的权限,使它能被这个组写入:
bash
sudo chown -R :ftpwriters /home
sudo chmod -R 775 /home
总结
概括起来,可以通过良好的配置,避免直接赋予FTP用户超级用户权限,实现安全的文件写操作管理。在进行这样的配置调整时,一定要非常注意安全性,避免不必要的风险。
如果无经验和权限设置知识,建议不要随意更改权限,亦或者联系有经验的系统管理员来处理。 (已编辑) 

如何查看本地用户都在哪些组中,ftp用户又都在哪些组中?

在 Linux 系统中,你可以使用以下命令查看本地用户和 FTP 用户所属的组。
查看本地用户所属的组
1. 使用 id 命令:
   你可以使用 id 命令查看某个特定用户所属的组。例如,要查看当前用户所属的组:
   
bash
   id
   
   要查看指定用户所属的组,例如用户 username:
   
bash
   id username
   
2. 使用 groups 命令:
   你也可以使用 groups 命令查看某个用户所属的组。例如:
   
bash
   groups
   
   要查看指定用户的组信息:
   
bash
   groups username
   
3. 查看 /etc/group 文件:
   你可以查看 /etc/group 文件,以获取所有组的信息,以及各组成员。使用以下命令可以过滤出特定用户所在的组:
   
bash
   grep username /etc/group
   
查看 FTP 用户所属的组
FTP 用户通常也是系统用户,所以你可以使用上述相同的方法查看 FTP 用户所属的组。要查看特定 FTP 用户的组,可以使用 id 或 groups 命令。
例如,要查看 FTP 用户 ftpuser 所属的组:
bash
id ftpuser

bash
groups ftpuser
总结
- 使用 id username 命令查看用户的 UID、GID 和组信息。
- 使用 groups username 命令查看用户所属的组。
- 查阅 /etc/group 文件获取更详细的组信息。
这些方法可以帮助你有效地查看本地和 FTP 用户所属的所有组。

相关链接

【linux】Debian10.0配置vsftpd_debian vsftpd-CSDN博客

  • 32
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

109702008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值