文件共享服務之vsftpd的配置與應用

FTP  File Transfer Protocol 傳輸控制協議,可以使文件通過網絡從一臺主機傳送到同一網絡的另一臺主機上,而不受計算機類型和操作系統類型的限制。

常用ftp軟件:Windows: Serv-U ,IIS Filezilla

Linux:Filezilla ,Vsftpd(very secure),wu-ftpd proftpd  pureftp

ftp工作原理:

主動模式:連接由服務器端發起

被動模式:客戶端需要下載數據的時候,服務器端不是主動連接客戶端,而是告訴客戶端自己已經打開了某個端口,等待客戶端進行數據下載。即服務端等待,客戶端發起請求。

wps_clip_p_w_picpath-15512

在服務器端,FTP服務器有兩個預分配的端口號,分別爲21和20,其中,端口號21用於發送和接收FTP的控制信息,FTP服務器通過偵聽這個端口來判斷是否有FTP客戶端的連接請求,一個FTP會話建立後,端口21在會話期間將始終保持打開狀態,端口20用於發送和接收FTP數據(ASCII碼或者二進制文件),該端口只在傳輸數據時打開,在傳輸結束時關閉。

在客戶端,當FTP客戶端發起請求後,課動態分配端口號,選擇範圍爲1024~65535.當一個FTP會話開始後,客戶端程序打開一個控制端口,該端口連接到服務器上的21端口,需要傳輸數據時,客戶端再打開到服務器端口20 的第二個端口。每當開始傳輸文件時,客戶端程序就會打開一個新的數據端口,在文件傳輸完畢後,再將該端口自動關閉。

wps_clip_p_w_picpath-1842

如果客戶端和服務器端存在防火牆,主動模式就會產生問題:防火牆無法預知客戶端用於建立數據連接的端口。需採用被動模式,被動模式的控制連接和數據連接都是由FTP客戶端發起的。首先客戶端隨機開啓一個大於1024的端口X向服務器的21端口發起連接,同時開啓X+1端口,然後向服務器發送PASV命令,通知服務器自己處於被動模式,服務器收到命令後,會開放一個大於1024的端口Y進行監聽,然後用PORT Y告訴客戶端,自己的數據端口是Y,客戶端收到命令後,會通過X+1號端口連接服務器的端口Y,然後在兩個端口之間進行數據傳輸。

軟件包:vsftpd

進程:vsftpd (/usr/sbin/vsftpd)

啓動腳本:/etc/init.d/vsftpd

三個主配置文件

/etc/vsftpd/vsftpd.conf  600 root root

/etc/vsftpd/ftpusers     600 root root    //**默認不允許登錄本機ftp的用戶

/etc/pam.d/vsftpd       644 root root   //**訪問控制文件,驗證用戶賬號及密碼

PS:vsftpd接受tcpwrapper的控制。但需要加載兩個額外模塊:ip_conntrack_ftp,

ip_nat_ftp。

下面就來安裝使用vsftpd:

[root@station39 ~]# yum install vsftpd -y

[root@station39 ~]# service vsftpd start

查看下主配置文件:

[root@station39 ftp]# cd /etc/vsftpd/

[root@station39 vsftpd]# vim vsftpd.conf

# Allow anonymous FTP? (Beware - allowed by default if you comment this out).

anonymous_enable=YES      //**是否允許匿名用戶登錄。

PS:所謂匿名用戶實際上被映射爲系統上的ftp用戶

[root@station39 vsftpd]# finger ftp

Login: ftp                              Name: FTP User

Directory: /var/ftp                     Shell: /sbin/nologin

Never logged in.

No mail.

No Plan.

所以我們實際訪問的是ftp的家目錄。

# Uncomment this to allow local users to log in.

local_enable=YES     //**是否允許系統上的賬號登錄

PS:任何一個用戶登錄FTP時訪問的是自己的家目錄。

# Uncomment this to enable any form of FTP write command.

write_enable=YES           //**是否允許本地用戶上傳

# Default umask for local users is 077. You may wish to change this to 022,

# if your users expect that (022 is used by most other ftpd's)

local_umask=022     //** 當本地用戶上傳文件時建立新文件時所使用的權限掩碼

# Uncomment this to allow the anonymous FTP user to upload files. This only

# has an effect if the above global write enable is activated. Also, you will

# obviously need to create a directory writable by the FTP user.

#anon_upload_enable=YES     //** 允許匿名用戶上傳文件

# Uncomment this if you want the anonymous FTP user to be able to create

# new directories.

#anon_mkdir_write_enable=YES   //** 允許匿名用戶上傳目錄

anon_other_write_enable=YES   //** 匿名用戶的其他權限

# Activate directory messages - messages given to remote users when they

# go into a certain directory.

dirmessage_enable=YES        //** 是否打開目錄中的歡迎信息

message_file =. message     //**在任何一個ftp的目錄下建立. message的文件寫上歡迎信

                              息,切換到那個目錄時就會顯示歡迎信息

# The target log file can be vsftpd_log_file or xferlog_file.

# This depends on setting xferlog_std_format parameter

xferlog_enable=YES           //** 是否啓用傳輸日誌,一般和下面兩項結合使用

# The name of log file when xferlog_enable=YES and xferlog_std_format=YES

# WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log

#xferlog_file=/var/log/xferlog

# Switches between logging into vsftpd_log_file and xferlog_file files.

