CentOS搭建FTP服务器(转)

https://www.linuxidc.com/Linux/2017-11/148518.htm

 

本文测试环境 
1、CentOS 7 
2、测试服务器IP 192.168.1.170

1、安装并启动 FTP 服务

1.1 安装 VSFTPD

使用 yum 安装 vsftpd

yum install -y vsftpd

1.2 启动 VSFTPD

安装完成后,启动 FTP 服务:

service vsftpd start

启动后,可以看到系统已经监听了 21 端口:

netstat -nltp | grep 21

此时,访问 ftp://192.168.1.170 可浏览机器上的 /var/ftp目录了。

2、配置 FTP 权限

2.1 了解 VSFTP 配置

vsftpd 的配置目录为 /etc/vsftpd,包含下列的配置文件:

  • vsftpd.conf 为主要配置文件
  • ftpusers 配置禁止访问 FTP 服务器的用户列表
  • user_list 配置用户访问控制

2.2 阻止匿名访问和切换根目录

匿名访问和切换根目录都会给服务器带来安全风险,我们把这两个功能关闭。

编辑 /etc/vsftpd/vsftpd.conf,找到下面两处配置并修改:

 

# 禁用匿名用户  12 YES 改为NO
anonymous_enable=NO

# 禁止切换根目录 101 行 删除#
chroot_local_user=YES

编辑完成后保存配置,重新启动 FTP 服务

service vsftpd restart

2.3 创建 FTP 用户

创建一个用户 ftpuser

useradd ftpuser

为用户 ftpuser 设置密码

echo "javen205" | passwd ftpuser --stdin

2.4 限制该用户仅能通过 FTP 访问

限制用户 ftpuser只能通过 FTP 访问服务器,而不能直接登录服务器:

usermod -s /sbin/nologin ftpuser

2.5 为用户分配主目录

为用户 ftpuser创建主目录并约定:

/data/ftp 为主目录, 该目录不可上传文件 
/data/ftp/pub 文件只能上传到该目录下

/data中创建相关的目录

mkdir -p /data/ftp/pub

2.5.1 创建登录欢迎文件

 

echo "Welcome to use FTP service." > /data/ftp/welcome.txt

设置访问权限

chmod a-w /data/ftp && chmod 777 -R /data/ftp/pub

设置为用户的主目录:

usermod -d /data/ftp ftpuser

 

usermod -s /sbin/nologin  go778995

chmod a-w /opt/zjx/ftp 

usermod -d  /opt/zjx/ftp  go778995

3、访问FTP

根据您个人的工作环境,选择一种方式来访问已经搭建的 FTP 服务

注意:记得关闭防火墙或者开放FTP默认端口(21)

# 关闭SELinux服务
setenforce 0 
# 关闭防火墙
iptables -F 
  • 通过 Windows 资源管理器访问

Windows 用户可以复制下面的链接 
到资源管理器的地址栏访问:

ftp://ftpuser:javen205@192.168.1.170 

其中ftpuser为登录FTP的用户名,javen205为登录FTP的密码

 

自己实际配置可正常访问下载的vsftpd.conf配置文件

