FTP Service

1.1 FTP简介

 
FTP(File Transfer Protocol),21/tcp,20/tcp,文件传输协议,工作在应用层,它最主要功能是在服务器与客户端之间进行文件的传输。
 
FTP是明文协议:认证及数据传输都是明文传输。
 
C/S架构
  • 服务端实现:vsftpd、pureftpd、proftpd、...Filezilla Server
  • 客户端实现:
    • Linux:ftp、lftp
    • Windows:cuteftp、Filezilla、Flashfxp,...
 
1.2 FTP的连接类型
 
命令连接:传输指令.21/tcp,客户端发出请求,服务端响应
数据连接:传输数据
  • 注意:数据连接必然是通过某个命令连接发起;
 
  • 主动模式:服务器向客户端发起数据传输请求;服务器端口:固定;
  • 被动模式:客户端向服务器端发起数据传输请求;服务器端口:半随机;
 
  • 数据传输格式:
    • ASCII
    • BINARY    
 
用户:资源位于用户的家目录下;
  • 匿名用户(映射到某一固定的系统用户):ftp、vsftp、/var/ftp/
  • 本地用户(系统用户):root及系统用户(0-999);
  • 虚拟用户:
    • nsswitch:name service switch(名称服务转换)
    • PAM:Plugable Authentication Modules(Plugable身份验证模块)
 
1.3 vsftpd Server配置
 
vsftpd:Very Secure Ftp Daemon
主程序:/usr/sbin/vsftpd
配置文件:/etc/vsftpd/vsftpd.conf
 
Centos 6:
/etc/rc.d/init.d/vsftpd
chkocnfig vsftpd on   #设置开机自动启动
 
Centos 7:
/usr/lib/systemd/system/vsftpd.service    #启动服务
systemctl enable vsftpd.service   #设置开机自动启动
 
1.3.1 安装vsftpd
 
yum install vsftpd -y
 
1.3.2 vsftpd配置文件
 
vsftpd配置文件:/etc/vsftpd/vsftpd.conf
cp vsftpd.conf{,.bak}
 
1、匿名用户
anonymous_enable=YES    # 是否启用匿名用户,不启用则输入值"NO"。
anon_upload_enable=YES    # 是否允许匿名用户的上传操作;生效要依赖于write_enable=YES;
write_enable=YES    # 是否允许用户上传操作
anon_mkdir_write_enable=YES    # 是否允许匿名用户创建目录的权限
anon_other_write_enable=YES    # 是否允许匿名用户的删除及重命名操作权限
 
允许匿名用户有上传文件,上传的目录必须有权限
[root@CentOS7-171 ftp]# mkdir upload
[root@CentOS7-171 ftp]# chown ftp upload
[root@CentOS7-171 ftp]# ll -d upload/
drwxr-xr-x 2 ftp root 6 May 31 16:21 upload/
 
2、本地用户
local_enable=YES    # 所有的非匿名用户的生效,都依赖于此指令;
local_umask=022    # 用于设置本地用户上传文件权限的掩码
 
3、目录消息
dirmessage_enable=YES    # 用户第一次进入目录时,vsftpd会查看 .message文件,并将其内容显示给用户;
message_file    # 指定文件路径,而不使用默认的.message;
 
4、数据传输日志
xferlog_enable    # 是否启用数据传输日志
xferlog_std_format    #  定义数据传输日志格式
xferlog_file=/var/log/xferlog    # 定义数据传输日志文件路径
 
5、数据传输模式
connect_from_port_20=YES    #是否启用PORT模式,服务器是否工作于主动模式
 
6、修改匿名用户上传的文件属主
chown_uploads    # 是否修改 匿名用户上传的文件属主
chown_username    # 启用chown_uploads指令时,将文件属主修改为此指令指定的用户;默认为root用户。
chown_upload_mode    # 设定匿名用户上传文件的权限,默认为600
 
7、设定会话超时时长:
idle_session_timeout    # 空闲会话超时时长,默认是300s;
connect_timeout    # PORT模式下,服务器连接客户端的超时时长,默认60s;
data_connection_timeout    # 数据传输过程中超时时长,默认300s
 
8、命令连接的监听端口:
listen_port    # 命令连接的监听端口,默认为21号端口;
 
9、设定连接及传输速率:
local_max_rate    # 本地用户的传输速率,单位是字节,默认为0,表示无限制;
max_clients    # 设定最大并发连接数,默认为2000个;
max_per_ip    # 每个IP所允许发起的最大连接数;
anon_max_rate    # 匿名用户的最大传输速率;
 
10、禁锢本地用户:
chroot_local_user=YES     # 禁锢所有本地用户;
    注意:要求用户不能对家目录有写权限;
 
禁锢指定用户于家目录中:
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list  # 创建此文件并将要禁锢的用户添加进去
 
注意:chroot_list_file与chroo_local_user指令不能同时使用
 
