目录
2. 了解ftp服务的配置文件 /etc/ftp/vsftpd
ftp 服务:
实现ftp功能的一个服务,安装vsftpd软件搭建一台ftp服务器
ftp协议:
文件传输协议 (file transfer protocol),在不同的机器之间实现文件传输功能,
例如
视频文件下载,源代码文件下载
公司内部:弄一个专门的文件服务器,将公司里的文档资料和视频都存放在服务器里面,所有的同事都可以去下载,起到集中存放文件的地方
1. 搭建ftp服务器
1.1 .安装vsftpd软件
yum install vsftpd -y
1.1.1 查看vsftpd软件的信息:
rpm -qi vsftpd
-
[root@localhost ~]
# rpm -qi vsftpd
-
Name : vsftpd
-
Version : 3.0.2
-
Release : 29.el7_9
-
Architecture: x86_64
-
Install Date: 2022年04月09日 星期六 00时40分08秒
-
Group : System Environment/Daemons
-
Size : 361349
-
License : GPLv2 with exceptions
-
Signature : RSA/SHA256, 2021年06月11日 星期五 23时06分15秒, Key ID 24c6a8a7f4a80eb5
-
Source RPM : vsftpd-3.0.2-29.el7_9.src.rpm
-
Build Date : 2021年06月10日 星期四 00时15分50秒
-
Build Host : x86-02.bsys.centos.org
-
Relocations : (not relocatable)
-
Packager : CentOS BuildSystem http://bugs.centos.org
-
Vendor : CentOS
-
URL : https://security.appspot.com/vsftpd.html
-
Summary : Very Secure Ftp Daemon
-
Description :
-
vsftpd is a Very Secure FTP daemon. It was written completely from
-
scratch.
vsftpd is a Very Secure FTP daemon.
vsftpd 是一个非常安全的ftp守护进程
daemon : 守护进程
守护进程:一直在内存里运行的进程,不会自动停止,除非人为停止,一般是对外提供服务的。
#######################################
1. 2 .启动vsftpd软件
service vsftpd start(centos 7)
-
[root@localhost ~]
# service vsftpd start
-
Redirecting to /bin/systemctl start vsftpd.service
#######################################
1.2.1 如何知道一个进程已经启动?
1.查看进程
ps aux | grep vsftpd
-
[root@localhost ~]
# ps aux | grep vsftpd
-
root 1690 0.0 0.0 53292 580 ? Ss 00:45 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
-
root 1692 0.0 0.0 112824 980 pts/0 S+ 00:46 0:00 grep --color=auto vsftpd
#######################################
2.查看端口号
netstat -anplut | grep vsftpd
-
[root@localhost network-scripts]
# netstat -anplut | grep vsftpd
-
tcp6 0 0 :::21 :::* LISTEN 1888/vsftpd
#######################################
1.3. 登录ftp服务
1.3.1 下载客户端工具 ftp和lftp
-
[root@localhost ~]
# yum install ftp lftp -y
-
已安装:
-
ftp.x86_64 0:0.17-67.el7 lftp.x86_64 0:4.4.8-12.el7_8.1
-
作为依赖被安装:
-
gnutls.x86_64 0:3.3.29-9.el7_6 nettle.x86_64 0:2.7.1-9.el7_9
-
trousers.x86_64 0:0.3.14-2.el7
-
完毕!
ftp访问:
默认情况下不允许root用户登录
vsftpd 使用的用户名和密码就是linux系统里面使用分用户和密码,不需要单独新建
-
[root@localhost ~]
# lftp 10.107.12.224
-
lftp 10.107.12.224:~>
ls
-
`
ls
' at 0 [正在连接...]
-
中断
-
lftp 10.107.12.224:~> exit
我们可以新建一个用户来测试ftp服务
新建用户sc
-
[root@localhost ~]
# useradd sc
-
[root@localhost ~]
# echo 123|passwd sc --stdin
-
更改用户 sc 的密码 。
-
passwd:所有的身份验证令牌已经成功更新。
#######################################
1.3.2 登录lftp
ftp使用的命令和linux基本相同,我们在ftp新建的文件夹都是存放在服务器上面的。
格式: lftp -u 用户名,密码 ip地址
-
[root@localhost ~]
# lftp -u sc,123 10.107.12.244
-
lftp sc@10.107.12.244:~>
ls
-
lftp sc@10.107.12.244:~>
pwd
-
ftp://sc:123@10.107.12.244/%2Fhome/sc
-
lftp sc@10.107.12.244:~>
mkdir sanchuang
-
mkdir 成功, 建立 `sanchuang
'
-
lftp sc@10.107.12.244:~> ls
-
drwxr-xr-x 2 1000 1000 6 Apr 08 17:01 sanchuang
-
#######################################
1.3.3 查看lftp常用命令:
-
lftp sc@10.107.12.244:~>
help
-
!<shell-command> (commands)
-
alias [<name> [<value>]] attach [PID]
-
bookmark [SUBCMD] cache [SUBCMD]
-
cat [-b] <files>
cd <rdir>
-
chmod [OPTS] mode file... close [-a]
-
[re]cls [opts] [path/][pattern]
-
debug [<level>|off] [-o <file>]
du [options] <
dirs>
-
exit [<code>|
bg]
-
get [OPTS] <rfile> [-o <lfile>] glob [OPTS] <cmd> <args>
-
help [<cmd>]
-
history -w file|-r file|-c|-l [cnt]
jobs [-v] [<job_no...>]
-
kill all|<job_no> lcd <ldir>
-
lftp [OPTS] <site>
ln [-s] <file1> <file2>
-
ls [<args>] mget [OPTS] <files>
-
mirror [OPTS] [remote [
local]]
mkdir [-p] <
dirs>
-
module name [args] more <files>
-
mput [OPTS] <files> mrm <files>
-
mv <file1> <file2> [re]nlist [<args>]
-
open [OPTS] <site>
-
pget [OPTS] <rfile> [-o <lfile>]
-
put [OPTS] <lfile> [-o <rfile>]
pwd [-p]
-
queue [OPTS] [<cmd>] quote <cmd>
-
repeat [OPTS] [delay] [
command]
rm [-r] [-f] <files>
-
rmdir [-f] <
dirs> scache [<session_no>]
-
set [OPT] [<var> [<val>]] site <site-cmd>
-
source <file>
-
torrent [-O <
dir>] <file|URL>... user <user|URL> [<pass>]
-
wait [<jobno>] zcat <files>
-
zmore <files>
#######################################
1.3.4 lcd命令 : 在本地切换目录
1.3.5 !ls : 查看本地目录内容
-
lftp sc@10.107.12.244:~> lcd /boot
-
lcd 成功, 本地目录=/boot
-
lftp sc@10.107.12.244:~> !
ls
-
config-3.10.0-1160.el7.x86_64
-
efi
-
grub
-
grub2
-
initramfs-0-rescue-649c8558acc34bc7b450781cc67d2041.img
-
initramfs-3.10.0-1160.el7.x86_64.img
-
initramfs-3.10.0-1160.el7.x86_64kdump.img
-
symvers-3.10.0-1160.el7.x86_64.gz
-
System.map-3.10.0-1160.el7.x86_64
-
vmlinuz-0-rescue-649c8558acc34bc7b450781cc67d2041
-
vmlinuz-3.10.0-1160.el7.x86_64
#######################################
1.3.6 put命令 上传文件到ftp服务器·
-
lftp sc@10.107.12.244:~> put initramfs-3.10.0-1160.el7.x86_64.img
-
21721039 bytes transferred
-
lftp sc@10.107.12.244:~>
ls
-
-rw-r--r-- 1 1000 1000 21721039 Apr 08 17:06 initramfs-3.10.0-1160.el7.x86_64.img
-
drwxr-xr-x 2 1000 1000 6 Apr 08 17:01 sanchuang
#######################################
1.3.7 mirror命令 下载文件夹到本地
-
lftp sc@10.107.12.244:~> mirror sanchuang/
-
Total: 1 directory, 0 files, 0 symlinks
-
lftp sc@10.107.12.244:~> !
ls
-
config-3.10.0-1160.el7.x86_64
-
efi
-
grub
-
grub2
-
initramfs-0-rescue-649c8558acc34bc7b450781cc67d2041.img
-
initramfs-3.10.0-1160.el7.x86_64.img
-
initramfs-3.10.0-1160.el7.x86_64kdump.img
-
sanchuang
-
symvers-3.10.0-1160.el7.x86_64.gz
-
System.map-3.10.0-1160.el7.x86_64
-
vmlinuz-0-rescue-649c8558acc34bc7b450781cc67d2041
-
vmlinuz-3.10.0-1160.el7.x86_64
#######################################
2. 了解ftp服务的配置文件 /etc/ftp/vsftpd
-
[root@localhost ~]
# cd /etc/vsftpd
-
[root@localhost vsftpd]
# ls
-
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh
一般下载vsftpd服务后有四个重要的配置文件
#######################################
2.1 vsftpd.conf 主配置文件
2.1.1 一些常用配置:
是否允许匿名用户ftp登录,YES代表允许,NO不允许
anonymous_enable=YES
是否允许本地登录
local_enable=YES
是否有写的权利
write_enable=YES
日志文件位置
xferlog_file=/var/log/xferlog 采用标准的日志格式
#######################################
2.2 ftpusers : ftp黑名单
如果想让一个用户登录不了ftp服务,可以将用户加入黑名单
-
[root@localhost vsftpd]
# cat ftpusers
-
Users that are not allowed to login via ftp
-
root
-
bin
-
daemon
-
adm
-
lp
-
sync
-
shutdown
-
halt
-
mail
-
news
-
uucp
-
operator
-
games
-
nobody
#######################################
2.3 user_list 黑名单或白名单
If userlist_deny=NO, only allow users in this file
只有 userlist_deny=NO的时候,在这个文件里面所有的用户都可以使用ftp服务,这个时候文件就从黑名单变成白名单,
-
[root@localhost vsftpd]
# cat user_list
-
vsftpd userlist
-
If userlist_deny=NO, only allow
users
in this file
-
If userlist_deny=YES (default), never allow
users
in this file, and
-
do not even prompt
for a password.
-
Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
-
for
users that are denied.
-
root
-
bin
-
daemon
-
adm
-
lp
-
sync
-
shutdown
-
halt
-
mail
-
news
-
uucp
-
operator
-
games
-
nobody
#######################################
2.4 限制用户只能在自己家目录操作
如果让普通用户可以随便切换目录的话及其不安全
所以一般可以修改配置文件将用户限制在自己的家目录
在 vsftpd.conf 配置文件里面修改以下配置
chroot_list 文件默认不存在,需要在 /etc/vsftpd/下新建
-
#对本地用户限制在自己的家目录里
-
chroot_local_user
=YES
-
#启用限制名单
-
chroot_list_enable
=YES
-
(
default follows)
-
# 具体的名单路径,这个名单的用户不受限制,可以随意切换目录
-
chroot_list_
file
=
/etc
/vsftpd
/chroot_list
-
#这个目录没有,需要自己新建。
#######################################
示例 :新建用户bobo测试能否登陆
1.修改上述配置
2.在/etc/vsftpd 下面新建文件 chroot_llist
3.使用bobo用户登录
-
[root@localhost vsftpd]
# useradd bobo
-
[root@localhost vsftpd]
# echo 123|passwd bobo --stdin
-
更改用户 bobo 的密码 。
-
passwd:所有的身份验证令牌已经成功更新。
-
[root@localhost vsftpd]
# lftp -u bobo,123 192.168.44.132
-
lftp bobo@192.168.44.132:~>
ls
-
ls: 登录失败: 500 OOPS:
chroot
-
lftp bobo@192.168.44.132:~>
cd /home
-
cd: 登录失败: 500 OOPS:
chroot
-
lftp bobo@192.168.44.132:~>
会出现提示信息:500 oops:chroot
从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。
所以如果我们开启了限制用户在家目录的话,如果用户对家目录还有写权限的话就会报错
#######################################
解决方法:
1.给bobo家目录去除写的权限。
去除写的权利以后这类用户只能下载不能上传。
chmod -w bobo
-
[root@localhost home]# ll | grep bobo
-
drwx------.
2 bobo bobo
83
4月
10
19:
39 bobo
-
[root@localhost home]# chmod -w bobo
-
[root@localhost home]# ll | grep bobo
-
dr-x------.
2 bobo bobo
83
4月
10
19:
39 bobo
去除写的权利以后bobo用户就可以正常登陆,并且被限制在了家目录,
如果想让bobo用户正常切换路径可以将bobo恢复写的权限以后添加到chroot_list 文件里面
注意:
#######################################
linux系统的selinux可能会影响你的登陆
selinux是linux的一个内核模块,是linux的安全子系统。
SELinux 主要作用就是最大限度地减小系统中服务进程可访问的资源(最小权限原则)。
我们可以手动关闭它
getenforce 查看selinux状态
Enforcing 是代表强制的意思
setenforce 0 关闭selinux (临时)
Permissive 是放纵的,任由,
-
[root@localhost vsftpd]
# getenforce
-
Enforcing
-
[root@localhost vsftpd]
# setenforce 0
-
[root@localhost vsftpd]
# getenforce
-
Permissive
然后就可以正常登陆操作了。
可以看到我们正常登陆以后bobo用户就被限制在了家目录下面,不能随意切换目录了。
-
[root@localhost home]
# ll | grep bobo
-
drwx------. 2 bobo bobo 83 4月 10 19:39 bobo
-
[root@localhost home]
# chmod -w bobo
-
[root@localhost home]
# ll | grep bobo
-
dr-x------. 2 bobo bobo 83 4月 10 19:39 bobo
-
[root@localhost vsftpd]
# lftp -u bobo,123 192.168.44.132
-
lftp bobo@192.168.44.132:~>
ls
-
lftp bobo@192.168.44.132:/>
ls
-
lftp bobo@192.168.44.132:/>
cd /home
-
cd: Access failed: 550 Failed to change directory. (/home)
-
lftp bobo@192.168.44.132:/>
cd /root
-
cd: Access failed: 550 Failed to change directory. (/root)
-
lftp bobo@192.168.44.132:/>
cd /lianxi
-
cd: Access failed: 550 Failed to change directory. (/lianxi)
-
lftp bobo@192.168.44.132:/>
cd /var/ftp/pub
-
cd: Access failed: 550 Failed to change directory. (/var/ftp/pub)
-
-
注:这个时候我们还是可以使用lcd命令来切换本地的路径
但是我们依旧不能对文件进行操作。
-
lftp bobo@192.168.44.132:/> lcd /lianxi/ftp
-
lcd 成功, 本地目录=/lianxi/ftp
-
lftp bobo@192.168.44.132:/> !
ls
-
1111.txt rr.sh
-
lftp bobo@192.168.44.132:/>
mkdir bobo.sh
-
mkdir: Access failed: 550 Create directory operation failed. (bobo.sh)
-
lftp bobo@192.168.44.132:/> !
ls
-
1111.txt rr.sh
#######################################
2.修改vsftpd.conf 文件
添加allow_writeable_chroot=YES
添加这条规则
-
chroot_local_user=YES
-
chroot_list_enable=YES
-
# (default follows)
-
chroot_list_file=/etc/vsftpd/chroot_list
-
allow_writeable_chroot=YES
#######################################
3. 使用ftp服务在linux和windows传文件
3.1 使用linux向windows传文件。
搭建一个ftp服务,
#######################################
1.关闭防火墙
service firewalld stop
#######################################
2.启动vsftpd 服务
service vsftpd restart
将 rr.sh 复制到 /var/ftp/pub 目录下面
-
root@localhost lianxi2]
# cp rr.sh /var/ftp/pub/
-
-
[root@localhost lianxi2]
# cd /var/ftp/pub/
-
-
[root@localhost pub]
# ls
-
-
rr.sh
#######################################
3.登录ftp服务
这里新建一个用户hang
登录lftp客户端
可以看到这是因为我们开启了设置用户只能在自己家目录下面
-
[root@localhost vsftpd]
# useradd hang
-
[root@localhost vsftpd]
# echo 123 | passwd hang --stdin
-
更改用户 hang 的密码 。
-
passwd:所有的身份验证令牌已经成功更新。
-
[root@localhost vsftpd]
# lftp -u hang,123 192.168.44.132
-
lftp hang@192.168.44.132:~>
ls
-
lftp hang@192.168.44.132:/>
cd /var/ftp/pub
-
cd: Access failed: 550 Failed to change directory. (/var/ftp/pub)
-
解决方法:将 用户hang 加入到 /etc/vsftpd/chroot_list文件里
[root@localhost vsftpd]# vim chroot_list
添加以后hang用户就可以切换到/var/ftp/pub目录了。
-
[root@localhost vsftpd]
# lftp -u hang,123 192.168.44.132
-
lftp hang@192.168.44.132:~>
cd /var/ftp/pub
-
cd 成功, 当前目录=/var/ftp/pub
-
lftp hang@192.168.44.132:/var/ftp/pub>
ls
-
-rw-r--r-- 1 0 0 0 Apr 10 08:32 1111.txt
-
-rw-r--r-- 1 0 0 621 Mar 09 01:04 rr.sh
可以看到该目录下面有两个文件,
然后用在windows里面用浏览器访问ftp服务
注意:一定要关闭防火墙!
点击文件就可以下载了。
#######################################
3.2 使用windows向linux传文件。
使用资源浏览器访问ftp服务器
ftp://192.168.44.132/
右击登录
登录成功以后将要上传的文件拖进来就可以啦
在windows上传的文件保存在该登录用户的家目录里面。
-
[root
@localhost pub]
# cd /home/hang
-
[root
@localhost hang]
# ls
-
面试
??
?txt
这样就实现了windows和linux里面互传文件。