ftp 文件传输协议,目的就是为了传输文件,但是现在用得已经很少了。
FTP的工作模式分为2中,主动和被动,无论主动还是被动都是相对于服务器来说的。
FTP在工作时会产生两个链接占用两个端口,一个是命令连接,一个是数据连接,命令连接时一直存在的,数据连接是在传输数据时存在,目的就是比如传输一个大文件时,中途想放弃,就要输入指令终止操作,很多的软件都可以一个连接实现的。
1、主动模式(服务器端开启的端口是tcp 20和21;21端口传输命令,20端口传输数据)
主动模式首先客户端请求服务器端的21端口(命令端口),建立命令连接;当客户端想要传输数据时,发送PORT命令,告诉服务器“我打开了xxx端口,快理我”,服务器使用20端口与客户端建立数据连接。
2、被动模式(服务器端开启的端口是tcp 21和一个大于1024的随机端口;21端口传输命令,另外一个端口传输数据)
被动模式首先客户端请求服务器端的21端口,建立命令连接;当客户端想要传输数据时,发送PASV命令,表示使用被动模式传输数据,服务器端会告诉客户端“我开启了xxx端口,快给我”,然后客户端就会连接服务器通知它的那个端口,进行数据传输。
首先 ftp分为服务端和客户端,
服务端软件叫 vsftpd 客户端叫 ftp
如果一个服务器只用于做服务端,那么安装vsftpd 就可以了。
[root@www gsc-test-ssh]# yum install vsftpd
Running transaction
Installing : vsftpd-3.0.2-22.el7.x86_64 1/1
Verifying : vsftpd-3.0.2-22.el7.x86_64 1/1
Installed:
vsftpd.x86_64 0:3.0.2-22.el7
Complete!
FTP的一些配置文件含义。
anonymous_enable=YES // 允许匿名用户登录 anonymous:匿名
local_enable=YES //允许本地用户登录
write_enable=YES //允许本地用户写操作
local_umask=022 //本地权限掩码
#anon_upload_enable=YES //默认匿名用户是不允许上传文件的
#anon_mkdir_write_enable=YES //是否允许匿名用户创建目录
dirmessage_enable=YES //显示目录信息
xferlog_enable=YES //对上传和下载记录日志
connect_from_port_20=YES //port模式使用的数据端口
#chown_uploads=YES //与上传文件的所有者相关的设置
#chown_username=whoever //上传的文件的所有者是谁,前提:该用户是真实存在的
#xferlog_file=/var/log/xferlog //默认日志记录位置
xferlog_std_format=YES //使用默认位置记录日志
#idle_session_timeout=600 //空闲会话超时时间
#data_connection_timeout=120 //数据连接超时时间
#chroot_local_user=YES //本地用户牢笼
#chroot_list_enable=YES //开启牢笼用户列表
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list //列表文件
listen=YES //是否监听
pam_service_name=vsftpd //pam验证
userlist_enable=YES //开启用户列表
#userlist_deny=YES //默认没有该行,默认是开启黑名单的
tcp_wrappers=YES //关于安全的限制
安装ftp服务之后,系统会创建用户ftp,当匿名用户登录到ftp时,默认位置就在ftp的家目录下。
登录和查看。
匿名用户就是用ftp用户登录的,密码不写直接回车。
[root@geili gsc-test]# ftp 172.16.12.15
Connected to 172.16.12.15 (172.16.12.15).
220 (vsFTPd 3.0.2)
Name (172.16.12.15:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,15,36,80).
150 Here comes the directory listing.
drwxr-xr-x 2 0 0 6 Aug 03 2017 pub
226 Directory send OK.
ftp>
看到这里有个 pub目录
[root@www ftp]# ls
pub
[root@www ftp]# pwd
/var/ftp
pub正好在这里。
[root@www ftp]# grep ftp /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
那个位置就是家目录。
ftp中最简单的操作就是下载上传了,不过匿名用户要想访问的话西药修改配置文件中的属性,允许匿名用户上传文件和创建目录。。
常用的命令:
ls —— 查看列表
cd —— 切换服务器目录
lcd —— 切换本地路径
put —— 上传文件
mput —— 上传多个文件
get —— 下载文件
mget —— 下载多个文件
delete —— 删除文件
切换本地路径需要解释下,在登录ftp之前用户一定在一个目录下,登录ftp后下载也默认存在登录ftp前在的那个目录下,lcd就是可以在ftp系统内修改这个目录的位置。
从ftp下载文件。
[root@geili gsc-test]# ftp 172.16.12.15
Connected to 172.16.12.15 (172.16.12.15).
220 (vsFTPd 3.0.2)
Name (172.16.12.15:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,15,190,224).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Nov 01 11:16 12.15ftp
drwxr-xr-x 2 0 0 6 Aug 03 2017 pub
226 Directory send OK.
ftp> get 12.15ftp onefile.txt
local: onefile.txt remote: 12.15ftp
227 Entering Passive Mode (172,16,12,15,164,106).
150 Opening BINARY mode data connection for 12.15ftp (0 bytes).
226 Transfer complete.
ftp> exit
221 Goodbye.
[root@geili gsc-test]# ls
12.15file.txt ggg.txt index.html lkkk onefile.txt
下载12.15ftp这个文件并改名存下。 这些操作和linux中类似。
匿名用户创建和上传文件。
服务端配置文件将下面这两行注释取消。
anon_upload_enable=YES //默认匿名用户是不允许上传文件的
anon_mkdir_write_enable=YES //是否允许匿名用户创建目录
重启服务
[root@www ftp]# systemctl restart vsftpd
创建一个目录并赋予权限。因为如果目录本身没有权限 那也是不能创建和上传文件的。
[root@www ftp]# mkdir gggss
[root@www ftp]# chmod 1777 gggss/
进ftp测试:
ftp> ls
227 Entering Passive Mode (172,16,12,15,77,133).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Nov 01 11:16 12.15ftp
drwxrwxrwt 2 0 0 6 Nov 01 11:26 gggss
drwxr-xr-x 2 0 0 6 Aug 03 2017 pub
226 Directory send OK.
ftp> cd gggss
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,12,15,155,137).
150 Here comes the directory listing.
226 Directory send OK.
ftp> mkdir adie
257 "/gggss/adie" created
ftp> ls
227 Entering Passive Mode (172,16,12,15,32,14).
150 Here comes the directory listing.
drwx------ 2 14 50 6 Nov 01 11:26 adie
226 Directory send OK.
ftp>
进入gggss目录能够创建目录。
上传文件也是如此,用put。
如果是用服务端本地用户登录,初始的目录在用户的家目录。
而这时可以随意跳转,可以随意跳到其他目录。这是合理的,因为用户就是服务端的用户啊。但是这并不安全。
[root@www ftp]# ftp 172.16.12.10
Connected to 172.16.12.10 (172.16.12.10).
220 (vsFTPd 3.0.2)
Name (172.16.12.10:root): 666777
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,10,227,32).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/home/666777"
ftp> cd /etc
250 Directory successfully changed.
ftp>
-rw-r--r-- 1 0 0 0 Nov 05 2016 subgid
-rw-r--r-- 1 0 0 0 Nov 05 2016 subuid
-rw-r----- 1 0 0 3181 Jun 07 2017 sudo-ldap.conf
-rw-r----- 1 0 0 1786 Jun 07 2017 sudo.conf
-r--r----- 1 0 0 3938 Jun 07 2017 sudoers
drwxr-x--- 2 0 0 6 Aug 04 2017 sudoers.d
drwxr-xr-x 6 0 0 4096 Nov 01 08:12 sysconfig
-rw-r--r-- 1 0 0 449 Aug 03 2017 sysctl.conf
drwxr-xr-x 2 0 0 28 Oct 28 04:05 sysctl.d
lrwxrwxrwx 1 0 0 14 Oct 28 04:02 system-release -> centos-release
-rw-r--r-- 1 0 0 23 Aug 30 2017 system-release-cpe
drwxr-xr-x 4 0 0 151 Oct 28 09:44 systemd
drwxr-xr-x 2 0 0 6 Jun 11 2014 terminfo
drwxr-xr-x 2 0 0 6 Aug 05 2017 tmpfiles.d
-rw-r--r-- 1 0 0 750 Aug 04 2017 trusted-key.key
drwxr-xr-x 2 0 0 70 Oct 28 04:06 tuned
drwxr-xr-x 3 0 0 54 Oct 28 04:49 udev
-rw-r--r-- 1 0 0 37 Oct 28 04:23 vconsole.conf
-rw-r--r-- 1 0 0 1982 Aug 02 2017 virc
drwxr-xr-x 5 0 0 231 Oct 28 04:06 vmware-tools
drwxr-xr-x 2 0 0 88 Nov 01 02:43 vsftpd
drwxr-xr-x 2 0 0 33 Oct 28 04:05 wpa_supplicant
drwxr-xr-x 4 0 0 38 Oct 28 04:05 xdg
drwxr-xr-x 2 0 0 6 Nov 05 2016 xinetd.d
drwxr-xr-x 6 0 0 100 Oct 28 04:05 yum
-rw-r--r-- 1 0 0 970 Aug 05 2017 yum.conf
drwxr-xr-x 2 0 0 4096 Nov 01 01:51 yum.repos.d
226 Directory send OK.
ftp>
服务端的系统就这样被ftp在远端随意访问。
牢笼-----本地用户chroot限制。
牢笼就是让服务器本地用户不能访问其他用户的限制。
以下是相关配置文件:
# 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 // 牢笼白名单的位置和文件名,可以随意写
allow_writeable_chroot=YES //解决不能登录的问题。
[root@www ftp]# ftp 172.16.12.10
Connected to 172.16.12.10 (172.16.12.10).
220 (vsFTPd 3.0.2)
Name (172.16.12.10:root): 8888
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,12,10,157,93).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd /etc/
550 Failed to change directory. //开启牢笼后切换目录显示没有权限。
在白名单内 缺失可以访问的
ftp> cd /etc
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,12,10,145,18).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 5090 Nov 04 2016 DIR_COLORS
-rw-r--r-- 1 0 0 5725 Nov 04 2016 DIR_COLORS.256color
-rw-r--r-- 1 0 0 4669 Nov 04 2016 DIR_COLORS.lightbgcolor
-rw-r--r-- 1 0 0 94 Mar 24 2017 GREP_COLORS
-rw-r--r-- 1 0 0 842 Nov 05 2016 GeoIP.conf
-rw-r--r-- 1 0 0 858 Nov 05 2016 GeoIP.conf.default
黑名单和白名单:
1、ftp默认情况下是开启的黑名单
userlist_enable=YES //开启用户列表功能
userlist_deny=YES //拦截
黑名单开启,默认写在/etc/vsftpd/user_list文件中的用户是不允许登录ftp服务器的
[root@localhost /]# vim /etc/vsftpd/user_list
8888 //添加该行
[root@localhost /]# ftp 172.16.12.10
Connected to 172.16.12.10 (172.16.12.10).
220 (vsFTPd 2.2.2)
Name (172.16.12.10:root): 8888 //注意用户名不要多打空格等字符
530 Permission denied.
Login failed.
2、白名单——只有写在user_list中的普通用户才可以登录ftp
[root@localhost /]# vim /etc/vsftpd/vsftpd.conf
userlist_enable=YES
userlist_deny=NO
[root@localhost /]# systemctl restart vsftpd
[root@localhost /]# ftp 172.16.12.10
Name (172.16.12.10:root): 8888
331 Please specify the password.
Password:
ftp> pwd
257 "/home/uplooking"
ftp> exit
221 Goodbye.
FTP 要点解析:
1、ftp中分为3种用户
1、匿名用户
默认只能下载,不能上传
2、本地用户
使用useradd添加的用户 ,也就是服务端系统用户
3、虚拟用户
其权限模拟的是匿名用户,其登录方式模拟的是本地用户,默认只能下载不能上传
2、开启匿名用户创建和上传文件的权限后,匿名用户可以上传和下载文件,也能创建目录,但是目录的权限是600,并且目录的属主是ftp用户,虽然匿名用户用ftp用户登录,但是也不能看这个目录中的内容。因为没有权限。这个很绕,虽然用ftp用户登录,但你不是ftp用户,没有权限…
3、匿名用户如果想在一个目录中上传文件那么对这个目录要有权限。目录本身没有权限也不能操作。毕竟登录ftp的目录时服务端ftp用户的家目录嘛。
4、 root用户是否能够登录ftp不光取决于/etc/vsftpd/user_list文件,还要看/etc/vsftpd/ftpusers文件。
要是想让root能够登录FTP有两种做法:
1、将/etc/vsftpd/user_list和/etc/vsftpd/ftpusers文件中的root删掉即可
2、修改vsftpd的配置文件,将黑名单改成白名单,修改如下:
1)在配置文件中添加一行:
userlist_deny=NO
2)重启服务
systemctl restart vsftpd
3)将/etc/vsftpd/ftpusers中的root删掉或者注释掉
5、查看连接的客户端数量
[root@srv-200 ~]# netstat -antp | grep ftp | grep EST | wc -l
6、使用时建议关闭selinux
固定ftp端口:
依旧是修改/etc/vsftpd/vsftpd.conf
配置文件。
pasv_min_port=20070
pasv_max_port=20075
最小端口和最大端口,这样就可以控制随机端口的开放了。