# NO writes to vsftpd_log_file, YES to xferlog_file

xferlog_std_format=YES        //** 傳輸日誌是否使用標準格式

log_ftp_protocol=YES        //**詳細日誌,包括用戶使用什麼命令,此選項使用非標準格

                              式,意味着上面一項必須爲NO

chroot_local_user=YES     //** 禁錮所有用戶只能訪問自己的家目錄

# You may specify an explicit list of local users to chroot() to their home

# directory. If chroot_local_user is YES, then this list becomes a list of

# users to NOT chroot().

#chroot_list_enable=YES

# (default follows)

#chroot_list_file=/etc/vsftpd/chroot_list    //**禁錮某些用戶只能訪問自己的家目錄,將用戶 

                                       名寫在/etc/vsftpd/chroot_list即可。

# If you want, you can arrange for uploaded anonymous files to be owned by

# a different user. Note! Using "root" for uploaded files is not

# recommended!

#chown_uploads=YES

#chown_username=whoever    //** 匿名用戶上傳文件時,將文件的權限改爲另一個用戶的

# You may fully customise the login banner string:

#ftpd_banner=Welcome to blah FTP service.          //** 歡迎信息

# You may activate the "-R" option to the builtin ls. This is disabled by

# default to avoid remote users being able to cause excessive I/O on large

# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume

# the presence of the "-R" option, so there is a strong case for enabling it.

#ls_recurse_enable=YES        //**是否允許遞歸使用ls命令

# You may change the default value for timing out an idle session.

#idle_session_timeout=600    //** 空閒會話超時時間

#

# You may change the default value for timing out a data connection.

#data_connection_timeout=120  //** 數據連接超時時間

max_clients              //** 允許併發連接的所有客戶端的數目

max_per_ip              //**  單個IP所允許打開的併發連接數

anon_max_rate=10240      //** 匿名用戶上傳下載的最大速率

local_max_rate=10240       //** 本地用戶上傳下載的最大速率

userlist_enable=YES        //** 是否接受/etc/vsftpd/user_list的管理,與下面一項結合使用

userlist_deny=YES|NO      //** YES 黑名單  NO 白名單(僅允許文件中的用戶登錄)

PS:vsftp支持虛擬用戶。

在開啓SElinux的情況下允許匿名用戶上傳

[root@station39 vsftpd]# setsebool -P allow_ftpd_anon_write 1

[root@station39 vsftpd]# setsebool  -P ftp_home_dir 1

[root@station39 vsftpd]# getsebool -a | grep ftp

allow_ftpd_anon_write --> on

allow_ftpd_full_access --> off

allow_ftpd_use_cifs --> off

allow_ftpd_use_nfs --> off

allow_tftp_anon_write --> off

ftp_home_dir --> on

ftpd_connect_db --> off

ftpd_disable_trans --> off

ftpd_is_daemon --> on

httpd_enable_ftp_server --> off

tftpd_disable_trans --> off

使用SSL

做一個自簽名的證書:

[root@station39 vsftpd]# mkdir ssl

[root@station39 vsftpd]# cd ssl/

[root@station39 ssl]# openssl genrsa 1024 > vsftpd_key.pem

[root@station39 ssl]# openssl req -new -x509 -key vsftpd_key.pem -out vsftpd_cert.pem -days 3655

wps_clip_p_w_picpath-4878

[root@station39 ssl]# chmod 600 ./*

[root@station39 ssl]# ll

total 16

-rw------- 1 root root 1184 Mar 24 13:20 vsftpd_cert.pem

-rw------- 1 root root  887 Mar 24 13:15 vsftpd_key.pem

編輯/etc/vsftpd/vsftpd.conf文件

#ssl

ssl_enable=YES

ssl_tlsv1=YES

ssl_sslv2=YES

ssl_sslv3=YES

allow_anon_ssl=NO

force_local_data_ssl=YES

force_local_logins_ssl=YES

rsa_cert_file=/etc/vsftpd/ssl/vsftpd_cert.pem

rsa_private_key_file=/etc/vsftpd/ssl/vsftpd_key.pem

保存退出,重啓服務!

PS:vsftpd 的配置文件對語法要求比較嚴格,一定要注意選項後面不要有空格,否則就會有如下錯誤:

wps_clip_p_w_picpath-21616

配置選項參數解釋:

ssl_enable=yes/no //**是否啓用 SSL,默認爲no
allow_anon_ssl=yes/no //**是否允許匿名用戶使用SSL,默認爲no
rsa_cert_file=/path/to/file //**rsa證書的位置
dsa_cert_file=/path/to/file //**dsa證書的位置
force_local_logins_ssl=yes/no //**非匿名用戶登陸時是否加密,默認爲yes
force_local_data_ssl=yes/no //**非匿名用戶傳輸數據時是否加密,默認爲yes
force_anon_logins_ssl=yes/no //**匿名用戶登錄時是否加密,默認爲no
force_anon_data_ssl=yes/no //**匿名用戶數據傳輸時是否加密,默認爲no
ssl_sslv2=yes/no //**是否激活sslv2加密,默認no
ssl_sslv3=yes/no //**是否激活sslv3加密,默認no
ssl_tlsv1=yes/no //**是否激活tls v1加密,默認yes
ssl_ciphers=加密方法 //**默認是DES-CBC3-SHA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章