Centos6.5部署ftp檔案伺服器

梓沐發表於2016-07-21

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

2.配置防火牆

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.啟動vsftpd前的一些必要配置

3.1 關閉selinux,將enforcing改為disabled

[root@centos ~]# cat /etc/selinux/config |grep ^[^#]

ELINUX=disabled

SELINUXTYPE=targeted

3.2 setsebool設定

[root@centos ~]# setsebool ftpd_disable_trans 1

[root@centos ~]# setsebool -P ftp_home_dir=1

4.啟動vsftpd服務,並設定為開機自啟動

[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

5. 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   //限制所有網段都不可以連線

接下來編輯hosts.allow檔案,允許192.168.1網段可以連線

[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

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29812844/viewspace-2122316/,如需轉載,請註明出處,否則將追究法律責任。

相關文章