原文地址:http://blog.csdn.net/u011364306/article/details/48490143
1.檢查vsftpd是否安裝
- root@centos ~]# rpm -qa|grep vsftpd
- vsftpd-2.2.2-14.el6.x86_64
1.1如未安裝,進行yum安裝vsftpd
- [root@centos ~]# yum install vsftpd -y
vsftpd軟件安裝完後,默認端口是21,所以需要在防火牆配置中開放21端口
- [root@centos ~]# vim /etc/sysconfig/iptables
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT ACCEPT [0:0]
- :FORWARD ACCEPT [0:0]
- :OUTPUT ACCEPT [0:0]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- COMMIT
- [root@centos ~]# service iptables restart
3.1關閉selinux,將enforcing改爲disabled
- [root@centos ~]# cat /etc/selinux/config |grep ^[^#]
- ELINUX=disabled
- SELINUXTYPE=targeted
- [root@centos ~]# setsebool ftpd_disable_trans 1
- [root@centos ~]# setsebool -P ftp_home_dir=1
- [root@centos ~]# service vsftpd start
- Starting vsftpd for vsftpd: [ OK ]
- [root@centos ~]# chkconfig vsftpd on
- [root@centos ~]# netstat -lnput |grep vsftpd
- tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1797/vsftpd
vsftpd服務器的配置文件的是: /etc/vsftpd/vsftpd.conf
vsftpd服務器的根目錄,即FTP服務器的主目錄:/var/ftp/pub
6.添加FTP本地用戶(虛擬用戶)
FTP大家可能都用過,通過給定的HOST、賬號、密碼就可以訪問服務器對應的目錄空間了。但是,這個FTP賬號只能訪問FTP服務,不能登錄服務器系統,只能訪問自己的目錄。這樣的用戶就叫虛擬用戶,本質上這不叫虛擬用戶,僅僅只是不能通過終端等一系列途徑登錄服務器系統而已。
創建一個FTP用戶的命令如下:
- useradd -d /ftp -g ftp -s /sbin/nologin ftpserver
此命令的含義:
使用shell命令useradd添加一個ftpserver的系統賬戶,但是不能登錄系統(-s /sbin/nologin),該賬戶的主目錄在(-d /ftp),屬於ftp這個用戶組(-g ftp)
我們創建了一個FTP賬戶,現在來設置賬戶的密碼,命令如下
- passwd ftpserver
7.vsftpd 配置文件的調整
anonymous_enable=NO
//設定不允許匿名訪問,默認爲YES
local_enable=YES
//設定本地用戶可以訪問,默認爲YES。注:如使用虛擬宿主用戶,在該項目設定爲NO的情況下所有虛擬用戶將無法訪問。
chroot_local_user=YES
//使所有用戶不能離開主目錄,默認被#註釋,
注:如果要限制特定用戶不可以切換主目錄
將#chroot_list_enable=YES,#chroot_list_file=/etc/vsftpd/chroot_list前#註釋掉,並在/etc/vsftpd/chroot_list下添加制定的用戶即可
xferlog_file=/var/log/vsftpd.log
//設定vsftpd的服務日誌保存路徑,默認被#註釋。注意,該文件默認不存在。必須要手動touch
listen_port=2510
//默認端口爲21,爲了安全性可以修改該端口如上,修改好記得重啓服務和加入防火牆配置
idle_session_timeout=600
//默認爲600秒,空閒時間過久連接會中斷,默認未啓用,需將#號刪除
更詳細的參數解析:http://blog.csdn.net/u011364306/article/details/49078127
以下爲本機使用的參數文件配置
- [root@centos ~]# cat /etc/vsftpd/vsftpd.conf |grep ^[^#]
- 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
- idle_session_timeout=600
- chroot_local_user=YES
- listen=YES
- pam_service_name=vsftpd
- userlist_enable=YES
- tcp_wrappers=YES
- listen_port=2510
8.限制客戶端的可連接IP地址
首先檢查vsftpd.conf中的tcp_wrappers
- [root@centos vsftpd]# grep "tcp_wrappers" /etc/vsftpd/vsftpd.conf
- tcp_wrappers=YES
配置hosts.deny文件,限制所有網段不能連接
- [root@centos vsftpd]# vim /etc/hosts.deny
- #
- # hosts.deny This file contains access rules which are used to
- # deny connections to network services that either use
- # the tcp_wrappers library or that have been
- # started through a tcp_wrappers-enabled xinetd.
- #
- # The rules in this file can also be set up in
- # /etc/hosts.allow with a 'deny' option instead.
- #
- # See 'man 5 hosts_options' and 'man 5 hosts_access'
- # for information on rule syntax.
- # See 'man tcpd' for information on tcp_wrappers
- #
- vsftpd:all:Deny //限制所有網段都不可以連接
- [root@centos vsftpd]# vim /etc/hosts.allow
- #
- # hosts.allow This file contains access rules which are used to
- # allow or deny connections to network services that
- # either use the tcp_wrappers library or that have been
- # started through a tcp_wrappers-enabled xinetd.
- #
- # See 'man 5 hosts_options' and 'man 5 hosts_access'
- # for information on rule syntax.
- # See 'man tcpd' for information on tcp_wrappers
- #
- vsftpd:192.168.1.*:Allow //開放192.168.1網絡可以連接
9.使用chattr命令可以對某個文件夾下的內容只允許上傳和下載,而不許刪除
- [root@centos /]# chattr +a /ftp/Upload
10.對目錄設置500權限就將該文件夾只許下載,不許上傳和刪除
- [root@centos /]# chmod -R 500 /ftp/Upload