11、是否启用用户列表
userlist_enable=YES    #启用时,vsftpd将加载一个由userlist_file指令指定的用户列表文件;此文件中的用户是否能访问vsftpd服务取决于userlist_deny指令;
userlist_deny=YES     # 表示此列表为黑名单
userlist_deny=NO     # 表示此列表为白名单
 
 
1.4 vsftpd基于pam_mysql的虚拟用户
 
1.4.1 基于db文件
 
/etc/vsftpd/vusers.txt文件
奇数行:用户名
偶数行:密码
 
1.4.2 基于mysql服务
 
1、下载pam包
 
2、准备环境
yum -y groupinstall "Development Tools" "Server Platform Development"
yum -y install vsftpd pam-devel mariadb-server mariadb-devel openssl-devel
systemctl start mariadb.service   #启动mariadb服务
systemctl enable mariadb.service   #设置开机自启动mariadb
 
3、编译安装pam_mysql
[[email protected] tools]# tar xf pam_mysql-0.7RC1.tar.gz
[[email protected] tools]# cd pam_mysql-0.7RC1/
[[email protected] pam_mysql-0.7RC1]# ./configure --with-mysql=/usr --with-openssl=/usr --with-pam=/usr --with-pam-mods-dir=/lib64/security
[[email protected] pam_mysql-0.7RC1]# make && make install
[[email protected] pam_mysql-0.7RC1]# ls /lib64/security/pam_mysql.so  #查询是否编译成功,是否有pam_mysql.so模块
/lib64/security/pam_mysql.so
 
4、停止vsftpd,使用默认vsftpd配置文件
[root@CentOS7-171 ~]# systemctl stop vsftpd.service
[root@CentOS7-171 ~]# cd /etc/vsftpd/
[root@CentOS7-171 vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd.conf.bak  vsftpd_conf_migrate.sh
[root@CentOS7-171 vsftpd]# mv vsftpd.conf{,.test}
[root@CentOS7-171 vsftpd]# cp vsftpd.conf.bak vsftpd.conf
 
5、配置mysql
[[email protected] vsftpd]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
 
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> CREATE DATABASE vsftpd;
Query OK, 1 row affected (0.00 sec)
 
MariaDB [(none)]> use vsftpd;
Database changed
 
MariaDB [vsftpd]> CREATE  TABLE users (
    -> id int AUTO_INCREMENT NOT NULL PRIMARY KEY,
    -> name char(30) NOT NULL,
    -> password char(48) binary NOT NULL );
Query OK, 0 rows affected (0.04 sec)
 
MariaDB [vsftpd]> DESC users;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| name     | char(30) | NO   |     | NULL    |                |
| password | char(48) | NO   |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
MariaDB [vsftpd]> INSERT INTO users(name,password) VALUES ('tom',password('zhucke'));
Query OK, 1 row affected (0.00 sec)
 
MariaDB [vsftpd]> INSERT INTO users(name,password) VALUES ('jerry',password(' zhucke.com'));
Query OK, 1 row affected (0.00 sec)
 
MariaDB [vsftpd]> SELECT * FROM users;
+----+-------+-------------------------------------------+
| id | name  | password                                  |
+----+-------+-------------------------------------------+
|  1 | tom   | *9BDB807A93B6C421BBFCAC5EF1AE0835396EEE38 |
|  2 | jerry | *3E27BE6A3667961ABCCFCA4832F06B151F81185A |
+----+-------+-------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [vsftpd]> GRANT select ON vsftpd.* TO vsftpd@localhost IDENTIFIED BY 'zhucke';
Query OK, 0 rows affected (0.07 sec)
 
MariaDB [vsftpd]> GRANT select ON vsftpd.* TO vsftpd@'127.0.0.1' IDENTIFIED BY 'zhucke';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [vsftpd]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
 
[[email protected] vsftpd]# mysql -uvsftpd -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.52-MariaDB MariaDB Server
 
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| vsftpd             |
+--------------------+
2 rows in set (0.00 sec)
 
MariaDB [(none)]> use vsftpd;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
MariaDB [vsftpd]> SELECT * FROM users;
+----+-------+-------------------------------------------+
| id | name  | password                                  |
+----+-------+-------------------------------------------+
|  1 | tom   | *9BDB807A93B6C421BBFCAC5EF1AE0835396EEE38 |
|  2 | jerry | *3E27BE6A3667961ABCCFCA4832F06B151F81185A |
+----+-------+-------------------------------------------+
2 rows in set (0.00 sec)
 
6、配置pam
[[email protected] ftp]# cd /etc/pam.d/
[[email protected] pam.d]# vim vsftpd.mysql
auth required pam_mysql.so user=vsftpd passwd=zhucke host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=zhucke host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
[[email protected] pam.d]# useradd -s /sbin/nologin -d /ftproot vuser
[[email protected] pam.d]# ls -ld /ftproot/
drwx------ 3 vuser vuser 74 Dec 13 22:50 /ftproot/
[[email protected] pam.d]# chmod go+rx /ftproot/
[[email protected] pam.d]# ls -ld /ftproot/
drwxr-xr-x 3 vuser vuser 74 Dec 13 22:50 /ftproot/
[[email protected] pam.d]# vim /etc/vsftpd/vsftpd.conf
pam_service_name=vsftpd.mysql
local_enable=YES
write_enable=YES
local_umask=022
guest_enable=YES
guest_username=vuser   #指明虚拟用户映射到的系统用户
[[email protected] pam.d]# chmod -w /ftproot/
[[email protected] pam.d]# systemctl restart vsftpd
[[email protected] pam.d]# mkdir /ftproot/{pub,upload}
 
7、连接测试
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): tom
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 (192,168,5,171,225,80).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Dec 13 14:54 pub
drwxr-xr-x    2 0        0               6 Dec 13 14:54 upload
226 Directory send OK.
 
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): jerry
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 (192,168,5,171,106,153).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Dec 13 14:54 pub
drwxr-xr-x    2 0        0               6 Dec 13 14:54 upload
226 Directory send OK.
 
