RHEL 6.x vsftpd搭建 ftp ftps
RHEL 6.x vsftpd搭建 ftp ftps
==============================
ftp服务器的工作原理
vsftpd实现ftp的匿名用户
vsftpd实现ftp的本地用户
vsftpd实现限制客户端的速率和连接数
vsftpd+ssl实现安全的ftps
==============================
一、ftp服务器的工作原理
FTP是一种能实现下载文件的协议,FTP的协议连接有两种方式,一种是控制连接,一种是数据连接,FTP的数据连接方式中也有两种模式,一种是主动模式,一种是被动模式。
主动模式的工作原理
被动模式的工作原理
安装FTP的软件包:
yum -y install vsftpd
rpm -ql vsftpd (ql接包名,查看软件包安装生成了那些文件)
rpm -ql vsftpd (qc查看软件包生成的配置文件)
二、vsftpd实现ftp的匿名用户
[[email protected] ~]# rpm -ql vsftpd | grep -v /usr* /etc/logrotate.d/vsftpd #配置日志回滚的文件 /etc/pam.d/vsftpd #配置vsftpd的PAM认证文件 /etc/rc.d/init.d/vsftpd #服务启动的脚本文件 /etc/vsftpd /etc/vsftpd/ftpusers /etc/vsftpd/user_list /etc/vsftpd/vsftpd.conf #服务的配置文件 /etc/vsftpd/vsftpd_conf_migrate.sh /var/ftp #FTP默认的宿主目录 /var/ftp/pub
1)匿名用户能上传和下载文件,且宿主目录为默认目录(/var/ftp/pub)
[[email protected] ~]# grep -v "^#" /etc/vsftpd/vsftpd.conf | grep -v "^$" anonymous_enable=YES #允许匿名用户访问(yes,为允许,no为不允许) local_enable=NO #是否禁用本地用户(设置为no是表示禁用,yes表示不禁用) write_enable=YES #允许开放写的权限 anon_umask=022 #设置匿名用户上传建立文件时的权限掩码 anon_upload_enable=YES #允许匿名上传文件 anon_mkdir_write_enable=YES #允许匿名用户创建目录 dirmessage_enable=YES #用户切换进入目录时显示.message(如果存在)文件的内容 xferlog_enable=YES #开启xferlog日志,默认记录到/var/log/xferlog connect_from_port_20=YES #连接控制端口为20 xferlog_file=/var/log/xferlog #记录日志文件的路径 xferlog_std_format=YES #启用标准xferlog的日志格式,若禁用此项,将使用vsftpd自己的日志格式 listen=YES #是否以独立运行的方式监听服务 pam_service_name=vsftpd #设root置用于用户认证的PAM文件位置(/etc/pam.d/目录中对应的文件名) userlist_enable=NO #未启用本地用户时,可以将用户列表功能禁用 tcp_wrappers=YES #是否开启tcp_wrappers主机访问控制 [[email protected] ~]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ]
验证:
2)匿名用户可以上传、下载、删除目录且ftp访问的默认宿主目录为自己指定目录
vim /etc/vsftpd/vsftpd.conf ####在之前的配置基础上,在加入两句####### anon_other_write_enable=YES #是否允许匿名用户有其它写入权限,如对文件改名,覆盖以及删除(此选项慎用) anon_root=/gx #指定匿名用户登陆后的宿主目录
三、vsftpd实现ftp的本地用户
本地用户必须存在于系统中
1)本地用户可以上传下载,删除文件
[[email protected] vsftpd]# grep -v "^#" /etc/vsftpd/vsftpd.conf | grep -v "^$" anonymous_enable=NO local_enable=YES #开启本地用户 write_enable=YES #本地用户允许写入 local_umask=022 #设置本地用户的umask值为022 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_file=/var/log/xferlog xferlog_std_format=YES listen=YES pam_service_name=vsftpd userlist_enable=YES #启用user_list列表文件 tcp_wrappers=YES [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [[email protected] vsftpd]# useradd jie #创建系统用户 [[email protected] vsftpd]#echo redhat | passwd --stdin jie &>/dev/null #给系统用户设置密码为redhat
2)禁锢本地用户在自己的家目录,开启user_list文件的相关功能
本地用户是否能访问ftp,需注意/etc/vsftpd目录下的ftpusers文件和user_list文件这两个文件
ftpusers文件:该文件中包含的用户帐户将被禁止登入vsftpd服务器,不管用户是不是在user_list文件出现。
user_list文件:该文件中包含的用户帐户可能被禁止登入vsftpd服务器,也可能被允许登入,具体要看配置文件中的userlist_enable参数的配置(配置成YES,则启用user_list用户列表文件,NO则不启用user_list用户列表文件),当userlist_enable=YES,之后看配置文件中userlist_deny的配置(为YES则仅禁止用户列表文件的用户账号登入,为NO则仅用户列表文件的用户账号登入)
不允许user_list文件里面的用户登录ftp服务器
[[email protected] vsftpd]# useradd user1 [[email protected] vsftpd]# echo user1 | passwd --stdin user1 &>/dev/null [[email protected] vsftpd]# useradd user2 [[email protected] vsftpd]# echo user2 | passwd --stdin user2 &>/dev/null [[email protected] vsftpd]# useradd user3 [[email protected] vsftpd]# echo user3 | passwd --stdin user3 &>/dev/null ######配置文件里面添加这几条######################################### userlist_enable=YES #启用user_list文件 userlist_deny=YES #禁止用户列表文件的用户账号登入 chroot_local_user=YES #将本地用户禁锢在自己的家目录 ##################################################################### [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [[email protected] vsftpd]# echo "user1" >/etc/vsftpd/user_list #把user1用户添加到user_list文件中 [[email protected] vsftpd]# lftp -u user1 172.16.22.1 Password: lftp [email protected]:~> ls `ls' at 0 [Delaying before reconnect: 23] #验证显示user1无法登录ftp服务器 [[email protected] vsftpd]# lftp -u user2 172.16.22.1 Password: lftp [email protected]:~> ls lftp [email protected]:~> mkdir user2 #user2可以登录ftp服务器,还可以创建文件 mkdir ok, `user2' created lftp [email protected]:/> cd / #切换到ftp服务器的根目录 lftp [email protected]:/> ls #显示不能切换 drwxr-xr-x 2 502 502 4096 Aug 14 13:53 user2 lftp [email protected]:~> bye [[email protected] vsftpd]# lftp -u user3 172.16.22.1 Password: lftp [email protected]:~> mkdir user3 #user3也可以登录ftp服务器,还可以创建文件 mkdir ok, `user3' created lftp [email protected]:~> ls drwxr-xr-x 2 503 503 4096 Aug 14 13:54 user3 lftp [email protected]:/> cd / #尝试切换到根目录 lftp [email protected]:/> ls #显示切换不过去 drwxr-xr-x 2 503 503 4096 Aug 14 13:54 user3 lftp [email protected]:~> bye [[email protected] vsftpd]#
只允许user_list文件里面的用户能登录ftp服务器
sed -i '/userlist_deny/[email protected]@[email protected]' /etc/vsftpd/vsftpd.conf #修改配置文件匹配userlist_deny行,把YES改为NO [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [[email protected] vsftpd]# lftp -u user3 172.16.22.1 Password: lftp [email protected]:~> ls `ls' at 0 [Delaying before reconnect: 23] #验证显示user3无法登录ftp服务器 [[email protected] vsftpd]# lftp -u user1 172.16.22.1 Password: lftp [email protected]:~> ls lftp [email protected]:/> mkdir user1 #验证只有user1可以登录ftp服务器 mkdir ok, `user1' created lftp [email protected]:/> ls drwxr-xr-x 2 501 501 4096 Aug 14 14:09 user1
四、vsftpd实现限制客户端的速率和连接数
#####修改配置文件##################### anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_file=/var/log/xferlog xferlog_std_format=YES listen=YES pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES chroot_local_user=YES max_clients=20 #允许客户端最大连接数量 max_per_ip=2 #允许同一个IP的客户端同时最多连接2次 local_max_rate=10240 #限定最大的下载速度为10240B/s即10KB/s ############################################################# [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ]
验证:
五、vsftpd+ssl实现安全的ftps
1)搭建基于本地用户的ftp
grep -v "^#" /etc/vsftpd/vsftpd.conf | grep -v "^$" #####修改配置文件############################# anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES connect_from_port_20=YES xferlog_file=/var/log/xferlog xferlog_std_format=YES listen=YES pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES chroot_local_user=YES max_clients=20 max_per_ip=2 local_max_rate=10240 ####################################################### [[email protected] vsftpd]# useradd jie [[email protected] vsftpd]#echo redhat | passwd --stdin jie &>/dev/null [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ] [[email protected] vsftpd]#
2)自建CA服务器
a)生成自己的私钥文件
(umask 077;openssl genrsa -out /etc/vsftpd/ftpkey.pri 2048)
umask 077:用括号括起来,则表示此umsak只对当前子shell有效,如果不用括号,则对当前shell都生效,在此创建文件时属组和其它用户将没有任何权限
openssl genrsa:生成私钥的命令关键字
-out:指定文件的路径
ftpkey.pri:私钥的名称,名字可以随便取,用.pri结尾是为了方便记忆
2048:指私钥生成的位,默认是512,必须是2的n次方倍数
b)利用私钥文件生成证书签署请求文件
openssl req -new -key /etc/vsftpd/ftpkey.pri -out /etc/vsftpd/ftpreq.csr
req:证书请求和证书生成工具的命令关键字
-new:制作证书申请
-key:指定私钥文件
-out:输出证书请求文件的路径,以.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) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:Wuhan
Organization Name (eg, company) [Default Company Ltd]:Jie
Organizational Unit Name (eg, section) []:Tech
Common Name (eg, your name or your server's hostname) []:172.16.22.1 #这里写IP地址,访问时就必须输入IP地址
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
c)生成CA的私钥文件
(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
生成CA的私钥文件的存放路径一般存放于/etc/pki/CA/private路径下
d)生成自签证书
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
-x509: 此选项输出一个自签名证书,而不是证书请求。这通常是用于生成一个测试 证书或自签名的根CA
-key:指定CA私钥文件
-out:输出自签证书文件
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) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:Wuhan
Organization Name (eg, company) [Default Company Ltd]:Jie
Organizational Unit Name (eg, section) []:Tech
Common Name (eg, your name or your server's hostname) []:172.16.22.1 #这里写IP地址,访问时就必须输入IP地址
Email Address []:
[[email protected] vsftpd]# touch /etc/pki/CA/index.txt
[[email protected] vsftpd]# echo "01" >/etc/pki/CA/serial
e)颁发ftp的证书
openssl ca -in /etc/vsftpd/ftpreq.csr -out /etc/vsftpd/certftp.crt -days 3650
openssl ca :颁发CA证书的命令关键字
-in:指定证书签署请求文件
-out:输出颁发证书的文件
-days:限定证书的有效期,3650天
CA机构实际环境中:由公司提交签署证书的请求文件,ftpreq.csr,CA机构根据此请求文件,颁发签署证书文件certftp.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: Aug 13 02:51:01 2013 GMT
Not After : Aug 11 02:51:01 2023 GMT
Subject:
countryName = CN
stateOrProvinceName = Hubei
organizationName = Jie
organizationalUnitName = Tech
commonName = 172.16.22.1
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
75:8F:E4:5E:B7:61:EB:70:42:60:BE:CA:E5:F0:AE:61:5B:EB:05:EF
X509v3 Authority Key Identifier:
keyid:2A:E6:B1:0D:CA:56:10:33:4B:85:48:28:75:92:41:76:E3:D4:1C:1C
Certificate is to be certified until Aug 11 02:51:01 2023 GMT (3650 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
3)修改vsftpd的配置文件让其支持ssl
#####修改vsftpd的配置文件增加这些######## ssl_enable=YES ssl_tlsv1=YES ssl_sslv2=YES ssl_sslv3=YES allow_anon_ssl=NO #不允许匿名用户SSL连接FTP force_local_data_ssl=YES force_local_logins_ssl=YES rsa_cert_file=/etc/vsftpd/certftp.crt #证书的存放路径 rsa_private_key_file=/etc/vsftpd/ftpkey.pri #ftp私钥文件的存放路径 ###################################################### [[email protected] vsftpd]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ]
4)客服端软件用FlashFXP软件测试
小结:ftp的传输方式是明文的,所以,如果直接用本地用户连接ftp服务,则很容易别恶意***人员,利用嗅探工具抓包,获取该用户的密码;这样对服务器是一个很大的安全隐患,企业搭建ftps可以保障ftp的安全隐患,还可以利用ftp的虚拟用户解决ftp服务器的安全隐患,ftp的虚拟用户的搭建期待下一个博客。
转载于:https://blog.51cto.com/litaotao/1275574