# 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=NO
#
# 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.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#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.
# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CentOS 7.9搭建FTP服务器的步骤如下: 1. 首先,确保已经安装了vsftpd软件包。可以通过以下命令来安装:`sudo yum install vsftpd`。 2. 安装完成后,启动vsftpd服务。使用以下命令启动服务:`sudo systemctl start vsftpd`。 3. 接下来,设置vsftpd服务开机自启动。使用以下命令来设置:`sudo systemctl enable vsftpd`。 4. 默认情况下,匿名用户是不允许访问FTP服务器的。如果需要允许匿名用户访问,可以编辑vsftpd的配置文件`/etc/vsftpd/vsftpd.conf`,并找到配置项`anonymous_enable`,将其设置为`YES`。 5. 配置本地用户的访问权限。在vsftpd的配置文件中,可以通过添加以下配置来设置本地用户的访问权限: ``` userlist_enable=YES userlist_file=/etc/vsftpd/user_list userlist_deny=NO ``` 然后,创建`/etc/vsftpd/user_list`文件,并在文件中添加允许访问FTP服务器的本地用户的用户名。 6. 设置FTP服务器的根目录。默认情况下,FTP用户的根目录位于`/var/ftp`目录下。如果需要修改根目录,可以编辑vsftpd的配置文件,并找到配置项`local_root`,将其设置为所需的目录。 7. 最后,重启vsftpd服务以应用配置的更改。使用以下命令来重启服务:`sudo systemctl restart vsftpd`。 完成上述步骤后,CentOS 7.9上的FTP服务器搭建完成。您可以使用FTP客户端来连接并访问该服务器,具体的连接配置取决于您使用的FTP客户端。 ### 回答2: CentOS 7.9 系统可以使用 vsftpd(Very Secure FTP Daemon)来搭建 FTP 服务器。下面是基本的步骤: 1. 使用管理员身份登录到 CentOS 7.9 服务器。 2. 打开终端,并使用以下命令安装 vsftpd: ``` sudo yum install vsftpd ``` 3. 安装完成后,使用以下命令启动 vsftpd 服务,并设置开机自启动: ``` sudo systemctl start vsftpd sudo systemctl enable vsftpd ``` 4. 接下来,需要配置 vsftpd。使用以下命令编辑配置文件: ``` sudo vi /etc/vsftpd/vsftpd.conf ``` 在文件中,你可以根据需要进行以下设置: - 禁用匿名用户:将 `anonymous_enable=YES` 修改为 `anonymous_enable=NO`。 - 启用本地用户登录:取消注释(删除行首的 `#`)`local_enable=YES`。 - 允许本地用户上传文件:取消注释(删除行首的 `#`)`write_enable=YES`。 你还可以根据需要进行其他设置,然后保存并关闭文件。 5. 在防火墙中打开 FTP 服务器的端口。默认情况下,FTP 使用的端口是 20 和 21。你可以使用以下命令做出相应修改: ``` sudo firewall-cmd --zone=public --add-service=ftp --permanent sudo firewall-cmd --reload ``` 6. 接下来,你需要允许本地用户登录 FTP 服务器并设置其访问目录权限。使用以下命令添加用户: ``` sudo useradd ftpuser ``` 使用以下命令设置用户的密码: ``` sudo passwd ftpuser ``` 使用以下命令将用户的主目录设置为 FTP 目录: ``` sudo usermod -d /var/ftp ftpuser ``` 7. 最后,重新启动 vsftpd 服务以应用配置: ``` sudo systemctl restart vsftpd ``` 现在,你的 CentOS 7.9 服务器已经成功搭建了 FTP 服务器。通过使用 FTP 客户端工具,你可以使用添加的用户名和密码进行连接,并访问指定的 FTP 目录。 ### 回答3: 搭建FTP服务器可以实现网络文件传输和共享,CentOS 7.9是一种常用的Linux操作系统,以下是关于如何在CentOS 7.9上搭建FTP服务器的步骤: 1. 首先,使用root权限登录到CentOS 7.9服务器。 2. 安装vsftpd软件包,vsftpd是一种常用的FTP服务器软件。在终端中使用以下命令进行安装:yum install vsftpd -y 3. 安装完毕后,启动vsftpd服务并将其设置为开机自启动:systemctl start vsftpd && systemctl enable vsftpd 4. 默认情况下,vsftpd的配置文件位于/etc/vsftpd/vsftpd.conf。使用文本编辑器(如vi或nano)打开此文件:vi /etc/vsftpd/vsftpd.conf 5. 根据需要进行配置更改。一些常见的配置选项包括:启用匿名访问(anonymous_enable=YES)、禁止匿名用户上传(write_enable=NO)、添加本地用户访问(local_enable=YES)等。根据需要调整这些选项。 6. 保存并关闭配置文件后,重新启动vsftpd服务以使更改生效:systemctl restart vsftpd 7. 防火墙可能会阻止FTP访问。如果需要,可以使用firewalld或iptables等工具配置防火墙规则,允许FTP流量通过。 8. 检查FTP服务器的工作情况。您可以通过在Web浏览器中输入“ftp://your_server_ip”访问FTP服务器。使用服务器的IP地址来替换“your_server_ip”。 9. 如果需要,您可以进一步调整FTP服务器的相关配置,如限制用户的访问路径、设置访问权限和配额等。 通过以上步骤,您应该能够在CentOS 7.9上成功搭建FTP服务器。根据需要,您可以对FTP服务器进行进一步的配置和定制化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值