8、配置文件可以上传
[[email protected] pam.d]# chown vuser /ftproot/upload/
[[email protected] pam.d]# ls -ld /ftproot/upload/
drwxr-xr-x 2 vuser root 6 Dec 13 22:54 /ftproot/upload/
[[email protected] pam.d]# vim /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES
[[email protected] pam.d]# systemctl restart vsftpd
 
9、测试文件上传
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): tom
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> put fstab
local: fstab remote: fstab
227 Entering Passive Mode (192,168,5,171,210,216).
150 Ok to send data.
226 Transfer complete.
643 bytes sent in 0.000348 secs (1847.70 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,5,171,94,171).
150 Here comes the directory listing.
-rw-------    1 1003     1003          643 Dec 13 15:02 fstab
226 Directory send OK.
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): jerry
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> put issue
local: issue remote: issue
227 Entering Passive Mode (192,168,5,171,57,7).
150 Ok to send data.
226 Transfer complete.
23 bytes sent in 0.000167 secs (137.72 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,5,171,22,21).
150 Here comes the directory listing.
-rw-------    1 1003     1003          643 Dec 13 15:02 fstab
-rw-------    1 1003     1003           23 Dec 13 15:03 issue
226 Directory send OK.
 
10、配置用户拥有不同的权限,一个可以上传,一个不可以上传
[[email protected] vusers.conf.d]# cd /etc/vsftpd/
[[email protected] vsftpd]# mkdir vusers.conf.d
[[email protected] vusers.conf.d]# vim tom
anon_upload_enable=YES
[[email protected] vusers.conf.d]# cp tom jerry
[[email protected] vusers.conf.d]# vim jerry
anon_upload_enable=NO
[[email protected] vusers.conf.d]# vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers.conf.d/
[[email protected] vusers.conf.d]# systemctl restart vsftpd
 
11、验证tom用户和jerry用户文件上传
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): tom
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 (192,168,5,171,74,34).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Dec 13 14:54 pub
drwxr-xr-x    2 1003     0              30 Dec 13 15:03 upload
226 Directory send OK.
ftp> lcd /etc
Local directory now /etc
ftp> cd upload
250 Directory successfully changed.
ftp> pwd
257 "/upload"
ftp> put grub2.cfg
local: grub2.cfg remote: grub2.cfg
227 Entering Passive Mode (192,168,5,171,126,201).
150 Ok to send data.  #tom用户上传成功
226 Transfer complete.
4213 bytes sent in 0.133 secs (31.61 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,5,171,178,255).
150 Here comes the directory listing.
-rw-------    1 1003     1003          643 Dec 13 15:02 fstab
-rw-------    1 1003     1003         4213 Dec 13 15:39 grub2.cfg
-rw-------    1 1003
#测试jerry用户
[[email protected] ~]# ftp 192.168.5.171
Connected to 192.168.5.171 (192.168.5.171).
220 (vsFTPd 3.0.2)
Name (192.168.5.171:root): jerry
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> ls
227 Entering Passive Mode (192,168,5,171,58,232).
150 Here comes the directory listing.
-rw-------    1 1003     1003          643 Dec 13 15:02 fstab
-rw-------    1 1003     1003         4213 Dec 13 15:39 grub2.cfg
-rw-------    1 1003     1003           23 Dec 13 15:03 issue
226 Directory send OK.
ftp> put issue
local: issue remote: issue
227 Entering Passive Mode (192,168,5,171,233,13).
550 Permission denied.   #jerry测试结果是不能上传
ftp> ls
227 Entering Passive Mode (192,168,5,171,240,104).
150 Here comes the directory listing.
-rw-------    1 1003     1003          643 Dec 13 15:02 fstab
-rw-------    1 1003     1003         4213 Dec 13 15:39 grub2.cfg
-rw-------    1 1003     1003           23 Dec 13 15:03 issue
226 Directory send OK.
    

猜你喜欢

转载自www.cnblogs.com/zhuck/p/9173652.html
FTP