学习笔记之ftp服务器
来源:互联网 发布:淘宝上有防身电棍吗 编辑:程序博客网 时间:2024/04/28 19:15
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 vsftpdLoaded plugins: product-id, subscription-managerThis 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=ftpsuccess
reload一下防火墙[root@ns ~]# firewall-cmd --reloadsuccess
可以使用下面的命令看下ftp被防火墙放过了吗[root@ns ~]# firewall-cmd --list-allpublic (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]# lltotal 0drwxr-xr-x. 2 root ftp 6 Mar 7 2014 pubdrwxrwxr-x. 2 root ftp 35 Nov 14 22:36 upload[root@ns ftp]# pwd/var/ftp
给pub下放个文件。随便放一个。看看客户端可以看见吗
[root@ns pub]# lsautocreateCA.sh
客户端
好了。来看看客户端。客户端使用lftp进行访问。lftp默认是匿名用户访问的
没有可以yum安装
[root@www ~]# yum -y install lftp Loaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfileResolving Dependencies--> Running transaction check
[root@www ~]# lftp 192.168.1.234lftp 192.168.1.234:~> lsdrwxr-xr-x 2 0 50 6 Mar 07 2014 pubdrwxrwxr-x 2 0 50 35 Nov 14 14:36 uploadlftp 192.168.1.234:/> cd publftp 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 ftpftp_home_dir --> offftpd_anon_write --> offftpd_connect_all_unreserved --> offftpd_connect_db --> offftpd_full_access --> offftpd_use_cifs --> offftpd_use_fusefs --> offftpd_use_nfs --> offftpd_use_passive_mode --> offhttpd_can_connect_ftp --> offhttpd_enable_ftp_server --> offsftpd_anon_write --> offsftpd_enable_homedirs --> offsftpd_full_access --> offsftpd_write_ssh_home --> offtftp_anon_write --> offtftp_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 2048Generating 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 3650You are about to be asked to enter information that will be incorporatedinto 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 blankFor 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.comEmail 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 2048Generating 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.csrYou are about to be asked to enter information that will be incorporatedinto 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 blankFor 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.comEmail Address []:Please enter the following 'extra' attributesto be sent with your certificate requestA 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.crtUsing configuration from /etc/pki/tls/openssl.cnfCheck that the request matches the signatureSignature okCertificate 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:D7Certificate is to be certified until Nov 15 10:12:57 2017 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated
在/etc/vsftpd/vsftpd.conf 中写入如下内容
# ssl or tlsssl_enable=YESssl_sslv3=YESssl_tlsv1=YESallow_anon_ssl=NOforce_local_data_ssl=YESrsa_cert_file=/etc/vsftpd/ssl/vsftpd.crtrsa_private_key_file=/etc/vsftpd/ssl/vsftpd.key
配置完记得重启
这下匿名用户可以连接。而真实用户就需要证书了,就会出现如下错误
[root@www Desktop]# lftp 192.168.1.234 -u susePassword: 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.234suse@192.168.1.234's password: Connected to 192.168.1.234.sftp> lsinittab 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 & virt153 154 pam_service_name=ckvsftpd155 guest_enable=YES156 guest_username=ftp157 local_root=/home/$USER158 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 root137 anon_max_rate=200000好了,在主配置文件再加这么一行。
160 # vip user161 user_config_dir=/etc/vsftpd/vipuser创建那个目录。在那个文件里创建个文件。文件名必须为vip用户的名字
[root@ns vsftpd]# mkdir vipuser[root@ns vsftpd]# echo "anon_max_rate=20000000" > vipuser/kail
还想加什么特权,就取消主配置文件的权限并将权限给vipuser中的kail文件,比如上传权限呀啥的。
2 0
- 学习笔记之ftp服务器
- linux学习笔记之ftp服务器
- FTP服务器配置---学习笔记
- linux学习之FTP服务器
- linux学习笔记6-FTP服务器
- C#学习笔记之从FTP服务器上传和下载数据(一)
- C#学习笔记之从FTP服务器上传和下载数据(二)
- Ubuntu操作系统学习笔记之FTP基础
- ftp服务器之vsftp
- 文件服务器之FTP服务器
- Linux网络编程学习之---简单局域网FTP文件传输服务器
- 【FTP服务器之】FTP协议与文件传输
- FTP协议学习笔记
- FTP 源码学习笔记
- FTP协议学习笔记
- Ftp学习笔记
- FTP协议学习笔记
- FTP学习笔记
- 【Android Meditation】AS中同一个工程module太多导致编译慢
- 《锋利的JQuery》学习笔记
- php 利用gd库及tcpdf 自动多图片生成pdf
- Swift语言精要-闭包(Closure)
- Neural Network :Backpropagation的实现
- 学习笔记之ftp服务器
- 深入C#的String类
- 深入理解Java:注解(Annotation)自定义注解入门
- 每天一个PHP实例:preg_replace
- vb读取写入导出excel
- ClassLoader 详解及用途
- Spring Boot Servlet
- java的图形界面输入
- mysql 绿色版本安装和配置