FTP(File Transfer Protocol,文件傳輸協議),是一種應用層協議,能夠實現很好的實現跨平臺,可是沒法實現一些其餘的功能,像如文件系統掛載等功能。NFS(Network File System,網路文件系統)是工做在內核模式下的,所以不能很好的實現跨平臺,通常只能是Linux主機或者Unix主機之間,但能夠實現掛載使用等功能。SMB(Service Message Block,服務消息塊協議),可以實現Windows和Linux主機之間的文件共享服務,可實現跨平臺,在Linux上實現了CIFS(Common Internet File System)協議。mysql
是一種C/S架構,基於套接字通訊,用來在兩臺機器之間相互傳輸文件。FTP協議用到2種tcp鏈接:一是命令鏈接,用於客戶端和服務端之間傳遞命令,監聽在tcp/21端口;另外一個是數據傳輸鏈接,用來傳輸數據,監聽的端口是隨機的。linux
主動模式的原理以下圖:sql
主動模式存在的問題是,在客戶端通常都會有防火牆的設置,當服務端與客戶端數據進行數據通訊時,客戶端的防火牆會將服務端的端口擋在外面。此時,通訊就會受阻。所以,被動模式就產生了。數據庫
被動模式的原理圖以下:vim
被動模式也會存在防火牆的問題,客戶端與服務端傳輸數據時,在服務端也會有防火牆,但在服務端的防火牆有鏈接追蹤的功能,解決了防火牆的問題。所以,通常使用被動模式比較多。windows
FTP支持系統用戶,匿名用戶,和虛擬用戶三種用戶認證。centos
匿名用戶:登錄用戶名是anonymous,沒有密碼安全
系統用戶:是FTP服務器端的本地用戶和對應的密碼,默認訪問的是用戶家目錄服務器
虛擬用戶:僅用於訪問服務器中特定的資源,常見的虛擬用戶認證的方式有使用文件認證或使用數據庫進行認證。最終也會將這些虛擬用戶同一映射爲一個系統用戶,訪問的默認目錄就是這個系統用戶的家目錄。session
1**:提示信息
2**:成功執行的狀態碼
3**:須要進一步提供補充類的信息碼,例如在輸入用戶帳號信息後出現此狀態,提示繼續輸入密碼
4**:客戶端類的錯誤
5**:服務端錯誤
服務端:
Linux端:wu-ftpd,pureftp,vsftpd(Centos 6上默認提供的)
windows端:ServU,FileZilla-Server
客戶端工具:
Linux操做系統:ftp,lftp,lftpget,wget,cul,gftp等
windows操做系統:FileZilla
在CentOS上默認提供的是vsftpd(Very Secure FTP),以安全著稱。
用戶認證配置文件:/etc/pam.d/vsftpd
服務腳本:/etc/rc.d/init.d/vsftpd
配置文件目錄:/etc/vsftpd
主配置文件:vsftpd.conf
匿名用戶(映射爲ftp用戶)共享資源位置:/var/ftp
系統用戶經過ftp訪問的資源的位置:用戶本身的家目錄
虛擬用戶經過ftp訪問的資源的位置:給虛擬用戶指定的映射成爲的系統用戶的家目錄
Vsftpd
[root@centos ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd 日誌輪訓備份配置文件
/etc/pam.d/vsftpd 基於pam.d 用戶認證配置文件
/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
/usr/sbin/vsftpd 主程序
匿名用戶(映射爲ftp用戶)共享位置爲:/var/ftp
系統用戶經過ftp訪問時資源位置:用戶本身的家目錄
虛擬用戶ftp訪問時資源位置:給虛擬用戶指定的映射成爲的系統用戶的家目錄
/etc/vsftpd/vsftpd.conf配置文件詳解:
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES 是否容許匿名登陸
#
# Uncomment this to allow local users to log in.
local_enable=YES 系統用戶登陸,訪問到本身的家目錄,很危險須要鎖在本身的家目錄
#
# 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 是否容許匿名用戶創建目錄
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES 就是警告信息
#
# The target log file can be vsftpd_log_file or xferlog_file.
# This depends on setting xferlog_std_format parameter
xferlog_enable=YES 是否開啓日誌功能
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# 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 把用戶上傳文件屬組改變爲指定的文件屬組。例如改變成whoever
#
# 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 日誌文件格式
#
# 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 數據鏈接時超時時長
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service. 歡迎信息,沒多大用
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)0.-**
#banned_email_file=/etc/vsftpd/banned_emails
#
# 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_local_user=YES 所的本地用戶鎖定在本身的家目錄
#chroot_list_enable=YES 鎖定指定用戶的在本身的家目錄
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# 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
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES 是否啓用userlist文件
userlist_deny=YES|NO YES時爲黑名單,NO時爲白名單
tcp_wrappers=YES
匿名用戶配置:
anonymous_enable=YES 是否啓用匿名用戶訪問。默認爲打開。
anon_root=/var/ftp 匿名訪問FTP的路徑,默認爲/var/ftp
anon_upload_enable=YES 是否打開匿名用戶上傳。默認關閉。
anon_mkdir_write_enable=YES 是否打開匿名用戶創建目錄,默認關閉
anon_other_write_enable=YES 是否容許匿名用的其餘操做。好比說刪出。
anon_max_rate=0 匿名用戶最大傳輸速度,0時爲無限制
anon_umask=077 匿名上傳權限掩碼
本地用戶配置:
local_enable=YES 系統用戶登陸,訪問到本身的家目錄,很危險須要鎖在本身的家目錄
local_umask=022 限制本地用戶上傳的文件的權限
write_enable=YES 本地用戶是否有寫權限
chroot_local_user=YES 全部本地用戶鎖定在本身的家目錄
chroot_list_enable=YES 鎖定指定用戶的在本身的家目錄
chroot_list_file=/etc/vsftpd/chroot_list 鎖定指定用戶的在本身的家目錄的具體配置文件。
local_max_rate=0 本地用戶數據傳輸率
全局配置:
listen=YES 是否監聽端口
listen_port=21 監聽入站的FTP請求的端口號
download-enable=YES 若是設置爲NO,將拒絕全部的下載請求
dirmessage_enable=YES 用戶進入目錄是否顯示消息
connetct_from_port_20=YES 使用主動模式鏈接,啓用20端口
pasv_enable=YES 是否啓用被動模式鏈接,默認爲被動
pasv_max_port=24600 被動模式鏈接的最大端口號
pasv_min_port=24500 被動模式的最小端口號
userlist_enable=YES 是否啓用userlist文件
userlist_deny=YES|NO YES時爲黑名單,NO時爲白名單
xferlog_enable=YES 是否開啓xferlog 日誌功能
xferlog_std_format=YES 日誌文件格式
xferlog_file=/var/log/xferlog 日誌存在在那個地方
max_clients=0 最大鏈接併發數,等0時無限制,
max_per_ip: 每一個IP能夠同時發起的併發請求數
write_enable=YES 本地用戶是否有寫權限
tcp_wrappers=YES 是否啓用tcp_wrappers
guest_enable=YES 若是爲YES,則全部的非匿名登陸都映射爲guest_username指定的帳戶
guest_username=ftp 上面選項爲YES時,的帳戶名
user_config_dir=/etc/vsftd/conf 指定目錄,在該目錄下能夠爲用戶設置獨立的配置文件和選項
dual_log_enable=NO 是否開啓雙日誌功能
anonymous_enable=YES 是否開啓匿名登陸 默認爲開啓
chown_uploads=YES 是否容許把用戶上傳的文件屬組改變
chown_username=whoever 把用戶上傳文件屬組改變爲指定的文件屬組。例如改變成whoever
idle_session_timeout=600 會話時長
data_connection_timeout=120 數據鏈接操時時長
pam_service_name=vsftpd pam 認證
主動鏈接:
命令:client: 5000----->server: 21/tcp
數據:server: 20 /tcp-------> client:5000+
被動鏈接:
命令:client: 5000------->server:21 tcp
服務器告訴客戶端口本身的端口。121*256+23.客戶端計算後鏈接這個端口進行數據傳輸。
數據:client: 5000+ ------>server:隨機端口
# yum -y install vsftpd mysql-server mysql-devel pam_mysql
# cd /lib64/security/ 驗證模塊位置
Vsftpd+mysql+pam.d(單臺服務器)
1、事先安裝好開發環境和mysql數據庫;
# yum -y install mysql-server mysql-devel vsftpd pam_mysql
# yum -y groupinstall "Development Tools" "Development Libraries"
2.安裝pam_mysql-0.7RC1
# tar zxvf pam_mysql-0.7RC1.tar.gz
# cd pam_mysql-0.7RC1
# ./configure --with-mysql=/usr --with-openssl
# make
# make install
2、建立虛擬用戶帳號
1.準備數據庫及相關表
首先請確保mysql服務已經正常啓動。然後,按須要創建存儲虛擬用戶的數據庫便可,這裏將其建立爲vsftpd數據庫。
mysql> create database vsftpd;
mysql> grant select on vsftpd.* to vsftpd@localhost identified by '123456';
mysql> grant select on vsftpd.* to vsftpd@127.0.0.1 identified by '123456';
mysql> flush privileges;
mysql> use vsftpd;
mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));
2、添加測試的虛擬用戶
根據須要添加所須要的用戶,須要說明的是,這裏將其密碼採用明文格式存儲,緣由是pam_mysql的password()函數與MySQL的password()函數可能會有所不一樣。
mysql> INSERT INTO users (name,password) VALUES ('tom',‘tompassword’);
mysql> INSERT INTO users (name,password) VALUES ('jack','jackpassword');
3、配置vsftpd
1.創建pam認證所需文件
#vi /etc/pam.d/vsftpd.mysql
添加以下兩行
auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0
account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=localhost db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=0
2.修改vsftpd的配置文件,使其適應mysql認證
創建虛擬用戶映射的系統用戶及對應的目錄
#useradd -s /sbin/nologin -d /var/ftproot vuser
#chmod go+rx /var/ftproot
請確保/etc/vsftpd.conf中已經啓用瞭如下選項
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES
然後添加如下選項
guest_enable=YES
guest_username=vuser
並確保pam_service_name選項的值以下所示
pam_service_name=vsftpd.mysql
4、啓動vsftpd服務
# service vsftpd start
# chkconfig vsftpd on
查看端口開啓狀況
# netstat -tnlp |grep :21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 23286/vsftpd
基於mysql控制vsftp的用戶認證機制(兩臺服務器)
第一mysql準備工做:假如:172.16.10.109爲mysql 數據庫:
1:創建vsftp庫:
mysql> create database vsftpd;
2:給來來訪的的vsftp電腦受權
mysql> grant select on vsftpd.* to vsftpd@172.16.10.107 identified by '123456';
MariaDB [vsftpd]> FLUSH PRIVILEGES;
3:建立用戶表:
mysql> create table users (id int AUTO_INCREMENT NOT NULL,name char(20) binary NOT NULL,password char(48) binary NOT NULL,primary key(id));
4:添加測試的虛擬用戶
mysql> INSERT INTO users (name,password) VALUES ('tom',password('tompassword'));
mysql> INSERT INTO users (name,password) VALUES ('jack',password('jackpassword'));
在ftp服務鏈接mysql數據庫服務器,看鏈接的上不,注意防火牆和SElinux
# mysql -uvsftpd -h172.16.10.109 -p
第二配置vsftpd,服務器IP爲172.16.10.107
1:創建pam認證的文件#vi /etc/pam.d/vsftpd.mysql
添加以下兩行
auth required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required /lib64/security/pam_mysql.so user=vsftpd passwd=123456 host=172.16.10.109 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
2.修改vsftpd的配置文件,使其適應mysql認證
創建虛擬用戶映射的系統用戶及對應的目錄
#useradd -s /sbin/nologin -d /var/ftproot vuser
#chmod go+rx /var/ftproot
請確保/etc/vsftpd.conf中已經啓用瞭如下選項
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES
然後添加如下選項
guest_enable=YES
guest_username=vuser
並確保pam_service_name選項的值以下所示
pam_service_name=vsftpd.mysql
4、啓動vsftpd服務
# service vsftpd start
# chkconfig vsftpd on
查看端口開啓狀況
# netstat -tnlp |grep :21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 23286/vsftpd
使用虛擬用戶登陸,驗正配置結果,默認爲只讀訪問。
5、配置虛擬用戶具備不一樣的訪問權限
vsftpd能夠在配置文件目錄中爲每一個用戶提供單獨的配置文件以定義其ftp服務訪問權限,每一個虛擬用戶的配置文件名同虛擬用戶的用戶名。配置文件目錄能夠是任意未使用目錄,只須要在vsftpd.conf指定其路徑及名稱便可。
一、配置vsftpd爲虛擬用戶使用配置文件目錄
# vim vsftpd.conf
添加以下選項
user_config_dir=/etc/vsftpd/vusers
二、建立所須要目錄,併爲虛擬用戶提供配置文件
# mkdir /etc/vsftpd/vusers
# cd /etc/vsftpd/vusers
# touch tom jerry
三、配置虛擬用戶tom的訪問權限:
anon_upload_enable=YES 可上傳
anon_mkdir_write_enable=YES 能夠創建目錄
anon_other_write_enable=YES 其餘全部權限
jack只能上傳
anon_upload_enable=YES
ftp鏈接後的操做命令:
mkdir 創建目錄
rmdir 刪除目錄
del 刪除文件
put 將本地文件上傳到主機
get 下載