搭建ftp服务
安装FTP
yum install vsftpd -y
如果提示无法安装可以尝试替换yum源
验证是否安装成功
[root@localhost ~]# rpm -q vsftpd
vsftpd-3.0.5-5.el9.x86_64
关闭防火墙
# 关闭防火墙
[root@localhost ~]# systemctl stop firewalld
# 验证防火墙是否关闭 Active: inactive (dead)即为关闭
[root@localhost ~]# systemctl status firewalld
○ firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled)
Active: inactive (dead) since Fri 2024-05-10 19:34:25 CST; 1min 1s ago
Duration: 1d 3h 58min 15.046s
Docs: man:firewalld(1)
Process: 917 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/>
Main PID: 917 (code=exited, status=0/SUCCESS)
CPU: 578ms
5月 09 15:36:08 localhost systemd[1]: Starting firewalld - dynamic firewall daemon...
5月 09 15:36:10 localhost systemd[1]: Started firewalld - dynamic firewall daemon.
5月 10 19:34:25 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
5月 10 19:34:25 localhost.localdomain systemd[1]: firewalld.service: Deactivated successfully.
5月 10 19:34:25 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
关闭安全策略
# 关闭安全策略
[root@localhost ~]# setenforce 0
# 验证是否关闭 Permissive为关闭
[root@localhost ~]# getenforce
Permissive
开启ftp服务
# 启动ftp服务
[root@localhost ~]# systemctl start vsftpd
# 验证ftp服务是否启动 Active: active (running)为正在运行
[root@localhost ~]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; preset: disabled)
Active: active (running) since Fri 2024-05-10 19:15:05 CST; 26min ago
Process: 34495 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 34496 (vsftpd)
Tasks: 1 (limit: 10684)
Memory: 708.0K
CPU: 3ms
CGroup: /system.slice/vsftpd.service
└─34496 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
5月 10 19:15:05 localhost.localdomain systemd[1]: Starting Vsftpd ftp daemon...
5月 10 19:15:05 localhost.localdomain systemd[1]: Started Vsftpd ftp daemon.
允许匿名用户
需修改/etc/vsftpd/vsftpd.conf
文件
vi /etc/vsftpd/vsftpd.conf
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=YES (绑定到listen_port指定的端口,既然都绑定了也就是每时都开着的,就是standalone模式)
#
# 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=no
#
# 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 (允许为目录配置显示信息,显示每个目录下面的message_file文件的内容)
#
# 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)
#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 (允许使用\”async ABOR\”命令,一般不用,容易出问题)
#
# 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 模式上传。默认值为NO)
#ascii_download_enable=YES (管控是否可用ASCII 模式下载。默认值为NO)
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service. (login时显示欢迎信息.如果设置了banner_file则此设置无效)
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES (如果匿名用户需要密码,那么使用banned_email_file里面的电子邮件地址的用户不能登录)
# (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 (如果启动这项功能,则所有列在chroot_list_file中的使用者不能更改根目录)
# (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 (是否能使用ls -R命令以防止浪费大量的服务器资源)
#
# Customization
#
# 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 (定义PAM 所使用的名称,预设为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
允许匿名用户需要配置以下内容
anonymous_enable=YES #支持匿名用户登录
anon_upload_enable=YES #支持匿名用户上传
anon_mkdir_write_enable=YES #支持匿名用户改写
anon_other_write_enable=YES #允许匿名用户拥有其他写权限
anon_root=/var/ftp/pub #访问文件夹
chown_uploads=YES #允许修改匿名用户上传文件的所有者
chown_username=root #设置匿名用户上传文件的所有者
我的配置文件如下
listen=YES
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/vsftpd.log
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
anon_other_write_enable=YES
anon_root=/var/ftp/pub
chown_uploads=YES
chown_username=root
配置完后重启ftp服务
systemctl restart vsftpd
使用ftp来上传下载文件
安装ftp
# 使用yum安装ftp
[root@localhost ~]# yum install ftp -y
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:0:00:23 前,执行于 2024年05月10日 星期五 20时12分23秒。
依赖关系解决。
=========================================================================================================
软件包 架构 版本 仓库 大小
=========================================================================================================
安装:
ftp x86_64 0.17-89.el9 ali_appstream 62 k
事务概要
=========================================================================================================
安装 1 软件包
总下载:62 k
安装大小:112 k
下载软件包:
ftp-0.17-89.el9.x86_64.rpm 269 kB/s | 62 kB 00:00
---------------------------------------------------------------------------------------------------------
总计 266 kB/s | 62 kB 00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : ftp-0.17-89.el9.x86_64 1/1
运行脚本: ftp-0.17-89.el9.x86_64 1/1
验证 : ftp-0.17-89.el9.x86_64 1/1
已更新安装的产品。
已安装:
ftp-0.17-89.el9.x86_64
完毕!
# 验证是否安装成功
[root@localhost ~]# rpm -q ftp
ftp-0.17-89.el9.x86_64
Linux FTP命令的基本语法
FTP命令的基本语法如下:ftp [options] [host]
options
是FTP命令的选项,host
是您要连接的远程FTP服务器的IP地址或域名。
详细ftp命令可以查看
连接ftp
命令 ftp ip
# 连接刚刚搭建好的ftp服务
[root@localhost ~]# ftp 192.168.22.129
Connected to 192.168.22.129 (192.168.22.129).
220 (vsFTPd 3.0.5)
# 输入除root的用户登录
Name (192.168.22.129:root): bm
331 Please specify the password.
# 输入用户名对应的密码
Password:
# 出现下面提示为连接成功
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
匿名登录
# 连接刚刚搭建好的ftp服务
[root@localhost ~]# ftp 192.168.22.129
Connected to 192.168.22.129 (192.168.22.129).
220 (vsFTPd 3.0.5)
# 输入匿名用户名anonymous
Name (192.168.22.129:root): anonymous
331 Please specify the password.
# 密码直接回车跳过
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
上传/下载ftp文件
# 查询列表文件
ftp> ls
227 Entering Passive Mode (192,168,22,129,236,221).
150 Here comes the directory listing.
drwxrwxrwx 2 0 0 18 May 10 12:47 pub
226 Directory send OK.
# 进入pub目录
ftp> cd pub
250 Directory successfully changed.
# 再查询一下
ftp> ls
227 Entering Passive Mode (192,168,22,129,90,97).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 12 May 10 12:47 beim
226 Directory send OK.
# 尝试下载beim文件(我已经提前在pub文件夹里面放了beim文件),下载成功
ftp> get beim
local: beim remote: beim
227 Entering Passive Mode (192,168,22,129,73,194).
150 Opening BINARY mode data connection for beim (12 bytes).
226 Transfer complete.
12 bytes received in 2.5e-05 secs (480.00 Kbytes/sec)
# 上传beim1文件到ftp,也需要提前创建,或者上传一个已经存在的文件,上传完成
ftp> put beim1
local: beim1 remote: beim1
227 Entering Passive Mode (192,168,22,129,205,105).
150 Ok to send data.
226 Transfer complete.
12 bytes sent in 6.4e-05 secs (187.50 Kbytes/sec)
注意:如果无法上传下载可以设置pub目录的权限
chmod 777 /var/ftp/pub
© 版权声明
THE END
暂无评论内容