1.ftp的基础知识
ftp是一种古老的数据传输工具,也是一个明文传输的协议。按照用户登录方式。可以分为
1.实体用户登录(real user)
2.访客(guest)
3.匿名用户(anonymous)
ftp工作时有两个端口在使用,分别是20,21端口。
命令传输通道ftp(20端口)
数据传输通道ftp-datta(21端口)
ftp的连接过程如下。
1.先是服务器端在21端口监听,等待客户端的连接
2.客户端发起连接,建立命令通道连接后告诉服务器端的客户端的数据端口号A,并等待服务器端的数据端口21和自己的端口A相连
3.由服务器端的21端口发起连接请求。连接客户端的A端口。
注意:如果客户端生活在防火墙,NAT服务器后面的话就很有可能连接失败,需要特别的设置。(如PASV)
2.安装最简单的FTP服务器。
注意,这里主机名是ns的是服务器。www的是客户端
服务器端
1.直接yum安装就好了。
[root@ns ~]# yum -y install vsftpd
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
RHEL | 4.1 kB 00:00:00
我的使用的是centos7,可以直接关闭防火墙。也可以使用如下命令
[root@ns ~]# firewall-cmd --permanent --add-service=ftp
success
reload一下防火墙
[root@ns ~]# firewall-cmd --reload
success
可以使用下面的命令看下ftp被防火墙放过了吗
[root@ns ~]# firewall-cmd --list-all
public (default, active)
interfaces: enp2s0
sources:
services: dhcpv6-client dns ftp nfs ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
其实ftp还被selinux影响着。看看selinux的级别
[root@ns ~]# getenforce
Enforcing
[root@ns ftp]# ll
total 0
drwxr-xr-x. 2 root ftp 6 Mar 7 2014 pub
drwxrwxr-x. 2 root ftp 35 Nov 14 22:36 upload
[root@ns ftp]# pwd
/var/ftp
给pub下放个文件。随便放一个。看看客户端可以看见吗
[root@ns pub]# ls
autocreateCA.sh
客户端
好了。来看看客户端。客户端使用lftp进行访问。lftp默认是匿名用户访问的
没有可以yum安装
[root@www ~]# yum -y install lftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
[root@www ~]# lftp 192.168.1.234
lftp 192.168.1.234:~> ls
drwxr-xr-x 2 0 50 6 Mar 07 2014 pub
drwxrwxr-x 2 0 50 35 Nov 14 14:36 upload
lftp 192.168.1.234:/> cd pub
lftp 192.168.1.234:/pub> ls
-rwxr--r-- 1 0 0 818 Nov 15 07:43 autocreateCA.sh
可以使用get 文件名,下载文件了。
lftp 192.168.1.234:/pub> get autocreateCA.sh
818 bytes transferred
lftp 192.168.1.234:~> 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>
3.vsftpd.conf配置文件的参数
这样就完了吗,什么参数都不用改就算搭建了一个ftp服务器?太天真了。
在/etc/vsftpd/vsftpd.conf下填写一些参数。让用户不局限于匿名用户,可以上传,下载限速,最大连接数等等配置都在这里。
1.与服务器环境相关的设置值(配置完记得从启服务)
dirmessage_enable=YES 当用户进入目录后的出现的内容
message_file=.message 内容在这个文件内
write_enable=YES 客户端可以写
anon_upload_enable=YES 匿名用户可以上传
anon_mkdir_write_enable=YES 匿名用户可以新建目录
xferlog_enable=YES 客户端的行为会被记录到服务器端的日志系统中
pasv_enable=YES 允许被动式连接
2.与实体用户较为相关的设置值
local_enable=YES /etc/passwd 下的课登录的用户将可以以ftp的方式登录
但是可能会被selinux拒绝,需要先setenforce 1
chroot_local_user=YES 给登录的用户根切换(就是不能让登录的用户进根或其他目录里
)
chroot_list_enable=YES 部分用户被根切换开启
chroot_list_file=/etc/vsftpd/chroot_list 这个文件内的用户将被根切换
或者以上参数都不要。直接chroot_local_user=YES,禁锢所有用的家目录
userlist_enable=YES 不受欢迎用户列表使能,这个列表和vsftpd.conf在一个目录中
关于实体用户的selinux
[root@ns vsftpd]# getsebool -a |grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@ns vsftpd]# setsebool -P ftp_home_dir=1
如果发现以实体用户登录后发现
ls: Login failed: 500 OOPS: cannot change directory:/home/suse
这是由于没有指定家目录
在配置文件加上这段话
local_root=/home/suse
ls: Login failed: 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
这是由于安全原因。需要将suse 的家目录的w权限去掉。chmod u-w /home/suse
如果你不想执行上面的操作。那么就在配置文件加上这句
allow_writeable_chroot=YES
3.和匿名用户相关
anon_max_rate=0 限制匿名用户的传输速率,0为不限制
4.安全的ftp。密文传输
想要让ftp支持ssl,就需要先创建CA证书。CA的创建我以前写过,里面还有个脚本。可以直接创建CA。点击打开链接
创建一对秘钥
[root@ns CA]# openssl genrsa -out private/cakey.pem 2048
Generating RSA private key, 2048 bit long modulus
.....................................+++
...........................+++
e is 65537 (0x10001)
改变权限
[root@ns CA]# chmod 600 private/cakey.pem
[root@ns CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [Shannxi]:
Locality Name (eg, city) [Xian]:
Organization Name (eg, company) [Xiyou]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) []:ca.double2.com
Email Address []:admin@double2.com
在vsftpd目录下创建一个ssl目录
[root@ns CA]# mkdir /etc/vsftpd/ssl
[root@ns CA]# cd !*
cd /etc/vsftpd/ssl
在ssl目录中创建秘钥
[root@ns ssl]# openssl genrsa -out vsftpd.key 2048
Generating RSA private key, 2048 bit long modulus
................................................................................+++
..............................+++
e is 65537 (0x10001)
[root@ns ssl]# chmod 600 vsftpd.key
生成CA证书
[root@ns ssl]# openssl req -new -key vsftpd.key -out vsftpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [Shannxi]:
Locality Name (eg, city) [Xian]:
Organization Name (eg, company) [Xiyou]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) []:ftp.double2.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
签署证书
注意需要在/etc/pki/CA目录下执行
[root@ns ssl]# cd -
/etc/pki/CA
[root@ns CA]# openssl ca -in /etc/vsftpd/ssl/vsftpd.csr -out /etc/vsftpd/ssl/vsftpd.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Nov 15 10:12:57 2016 GMT
Not After : Nov 15 10:12:57 2017 GMT
Subject:
countryName = CN
stateOrProvinceName = Shannxi
organizationName = Xiyou
organizationalUnitName = Tech
commonName = ftp.double2.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
C3:F9:B4:B9:EF:A6:DD:13:EA:E5:7A:23:96:3C:C7:E4:7F:1E:CA:C5
X509v3 Authority Key Identifier:
keyid:8B:CD:F2:AE:ED:4F:87:EF:FE:B4:97:AE:92:47:FA:E6:F3:82:CA:D7
Certificate is to be certified until Nov 15 10:12:57 2017 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
在/etc/vsftpd/vsftpd.conf 中写入如下内容
# ssl or tls
ssl_enable=YES
ssl_sslv3=YES
ssl_tlsv1=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
rsa_cert_file=/etc/vsftpd/ssl/vsftpd.crt
rsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key
配置完记得重启
这下匿名用户可以连接。而真实用户就需要证书了,就会出现如下错误
[root@www Desktop]# lftp 192.168.1.234 -u suse
Password:
lftp suse@192.168.1.234:~> ls
ls: Fatal error: Certificate verification: Not trusted
在linux端需要直接在终端中输入:sftp username@remote ip(or remote host name)。出现验证时,只需填入正确的密码即可实现远程链接。登入成功后终端呈现出:sftp>....
[root@www Desktop]# sftp suse@192.168.1.234
suse@192.168.1.234's password:
Connected to 192.168.1.234.
sftp> ls
inittab passwd
sftp>
5.虚拟用户的创建
在/etc/vsftpd/下创建一个文件,内容的奇数行是用户名,偶数行是密码
将用户信息文件转换为数据库并使用hash加密
[root@ns vsftpd]# db_load -T -t hash -f /etc/vsftpd/vuser.passwd /etc/vsftpd/vuser.passwd.db
在/etc/pam.d/ckvsftpd下填写这两句话
9 auth required pam_userdb.so db=/etc/vsftpd/vuser.passwd
10 account required pam_userdb.so db=/etc/vsftpd/vuser.passwd
创建虚拟用户的家目录
[root@ns pam.d]# mkdir -p /home/kail
改变权限
[root@ns vsftpd]# chmod 777 /home/kail
在配置文件/etc/vsftpd/vsftpd.conf 中添加如下的话
152 # pam & virt
153
154 pam_service_name=ckvsftpd
155 guest_enable=YES
156 guest_username=ftp
157 local_root=/home/$USER
158 user_sub_token=$USER
但是记得给kail创建一个加目录哦。还有就是给创建的目录个ftp的acl权限或者777.(推荐acl)
pam_service_name=ckvsftpd这个选项在之前有可能写过,记得只写一次,要不可能出错
6.创建VIP用户
什么是VIP呢。举个简单的栗子就是别人不能下载速度只有200k/s,而QQ会员可以下载速度20M/s ,还记得有个限速的选项吗。
那创建VIP用户之前先将所有人的下载权限都限制成200k/s。主配置文件里改
还想加什么特权,就取消主配置文件的权限并将权限给vipuser中的kail文件,比如上传权限呀啥的。
136 # anonymous_create by root
137 anon_max_rate=200000
好了,在主配置文件再加这么一行。
160 # vip user
161 user_config_dir=/etc/vsftpd/vipuser
创建那个目录。在那个文件里创建个文件。文件名必须为vip用户的名字
[root@ns vsftpd]# mkdir vipuser
[root@ns vsftpd]# echo "anon_max_rate=20000000" > vipuser/kail
还想加什么特权,就取消主配置文件的权限并将权限给vipuser中的kail文件,比如上传权限呀啥的。