Fedora 服務器之 FTP 配置
(我遇到的問題:root無法登陸--500 OOPS: cannot change directory:/root和500 OOPS:priv_sock_get_cmd的錯誤
解決辦法:其他的網上搜,
主要注意點:修改/etc/sysconfig/selinux文件:
#SELINUX=enforcing
SELINUX=disabled
重啟生效,如果不想重啟,用命令(要重啟--搞了半天坑爹)
#setenforce 0
)
比如:
非常高的安全性需求
帶寬限制
良好的可伸縮性
創建虛擬用戶的可能性
IPv6支持
中等偏上的性能
分配虛擬 IP 的可能性
高速
目錄
一.ftp原理與vsftpd安裝
二.設置匿名用戶也支持下載和上傳,創建目錄
三.基於系統用戶支持ftp上傳和訪問
四.搭建支持SSL加密傳輸的vftpd
五.vsftpd.conf配置詳解
六.建立多個虛擬用戶支持ftp不同訪問權限
七.利用quota對Vsftpd做磁盤配額
(一)ftp原理與vsftp安裝
1.ftp原理
FTP Transfer Protocol 件傳輸協議的縮寫,在RFC 959中具體說明。
FTP會話時包含了兩個通道,一個叫控制通道,一個叫數據通道。

控制通道:控制通道是和FTP服務器進行溝通的通道,連接FTP,發送FTP指令都是通過控制通道來完成的, PI稱為控制連接
數據通道:數據通道是和FTP服務器進行文件傳輸或者列表的通道。
FTP協議中,控制連接均有客戶端發起,而數據連接有兩種工作方式:PORT方式和PASV方式,DTP稱為數據連接
PORT模式(主動方式)
FTP 客戶端首先和FTP Server的TCP 21端口建立連接,通過這個通道發送命令,客戶端需要接收數據的時候在這個通道上發送PORT命令。 PORT命令包含了客戶端用什么端口(一個大於1024的端口)接收數據。在傳送數據的時候,服務器端通過自己的TCP 20端口發送數據。 FTP server必須和客戶端建立一個新的連接用來傳送數據。

PASV模式(被動方式)
在建立控制通道的時候和PORT模式類似,當客戶端通過這個通道發送 PASV 命令的時候,FTP server打開一個位於1024和5000之間的隨機端口並且通知客戶端在這個端口上傳送數據的請求,然后FTP server 將通過這個端口進行數據的傳送,這個時候FTP server不再需要建立一個新的和客戶端之間的連接傳送數據。

如果從C/S模型這個角度來說,PORT對於服務器來說是OUTBOUND,而PASV模式對於服務器是INBOUND,這一點請特別注意,尤其是在使用防火牆的企業里,這一點非常關鍵,如果設置錯了,那么客戶將無法連接。
2.安裝vsftpd
[root@localhost /]# rpm -qa |grep vsftpd 查詢是否安裝了vsftpd(提示無)
[root@localhost /]# mkdir /mnt/cdrom 創建掛載光驅目錄文件
[root@localhost /]# mount /dev/cdrom /mnt/cdrom/ 掛載光驅到剛創建目錄
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@localhost /]# cd /mnt/cdrom/Server/
[root@localhost Server]# rpm -ivh vsftpd-2.0.5-12.el5.i386.rpm 安裝vsftp RPM包
warning: vsftpd-2.0.5-12.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing... ########################################### [100%]
1:vsftpd ########################################### [100%]
[root@localhost Server]# rpm -qa |grep vsftpd再查詢是否安裝了vsftpd
vsftpd-2.0.5-12.el5
[root@localhost ~]# rpm -qi vsftpd 查詢vsftpd詳細信息
Name : vsftpd Relocations: (not relocatable)
Version : 2.0.5 Vendor: Red Hat, Inc.
Release : 12.el5 Build Date: 2007年12月13日 星期四 19時35分37秒
Install Date: 2009年12月26日 星期六 00時35分03秒 Build Host: ls20-bc1-14.build.redhat.com
Group : System Environment/Daemons Source RPM: vsftpd-2.0.5-12.el5.src.rpm
Size : 289673 License: GPL
Signature : DSA/SHA1, 2007年12月17日 星期一 23時43分34秒, Key ID 5326810137017186
Packager : Red Hat, Inc.
URL : http://vsftpd.beasts.org/
Summary : vsftpd - 非常安全 Ftp 守護進程
Description :
vsftpd 是一個非常安全 FTP 守護進程。它是完全從零開始編寫的。
[root@localhost ~]# rpm -ql vsftpd 列出vsftpd中包含文件
/etc/logrotate.d/vsftpd.log vsftpd的日志文件
/etc/pam.d/vsftpd PAM認證文件
/etc/rc.d/init.d/vsftpd 啟動腳本
/etc/vsftpd vsftpd的配置文件存放的目錄
/etc/vsftpd/ftpusers 禁止使用vsftpd的用戶列表文件
/etc/vsftpd/user_list 禁止或允許使用vsftpd的用戶列表文件
/etc/vsftpd/vsftpd.conf 主配置文件
/etc/vsftpd/vsftpd_conf_migrate.sh vsftpd操作的一些變量和設置
/usr/sbin/vsftpd vsftpd的主程序
其他一些說明文檔和手冊文件略!
/var/ftp 匿名用戶主目錄
/var/ftp/pub 匿名用戶的下載目錄
[root@localhost /]# service vsftpd start 啟動vsftpd
為 vsftpd 啟動 vsftpd: [確定]
[root@localhost ~]# chkconfig --level 3 vsftpd on打開3級別隨系統自動啟動vsftpd
(二)設置匿名用戶也支持下載和上傳與創建目錄
1.vsftpd的匿名用戶默認只支持下載權限
[root@localhost /]# ftp 127.0.0.1 測試ftp用匿名用戶登陸到本地服務器
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 登陸成功(默認vsftp是開啟匿名登錄)
接下來測試匿名用戶上傳與下載
[root@localhost ~]# touch /var/ftp/pub/test.txt 先在匿名用戶下載目錄創建一個test.txt文件
[root@localhost /]# cd root
[root@localhost ~]# touch test1.txt 進入根目錄創建一個test1.txt文件,作為上傳測試
ftp> cd pub
ftp> ls
227 Entering Passive Mode (127,0,0,12,92,5)
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 6 Dec 28 14:59 test.txt
226 Directory send OK.
ftp> mget test.txt 下載text.txt
mget test.txt?
227 Entering Passive Mode (127,0,0,12,123,33)
150 Opening BINARY mode data connection for test.txt (6 bytes).
226 File send OK.
6 bytes received in 0.0047 seconds (1.2 Kbytes/s)
ftp> !ls 測試是否下載到本地
anaconda-ks.cfg install.log install.log.syslog test1.txt test.txt 下載成功
ftp> put test1.txt 上傳test1.txt
local: test1.txt remote: test1.txt
200 PORT command successful. Consider using PASV.
550 Permission denied. 請求被拒絕,說明沒有上傳權限
ftp> delete test.txt
550 Permission denied. 請求被拒絕,說明沒有刪除權限
2.讓匿名用戶支持上傳功能,下載,創建目錄文件的權限
要實現這個功能,必須做三件事情(測試環境是在關閉selinux情況下)
修改/etc/sysconfig/selinux文件:
#SELINUX=enforcing
SELINUX=disabled
重啟生效,如果不想重啟,用命令
#setenforce 0
(1)修改/etc/vsftpd/vsftpd.conf 去掉注釋anon_upload_enable=YES
(2)修改/etc/vsftpd/vsftpd.conf 去掉注釋anon_mkdir_write_enable=YES
以上兩個步驟如下:
[root@localhost ~]# vi /etc/vsftpd/vsftpd.conf
25 # has an effect if the above global write enable is activated. Also, you will
26 # obviously need to create a directory writable by the FTP user.
27 anon_upload_enable=YES
28 #
29 # Uncomment this if you want the anonymous FTP user to be able to create
30 # new directories.
31 anon_mkdir_write_enable=YES
32 #
33 # Activate directory messages - messages given to remote users when they
34 # go into a certain directory.
35 dirmessage_enable=YES
(3)文件系統中FTP匿名用戶對某個目錄有寫權限
[root@localhost ~]# cat /etc/passwd |grep ftp
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologinà/var/ftp ftp用戶的家目錄 /sbin/nologin 不支持系統登錄,只能作為虛擬用戶用來登錄vsftpd
創建一個名為put目錄,並定義這個文件的所有者為ftp
[root@localhost ftp]# mkdir put
[root@localhost ftp]# chown ftp put 修改文件所有者為ftp
[root@localhost ftp]# ll
總計 8
drwxr-xr-x 2 root root 4096 2007-12-13 pub
drwxr-xr-x 2 ftp root 4096 12-29 18:13 put
[root@localhost ftp]# service vsftpd restart 重啟vsftpd服務
關閉vsftpd: [確定]
為vsftpd啟動vsftpd: [確定]
提示:要想讓匿名用戶支持刪除和更名權限,必須在vsftpd.conf加入以下參數
anon_other_write_enable=YES 允許匿名賬號具有刪除.更名權限
[root@uhome ~]# ftp 127.0.0.1 測試結果
Connected to 127.0.0.1 (127.0.0.1).
220 (vsFTPd 2.0.5)
Name (127.0.0.1:root): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (127,0,0,1,116,27).
150 Here comes the directory listing.
drwxrwxrwx 2 0 0 4096 Mar 05 02:28 pub
drwxrwxrwx 2 14 0 4096 Mar 05 01:31 put
226 Directory send OK.
ftp> cd put
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (127,0,0,1,186,61).
150 Here comes the directory listing.
226 Directory send OK.
ftp> !ls
anaconda-ks.cfg install.log.syslog test.txt 模板 圖片 下載 桌面
install.log test1.txt 公共的 視頻 文檔 音樂
ftp> put test1.txt
local: test1.txt remote: test1.txt
227 Entering Passive Mode (127,0,0,1,240,27).
150 Ok to send data.
226 File receive OK.
ftp> ls
227 Entering Passive Mode (127,0,0,1,238,74).
150 Here comes the directory listing.
-rw------- 1 14 50 0 Mar 05 02:30 test1.txt
226 Directory send OK.
(三)基於系統用戶支持ftp上傳和訪問
1.關閉匿名用戶登錄 ftp支持匿名登錄是不安全,所以要禁止匿名ftp登錄在/etc/vsftpd/vsftpd.conf修改以下三項
anonymous_enable=NO
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
[root@red-hat-5 ~]# service vsftpd restart
關閉vsftpd: [確定]
為vsftpd啟動vsftpd: [確定]
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): anonymous
331 Please specify the password. ------>提示登錄失敗,說明已經禁 用匿名登錄
Password:
530 Login incorrect.
Login failed.
2.創建一個系統用戶來登錄ftp
[root@red-hat-5 ~]# useradd -s /sbin/nologin uhome
建設一個不能登錄系統用戶. 只用來登錄ftp服務 ,這里如果沒設置用戶目錄。默認是在home下
[root@red-hat-5 ~]# passwd uhome
Changing password for user uhome.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): uhome
331 Please specify the password. ------>提示登錄成功
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
接下來測試權限
ftp> put test.txt 上傳test.txt文件成功
local: test.txt remote: test.txt
227 Entering Passive Mode (127,0,0,1,98,115)
150 Ok to send data.
226 File receive OK.
ftp> mkdir hong 創建hong目錄文件成功
257 "/home/uhome/hong" created
ftp> rmdir hong 刪除hong目錄文件成功
250 Remove directory operation successful.
ftp> pwd 顯示家目錄
257 "/home/uhome"
ftp> cd /etc 切換/etc成功
250 Directory successfully changed.
ftp> mget passwd 下載passwd
mget passwd?
227 Entering Passive Mode (127,0,0,1,220,77)
150 Opening BINARY mode data connection for passwd (1429 bytes).
226 File send OK.
1429 bytes received in 5.9e-05 seconds (2.4e+04 Kbytes/s)
ftp> !ls 顯示下載passwd成功
anaconda-ks.cfg install.log install.log.syslog passwd test.txt
3.加強vsftp安全設置
從以上可以看出ftp家目錄存在安全漏洞,所以要修改以下設置:
(1)限制系統用戶鎖定在家目錄
#Vi /etc/vsftpd/vsftpd.conf
去掉前面#號
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list 限制更多系統用戶
然后把所有用戶加入/etc/vsftpd/chroot_list即可
[root@red-hat-5 ~]# ls /etc/vsftpd/chroot_list
默認是不存在,需要我們手動建立
[root@red-hat-5 ~]# ls /etc/vsftpd/
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh
[root@red-hat-5 ~]# touch /etc/vsftpd/chroot_list
[root@red-hat-5 ~]# cut -d : -f 1 /etc/passwd>>/etc/vsftpd/chroot_list
把本地用戶都加入到chroot_list
cut命令是切去某一列,-d是每列的分隔符,-f 是切取第幾列,然后重定向到chroot文件
[root@red-hat-5 ~]# ll /etc/vsftpd/
總計 24
-rw-r--r-- 1 root root 197 12-25 19:57 chroot_list
-rw--------1 root root 125 2007-12-13 ftpusers
ftpusers指的是阻止這個文件中的用戶登陸
-rw------- 1 root root 361 2007-12-13 user_list
-rw------- 1 root root 4396 12-25 19:19 vsftpd.conf
-rwxr--r-- 1 root root 338 2007-12-13 vsftpd_conf_migrate.sh
(2)限制重要系統用戶不能登錄ftp權限
[root@red-hat-5 ~]# cat /etc/vsftpd/ftpusers
默認會加入一些比較重要系統用戶
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@red-hat-5 ~]# echo uhome >> /etc/vsftpd/ftpusers
把之前建的uhome賬號加進去測試
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): uhome
331 Please specify the password. ----->提示登錄失敗,說明生效了
Password:
530 Login incorrect.
Login failed.
(3)利用ftp用戶策略允許登錄ftp的系統用戶 系統添加一個用戶也默認有ftp的登陸權限,是不安全,要一個個設置,有點繁瑣。利用ftp用戶策略解決這個問題,即user_list文件設置,只有user_list中存在的用戶才能登錄系統
修改配置文件:#vi /etc/vsftpd/vsftpd.conf
在userlist_enable=YES文件后面添加
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list
[root@red-hat-5 ~]# service vsftpd restart
關閉vsftpd: [確定]
為vsftpd啟動vsftpd: [確定]
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): uhome
530 Permission denied. ------------->提示登錄失敗
Login failed.
[root@red-hat-5 ~]# echo uhome >> /etc/vsftpd/user_list
把viong賬號加入到 user_list
[root@red-hat-5 ~]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): uhome
331 Please specify the password. ------------->提示登錄成功
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
(4)設置登錄ftp目標ip地址 為了讓ftp更安全,我們設置ftp目標ip地址訪問
C:\Users\Administrator>ifconfig 查看本地ip
...................................
以太網適配器 VMware Network Adapter VMnet1:6
連接特定的 DNS 后綴 . . . . . . . :
IPv4 地址 . . . . . . . . . . . . : 192.168.184.1
子網掩碼 . . . . . . . . . . . . : 255.255.255.0
默認網關. . . . . . . . . . . . . :
只允許這個ip地址訪問ftp ssh,可以寫條iptable做限制.
如下:
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 22 -j ACCEPT
允許192.168.184.1訪問本地 22端口
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 21 -j ACCEPT
允許192.168.184.1訪問本地 21端口
[root@red-hat-5 ~]# iptables -A INPUT -p tcp -s 192.168.184.1 --dport 65341:65351 -j ACCEPT
允許192.168.184.1訪問本地 PASV端口
[root@red-hat-5 ~]# iptables -P INPUT DROP 禁止任何輸入的數據包
[root@red-hat-5 ~]# service iptables save 保存iptables設置
將當前規則保存到 /etc/sysconfig/iptables: [確定]
[root@red-hat-5 ~]# service iptables status 檢查iptables的設置
表格:filter
Chain INPUT (policy DROP)
num target prot opt source destination
1 RH-Firewall-1-INPUT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT tcp -- 192.168.184.1 0.0.0.0/0 tcp dpt:22
3 ACCEPT tcp -- 192.168.184.1 0.0.0.0/0 tcp dpt:21
4 ACCEPT tcp -192.168.184.1 0.0.0.0/0 tcp dpts:65341:65351
(四)搭建支持SSL加密傳輸的vftpd
ftp傳輸數據是明文,弄個抓包軟件就可以通過數據包來分析到賬號和密碼,為了搭建一個安全性比較高ftp,可以結合SSL來解決問題 SSL(Secure Socket Layer)工作於傳輸層和應用程序之間.作為一個中間層,應用程序只要采用SSL提供的一套SSL套接字API來替換標准的Socket套接字,就可以把程序轉換為SSL化的安全網絡程序,在傳輸過程中將由SSL協議實現數據機密性和完整性的保證.SSL取得大規模成功后,IETF將SSL作了標准化,並將其稱為TLS(Transport Layer Security).Ftp結合SSL,將實現傳輸數據的加密,保證數據不被別人竊取.
下面我們使用linux自帶的抓包工具tcpdump抓包分析,來截取ftp登錄用戶口令
[root@red-hat-5 vsftpd]# tcpdump -i eth0 -A |more
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
……………………………….
20:40:26.208724 IP 192.168.184.1.54516 > 192.168.184.129.ftp: S 289306029:28
9306029(0) win 8192
.............>u.....p. ..V..........
20:40:26.210838 IP 192.168.184.129.ftp > 192.168.184.1.54516: S 1292926425:1
292926425(0) ack 289306030 win 5840
E..0..@.@.H.............M.}..>u.p...S...........
20:40:26.212600 IP 192.168.184.1.54516 > 192.168.184.129.ftp: . ack 1 win 64
240
E..(m.@..................>u.M.}.P.............
20:40:26.229597 IP 192.168.184.129.ftp > 192.168.184.1.54516: P 42:48(6) ack
1 win 5840
E....2@.@...............M.~..>u.P.......220
20:40:26.251901 IP 192.168.184.1.54516 > 192.168.184.129.ftp: P 1:16(15) ack
48 win 64193
E..7m.@..................>u.M.~ P...#...USER uhome
20:40:26.251989 IP 192.168.184.129.ftp > 192.168.184.1.54516: . ack 16 win 5
840
E..(.3@.@...............M.~ .>u.P.......
20:40:26.252116 IP 192.168.184.129.ftp > 192.168.184.1.54516: P 48:82(34) ac
k 16 win 5840
E..J.4@.@...............M.~ .>u.P...8...331 Please specify the password.
20:40:26.255680 IP 192.168.184.1.54516 > 192.168.184.129.ftp: P 16:31(15) ac
k 82 win 64159
E..7m.@..................>u.M.~+P....3..PASS 123456
E..Nm......R.............:.!............ EJFDEBFEEBFACACACACACACACACACAAA.. ..
20:40:31.301262 IP 192.168.184.129.ftp > 192.168.184.1.54516: P 82:105(23) ack 31
win 5840
E..?.6@.@...............M.~+.>u.P....H..230 Login successful.
從我們抓的數據包,可以看到賬號密碼,所以明文傳輸的數據安全性太可怕了 讓vsftpd支持SSL,必須讓OPENSSL≥0.9.6版本,還有就是本身vsftpd版本是否支持
查詢vsftpd軟件是否支持SSL
[root@localhost vsftpd]# ldd /usr/sbin/vsftpd |grep libssl
libssl.so.6 => /lib/libssl.so.6 (0xf7f27000) 說明此版本支持
如沒有輸出libssl.so.6 => /lib/libssl.so.6 (0xf7f27000)類似文本,說明此vsftpd版本不支持SSL
[root@red-hat-5 ~]#openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout /etc/vsftpd/vsftpd.pem -out /etc/vsftpd/vsftpd.pem 生成vsftpd.pem 證書
Generating a 1024 bit RSA private key
..++++++
....................................++++++
writing new private key to 'vsftpd.pem'
-----
You are about to be asked to enter information that will be incorporated
into 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 blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:cn
State or Province Name (full name) [Berkshire]: shanghai
Locality Name (eg, city) [Newbury]:shanghai
Organization Name (eg, company) [My Company Ltd]:lenovo 根據提示填寫一些信息
Organizational Unit Name (eg, section) []:lenovo
Common Name (eg, your name or your server's hostname) []:uhome
Email Address []:uhome@uhome.com
[root@localhost ~]# ll /etc/vsftpd/ ==è查看是否生成vsftpd.pem文件
-rw-r--r-- 1 root root 197 12-25 19:57 chroot_list
-rw--------1 root root 125 2007-12-13 ftpusers
-rw------- 1 root root 361 2007-12-13 user_list
-rw------- 1 root root 4396 12-25 19:19 vsftpd.conf
-rwxr--r-- 1 root root 338 2007-12-13 vsftpd_conf_migrate.sh
-rw-r--r-- 1 root root 2168 01-08 01:53 vsftpd.pem 生成vsftpd.pem成功
[root@red-hat-5 ~]# vi /etc/vsftpd/vsftpd.conf 編輯主配置文件,添加以下參數
ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
rsa_cert_file=/etc/vsftpd/vsftpd.pem
下面是ssl參數一些定義,根據自己需求去修改
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
[root@red-hat-5 ~]# service vsftpd restart
關閉vsftpd: [確定]
為vsftpd啟動vsftpd: [確定]
通過LeapFTP連接FTP
打開LeapFTP按F4跳出站點管理器.填寫ftp目標地址並選擇連接類型




(五)vsftpd.conf配置詳解
1.根據 /etc/vsftpd/vsftpd.conf默認配置給出設定功能 # Example config file /etc/vsftpd/vsftpd.conf
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
# 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
允許使用任何可以修改文件系統的FTP的指令
# 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
允許為目錄配置顯示信息,顯示每個目錄下面的message_file文件的內容
# Activate logging of uploads/downloads.
xferlog_enable=YES
開啟日記功能
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
使用標准的20端口來連接ftp
# 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
#chown_username=whoever
匿名上傳文件所屬用戶名
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
日志文件位置
# If you want, you can have your log file in standard ftpd xferlog format
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
允許使用\"async ABOR\"命令,一般不用,容易出問題
# 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 模式上傳。默認值為NO
#ascii_download_enable=YES
管控是否可用ASCII 模式下載。默認值為NO
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
login時顯示歡迎信息.如果設置了banner_file則此設置無效
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
如果匿名用戶需要密碼,那么使用banned_email_file里面的電子郵件地址的用戶不能登錄
# (default follows)
#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_list_enable=YES
如果啟動這項功能,則所有列在chroot_list_file中的使用者不能更改根目錄
# (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
是否能使用ls -R命令以防止浪費大量的服務器資源
# 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
綁定到listen_port指定的端口,既然都綁定了也就是每時都開着的,就是那個什么
standalone模式
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd whith two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
pam_service_name=vsftpd
定義PAM 所使用的名稱,預設為vsftpd
userlist_enable=YES
若啟用此選項,userlist_deny選項才被啟動
tcp_wrappers=YES
開啟tcp_wrappers支持
2.過濾掉那些注釋,以便我們日后修改配置,大家可以刪除vsftpd.conf內容,拷貝以下:
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
#anon_upload_enable=YES
#anon_mkdir_write_enable=YES
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
#chown_uploads=YES
#chown_username=whoever
#xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
#idle_session_timeout=600
#data_connection_timeout=120
#nopriv_user=ftpsecure
#async_abor_enable=YES
#ascii_upload_enable=YES
#ascii_download_enable=YES
#ftpd_banner=Welcome to blah FTP service.
#deny_email_enable=YES
#banned_email_file=/etc/vsftpd/banned_emails
#chroot_list_enable=YES
#chroot_list_file=/etc/vsftpd/chroot_list
#ls_recurse_enable=YES
listen=YES
#listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
為了讓vsftpd的設置選項更全面, 提供一份超詳細vsftpd.conf供查詢參考,具體見 附件 。
(六)建立多個虛擬用戶支持ftp不同訪問權限
實驗環境:
公司最近上了一款游戲,當游戲客戶端登錄到服務器有補丁更新時,是通過ftp來更新數據.所以需要建立一個普通虛擬用戶賬號,用於客戶端登錄ftp服務器更新補丁用,此賬號只支持下載權限.由於平時我們也要維護服務器后台上傳數據,需要建立一個專用虛擬用戶賬號,此賬號擁有所有權限.還要把上傳數據發布到普通虛擬用戶家目錄下,提供下載!
解決方案:
1.創建用戶數據庫
(1)創建用戶文本文件先建立用戶文本文件vsftpd_login.txt
添加兩個虛擬帳號,普通帳號public和專用帳號personal
[root@red-hat-5 ~]# touch /etc/vsftpd/vsftpd_login.txt
[root@red-hat-5 ~]# echo public >>/etc/vsftpd/vsftpd_login.txt 生成public賬號
[root@red-hat-5 ~]# echo PUBLIC >>/etc/vsftpd/vsftpd_login.txt 生成public密碼
[root@red-hat-5 ~]# echo personal >>/etc/vsftpd/vsftpd_login.txt 生成personal賬號
[root@red-hat-5 ~]# echo PERSONAL >>/etc/vsftpd/vsftpd_login.txt 生成personal密碼
[root@red-hat-5 ~]# cat /etc/vsftpd/vsftpd_login.txt
Public 賬號
PUBLIC 密碼
Personal 賬號
PERSONAL 密碼
(2)生成數據庫
保存虛擬帳號和密碼的文本文件無法被系統帳號直接調用哈~我們需要使用db_load命令生成db口令數據庫文件
db_load -T -t hash -f /etc/vsftpd/vsftpd_login.txt /etc/vsftpd/vsftpd_login.db
要使用db_load這個命令,需要安裝三個文件,
db4-4.3.29-9.fc6.i386.rpm
db4-devel-4.3.29-9.fc6.i386.rpm
db4-utils-4.3.29-9.fc6.i386.rpm
[root@red-hat-5 ~]# rpm -qa|grep db4 只安裝了一個
db4-4.3.29-9.fc6
[root@red-hat-5 mnt]# mount /dev/cdrom /mnt/cdrom/ 掛載光驅
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@red-hat-5 cdrom]# cd Server/
[root@red-hat-5 Server]# rpm -ivh db4-devel-4.3.29-9.fc6.i386.rpm
warning: db4-devel-4.3.29-9.fc6.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing... ########################################### [100%]
1:db4-devel ########################################### [100%]
[root@red-hat-5 Server]# rpm -ivh db4-utils-4.3.29-9.fc6.i386.rpm
warning: db4-utils-4.3.29-9.fc6.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing... ########################################### [100%]
1:db4-utils ########################################### [100%]
[root@red-hat-5 Server]# cd /
[root@red-hat-5 /]# rpm -qa|grep db4
db4-4.3.29-9.fc6
db4-utils-4.3.29-9.fc6
db4-devel-4.3.29-9.fc6
[root@red-hat-5/]#db_load -T -t hash –f /etc/vsftpd/vsftpd_login.txt /etc/vsftpd/vsftpd_login.db
[root@red-hat-5 /]# ll /etc/vsftpd/
總計 48
-rw-r--r-- 1 root root 197 12-25 19:57 chroot_list
-rw------- 1 root root 126 12-25 20:06 ftpusers
-rw-r--r-- 1 root root 32 12-25 22:22 vsftpd_login.txt
-rw------- 1 root root 367 12-25 20:37 user_list
-rw------- 1 root root 4449 12-25 20:34 vsftpd.conf
-rwxr--r-- 1 root root 338 2007-12-13 vsftpd_conf_migrate.sh
-rw-r--r-- 1 root root 12288 12-25 23:12 vsftpd_login.db 生成數據庫成功
-rw-r--r-- 1 root root 32 12-25 22:42 vsftpd_login.txt
-rw-r--r-- 1 root root 7 12-25 22:40 vtpd_login.txtsf
(3)修改數據庫文件訪問權限
數據庫文件中保存着虛擬帳號的密碼信息,為了防止非法用戶盜取哈,我們可以修改該文件的訪問權限。生成的認證文件的權限應設置為只對root用戶可讀可寫,即600
[root@red-hat-5 /]# chmod 600 /etc/vsftpd/vsftpd_login.db
[root@red-hat-5 /]# ll /etc/vsftpd/vsftpd_login.db
-rw------- 1 root root 12288 12-25 23:12 /etc/vsftpd/vsftpd_login.db
2.配置PAM文件
為了使服務器能夠使用數據庫文件,對客戶端進行身份驗證,需要調用系統的PAM模塊.PAM(Plugable Authentication Module)為可插拔認證模塊,不必重新安裝應用系統,通過修改指定的配置文件,調整對該程序的認證方式。PAM模塊配置文件路徑為/etc/pam.d/目錄,此目錄下保存着大量與認證有關的配置文件,並以服務名稱命名。
[root@red-hat-5 /]# vi /etc/pam.d/vsftpd
#%PAM-1.0
#session optional pam_keyinit.so force revoke
#auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth required pam_shells.so
auth include system-auth
account include system-auth
session include system-auth
session required pam_loginuid.so
修改vsftpd對應的PAM配置文件/etc/pam.d/vsftpd,將默認配置使用“#”全部注釋,添加相應字段
[root@red-hat-5 /]# cat /etc/pam.d/vsftpd
#%PAM-1.0
#session optional pam_keyinit.so force revoke
#auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth required pam_shells.so
#auth include system-auth
#account include system-auth
#session include system-auth
#session required pam_loginuid.so
auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
注意:db=/etc/vsftpd/vsftpd_login 格式是這樣的,去掉.db后綴
3. 創建虛擬帳號對應的系統用戶
對於普通帳號和專用帳號,因為需要配置不同的權限,所以可以將兩個帳號的目錄進行隔離,控制用戶的文件訪問。
普通帳號public對應系統帳號ftp_public,並指定其主目錄為/var/ftp/public
chmod -R 500 /var/ftp/public/ :
普通帳號public只允許下載,修改public目錄其他用戶權限為rx可
讀可執行。
專用帳號personal對應系統帳號ftp_personal,指定主目錄為/var/ftp/personal
chmod -R 700 /var/ftp/ personal/ :
專用帳號personal允許上傳和下載,所以對personal目錄權限設置為rwx,可讀可寫可執行。 如果不設置可執行用戶登錄會出不能更改目錄錯誤。
[root@red-hat-5 ftp]# useradd -d /var/ftp/personal/ -s /sbin/nologin ftp_personal
[root@red-hat-5 ftp]# useradd -d /var/ftp/public/ -s /sbin/nologin ftp_public
[root@red-hat-5 ftp]# ll
drwx------ 2 ftp_personal ftp_personal 4096 12-26 00:41 personal
drwxr-xr-x 2 root root 4096 12-25 09:12 pub
drwx------ 2 ftp_public ftp_public 4096 12-26 00:41 public
[root@red-hat-5 ftp]# chmod -R 500 /var/ftp/public/
[root@red-hat-5 ftp]# chmod -R 700 /var/ftp/personal/
[root@red-hat-5 ftp]# ll
總計 12
drwx------ 2 ftp_personal ftp_personal 4096 12-26 00:41 personal
drwxr-xr-x 2 root root 4096 12-25 09:12 pub
dr-x------ 2 ftp_public ftp_public 4096 12-26 00:41 public
4.建立配置文件
設置多個虛擬帳號的不同權限,若使用一個配置文件無法實現此功能,需要為每個虛擬帳號建立獨立的配置文件,並根據需要進行相應的設置。
(1)修改vsftpd.conf主配置文件
配置主配置文件/etc/vsftpd/vsftpd.conf添加虛擬帳號的共同設置並添加user_config_dir字段,定義虛擬帳號的配置文件目錄,並anonymous_enable=NO(修改配置)
禁用匿名用戶登錄
write_enable=YES (默認開啟)
允許使用任何可以修改文件系統的FTP的指令
local_enable=YES (默認開啟)
啟用本地用戶登錄設置
chroot_local_enable=YES (自建配置)
將所有本地用戶限制在家目錄中
pam_service_name=vsftpd:(默認開啟)
配置vsftpd使用的PAM模塊為vsftpd
user_config_dir=/etc/vsftpd/ vsftpd_login:(自建配置)
設置虛擬帳號的主目錄為/ vsftpd_login
max_clients=300:(自建配置)
設置FTP服務器最大接入客戶端數為300個
max_per_ip=10: (自建配置)
設置每個IP地址最大連接數為10個
port_enable=NO (自建配置)
取消PORT模式進行數據傳輸
connect_from_port_20=NO (修改配置)
PORT模式進行數據傳輸部使用20端口
pasv_enable=YES (自建配置)
允許PASV模式進行數據傳輸
pasv_min_port=65341 (自建配置)
PASV模式下數據傳輸所使用port范圍下界
pasv_max_port=65351 (自建配置)
PASV模式下數據傳輸所使用port范圍上界
注意:主配置文件是虛擬賬號共享的配置,所以主配置文件的設置對於虛擬賬號是生效的
提示:每行的值都不要有空格,否則啟動時會出現錯誤,舉個例子,假如我在listen=YES后多了個空格,那我啟動時就出現如下錯誤:500 OOPS: bad bool value in config file for: listen
(2)建立虛擬帳號配置文件
在user_config_dir指定路徑下,建立與虛擬帳號同名的配置文件並添加相應的配置字段
[root@red-hat-5 vsftpd]# mkdir /vsftpd_login
[root@red-hat-5 vsftpd]# touch /etc/vsftpd/vsftpd_login/public
[root@red-hat-5 vsftpd]# touch /etc/vsftpd//vsftpd_login/personal
首先建立普通帳號public的配置文件
[root@red-hat-5 vsftpd_login]# echo guest_enable=yes >>public
[root@red-hat-5 vsftpd_login]# echo guest_username=ftp_public >>public
[root@red-hat-5 vsftpd_login]# echo anon_world_readable_only=no >>public
[root@red-hat-5 vsftpd_login]# echo anon_max_rate=50000 >>public
[root@red-hat-5 vsftpd_login]# cat public
guest_enable=yes
開啟虛擬帳號登錄
guest_username=ftp_public
設置ftp對應的系統帳號為ftp_public
anon_world_readable_only=no
允許匿名用戶瀏覽器整個服務器的文件系統
anon_max_rate=50000
限定傳輸速率為50KB/s
注意:
vsftpd對於文件傳輸速度限制並不是絕對鎖定在一個數值上哈,而是在80%~120%之間變化哈~比如設置100KB/s則實際是速度在80KB/s~120KB/s之間變化哈~
接着建立專用帳號的配置文件personal
[root@red-hat-5 vsftpd_login]# echo guest_enable=yes >> personal
[root@red-hat-5 vsftpd_login]# echo guest_username=ftp_personal >> personal
[root@red-hat-5 vsftpd_login]# echo anon_world_readable_only=no >> personal
[root@red-hat-5 vsftpd_login]# echo anon_mkdir_write_enable=yes >> personal
[root@red-hat-5 vsftpd_login]# echo anon_upload_enable=yes >> personal
[root@red-hat-5 vsftpd_login]# echo anon_world_readable_only=no >> personal
[root@red-hat-5 vsftpd_login]# echo anon_max_rate=50000 >> personal
[root@red-hat-5 vsftpd_login]# cat personal
guest_enable=yes:
開啟虛擬帳號登錄
guest_username=ftp_ personal:
設置ftp對應的系統帳號為ftp_personal
anon_other_write_enable=YES:
允許匿名賬號具有刪除.更名權限
anon_mkdir_write_enable=yes:
允許創建文件夾
anon_upload_enable=yes:
開啟匿名帳號的上傳功能
anon_world_readable_only=no
允許匿名用戶瀏覽器整個服務器的文件系統
anon_max_rate=100000:
限定傳輸速度為100KB/s
[root@red-hat-5 ~]# service vsftpd restart
關閉vsftpd: [確定]
為vsftpd啟動vsftpd: [確定]
如果還是連不上,建議重新啟動一下系統
5.測試權限是否生效
[root@red-hat-5 viong]# ftp 127.0.0.1
Connected to 127.0.0.1.
220 (vsFTPd 2.0.5)
KERBEROS_V4 rejected as an authentication type
Name (127.0.0.1:root): public
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put test.txt
550 Permission denied
ftp> del public.txt
550 Permission denied.
ftp> mkdir uhome
550 Permission denied.
ftp> mget public.txt
mget public.txt?
227 Entering Passive Mode (127,0,0,1,181,255)
150 Opening BINARY mode data connection for public.txt (0 bytes).
226 File send OK.
通過測試得知public這個用戶只支持下載權限,說明設置成功
C:\Users\Administrator>ftp 192.168.11.126
連接到 192.168.11.126。
220 (vsFTPd 2.0.5)
用戶(192.168.11.126:(none)): personal
331 Please specify the password.
密碼:
230 Login successful.
ftp> mput personal.txt
mput personal.txt?
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
test.txt
public.txt
226 Directory send OK.
ftp: 收到 99 字節,用時 0.00秒 33.00千字節/秒。
ftp> mkdir personal
257 "/personal" created
ftp> get passsv
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for passsv(2000998 bytes).
226 File send OK.
ftp: 收到 2000998 字節,用時 38.71秒 51.69千字節/秒。
ftp> delete passsv
250 Delete operation successful.
ftp> rename personal.txt ptest.txt
350 Ready for RNTO.
250 Rename successful.
通過測試得知personal這個用戶支持上傳.下載.刪除.更改權限,說明設置成功
關於把上傳數據發布到普通虛擬用戶家目錄下提供下載!
[root@red-hat-5 ~]# cp /var/ftp/personal/* /var/ftp/public/
把上傳數據發布到普通虛擬用戶家目錄下提供下載
[root@red-hat-5 ~]# chown -R ftp_public.ftp_public /var/ftp/public/
更改public目錄的所有者和屬組為ftp_public,這樣public用戶才能下載
還有一種方法就是在personal配置文件加入以下兩個參數
chown_uploads=YES 激活匿名用戶所上傳文件的修改所有權
chown_username=root 擁有匿名用戶上傳文件所有權的用戶
然后就可以直接拷貝文件到public目錄下
(七)利用quota對Vsftpd做磁盤配額
關於vsftpd磁盤配額,這里強調一下,vsftpd本身不帶這個功能,是系統本身自帶的quota這個軟件,quota字面上就是限額的意思.
quota磁盤配額比較常見的作用:
1.避免惡意ftp用戶用垃圾數據塞滿寶貴的硬盤.
2.虛擬空間不同用戶的容量限制
3.郵局用戶的容量限制
4.多人多任務使用同一個硬盤,妥善分配系統資源
寫這篇文章之前,先來個小插曲
之前參考了別人的做法,不小心犯了一個大忌。之前自己在VM裝的as5。是默認分區,導致我在/etc/fstab添加了LABEL=/home /home ext3 defaults,usrquota 1 2,重啟文件系統檢查失敗!是因為我沒把home獨立分區出來。
解決repair filesystem方案:
第一種情況:非正常關機引起的磁盤分區問題 不能正常進入系統
repair filesystem 1 #
非正常關機引起的,用fsck /dev/hdaX后shutdown –r now進入,或只用fsck來修復,只管y回車
第二種情況:由於/etc/fstab文件編輯錯誤 而引起的不能正常進入系統
解決方法就是修改/etc/fstab文件成原來正常格式 刪除錯誤的或者是不存在的掛載目錄
輸入root密碼登陸到修復模式,但是修復模式下(read-only system) 文件是被保護的不能修改 運行.需要把系統文件權限改成可讀寫(rw),如下:
(Repair filesystem) 1 # mount -o remount,rw /
使根目錄可寫.即可以修復/etc/fstab文件,使之可寫.然后就可以vi修改了.
根據我的情況,我就直接把#LABEL=/home /home ext3 defaults,usrquota 1 2注釋掉
保存退出 :x
(Repair filesystem) 1 #shutdown –r now
掛載虛擬硬盤並格式化
上面是些局外話,接下來需要在VM上添加一塊2G虛擬硬盤
打開VMware點擊VM---settings—add—Hard Disk—---Create a new virtual disk—SCSI—disk size(GB)2.0----重啟虛擬機系統
[root@uhome ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1044 8281507+ 8e Linux LVM
Disk /dev/sdb: 2147 MB, 2147483648 bytes -----------說明已經生效了
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
先把/dev/sdb划分為一個擴展分區,再新建一個邏輯分區,格式化為ext3分區,然后設置為重啟自動加載。
[root@uhome ~]# fdisk /dev/sdb
對/dev/sdb進行分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n 增加一個分區
Command action
e extended
p primary partition (1-4)
e增加擴展分區
Partition number (1-4): 1
First cylinder (1-261, default 1): 回車
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): 回車
Using default value 261
Command (m for help): w保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@uhome ~]# fdisk /dev/sdb
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l增加邏輯分區
First cylinder (1-261, default 1): 回車
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): 回車
Using default value 261
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@uhome ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1044 8281507+ 8e Linux LVM
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 261 2096451 5 Extended
/dev/sdb5 1 261 2096419+ 83 Linux
[root@viong ~]# mkfs.ext3 /dev/sdb5 格式化sdb5
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
262144 inodes, 524104 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@uhome ~]# mkdir /mnt/ftp
創建一個目錄來掛載sdb5
[root@uhome ~]# mount /dev/sdb5 /mnt/ftp/
掛載sdb5到/mnt/ftp/
[root@uhome ~]# vi /etc/fstab
添加最后一條信息,來達到重啟自動掛載上去
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/sdb5 /mnt/ftp ext3 defaults 0 0
.
最后重啟系統看有沒有被自動掛載上
創建一個ftp用戶做測試
[root@red-hat-5 ~]# useradd -d /mnt/ftp/hom -s /sbin/nologin hom
[root@red-hat-5 ~]# passwd hom
Changing password for user hom.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@red-hat-5 ~]# ll /mnt/ftp/
總計 20
drwx------ 2 hom hom 4096 01-10 17:15 hom
drwx------ 2 root root 16384 01-10 16:53 lost+found
[root@red-hat-5 ~]# service vsftpd start
為 vsftpd 啟動 vsftpd:[確定]
為ftp用戶做磁盤配額
剛才創建的hom用戶是在/dev/sdb5分區中,那么如果我們要對hom用戶進行磁盤限額,那我們需要修改/etc/fstab中根分區的記錄,將/dev/sdb5分區的第4個字段改成defaults,usrquota,如下:
/dev/sdb5 /mnt/ftp ext3 defaults,usrquota 0 0
為了不然系統重啟才生效,利用一下命令直接生效
# mount -o remount /dev/sdb5
對一個組進行磁配額,那我們需要增加參數grpquota,如下:
/dev/sdb5 /mnt/ftp ext3 defaults,usrquota, grpquota 0 0
[root@uhome /]# quotacheck -avug
生成每個啟動了配額的文件系統的當前磁盤用量表
quotacheck: Scanning /dev/sdb5 [/mnt/ftp] quotacheck: Cannot stat old user quota file: 沒有那個文件或目錄
quotacheck: Old group file not found. Usage will not be substracted.
done
quotacheck: Checked 4 directories and 6 files
quotacheck: Old file not found.
注釋:
-a :掃瞄所有在 /etc/mtab 里頭已經 mount 的具有 quota 支持的磁盤
-u :掃瞄使用者的檔案與目錄
-v :顯示掃瞄過程
-g :掃瞄群組使用的檔案與目錄
-m :強制進行 quotacheck
[root@uhome /]# edquota -u hom
為用戶hom設置磁盤配額
系統會自動用
VI文本打開配額文件,如下:
Disk quotas for user hom (uid 501):
Filesystem blocks soft hard inodes soft hard
/dev/sdb5 16 0 1024 4 0 0
這里為了做測試,我把硬塊限度為1024kb
注釋:
Filesystem是啟用了配額的文件系統的名稱
blocks顯示了用戶當前使用的塊數,單位為
KB
soft用來設置用戶在該文件系統上的軟塊限度.使用者在寬限期間之內,他的容量可以超過 soft , 但必需要寬限時間之內將磁盤容量降低到 soft 的容量限制之下
hard用來設置用戶在該文件系統上的硬塊限度,絕對不能超過的容量
inodes顯示了用戶當前使用的i節點數量。
最后兩列用來設置用戶在該文件系統上的軟硬i
節點限度.不同的是軟限可以在一段時期內被超過。 soft 到 hard 之間的容量其實就是寬限的容量啦!可以達到針對使用者的警示作用!這段時期被稱為過渡期(grace period),默認七天的超越。過渡期可以用秒鍾、分鍾、小時、天數、周數、或月數表示。
如果以上值中的任何一個被設置為 0,那個限度就不會被設置。
注釋:
-u :編輯 user 的 quota
-g :編輯 group 的 quota
-t :編輯寬限時間(就是超過 quota 值后,還能使用硬盤的寬限期限)
-p :copy 模板(以建立好的使用者或群組)到另一個使用者(或群組)
[root@uhome /]# quotaon -avu
打開磁盤配額監控進程,u是用戶g是組,這里我沒設置g參數
注釋:
-a :全部的 quota 設定都啟動(會自動去尋找 /etc/mtab 的設定)
-u :使用者的 quota 啟動
-g :群組的 quota 設定啟動
-v :顯示訊息
/dev/sdb5 [/mnt/ftp]: user quotas turned on
[root@uhome /]# quota -uvs hom
要校驗用戶的配額是否被設置
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb5 16 0 1024 4 0 0
注釋:
-g :顯示 group 群組
-u :顯示 user
-v :顯示 quota 的值
-s :選擇 inod 或 硬盤空間來顯示
[root@uhome /]# edquota –t
設置過渡期(grace period),只針對軟限制而言
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem Block grace period Inode grace period
/dev/sdb5 7days 7days
查看用戶配額
*** Report for user quotas on device /dev/sdb5
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 35880 0 0 5 0 0
hom -- 16 0 1024 4 0 0
注釋:
-a 列出在/etc/fstab文件里,有加入quota設置的分區的使用狀況與用戶和群組
-g 列出所有群組的磁盤空間限制
-u 列出所有用戶的磁盤空間限制
-v 顯示該用戶或群組的所有空間限制
測試用戶配額是否生效
下面利用LeapFTP做測試,上傳一pixviewer.rar,傳輸到1M就失敗了.說明生效了

[root@uhome /]# quotaoff -vug /dev/sdb5
關閉/mnt/ftp分區的磁盤限額
/dev/sdb5 [/mnt/ftp]: user quotas turned off
注釋:
-a :全部的 quota 設定都關閉(會自動去尋找 /etc/mtab 的設定)
最后並刪除/etc/fstab中設置配額的部分
總結:在對用戶進行磁盤限額時,需要掌握以下幾點原則:
A.由於對用戶進行文件數量的限制不是很實用,所以通常進行磁盤配額只限制用戶占用的磁盤容量。
B.為用戶設置的軟限制和硬限制的數值都不應該小於用戶已占用的磁盤容量或文件數量,否則可能造成用戶無法正常登錄和使用系統。
C.設置的硬限制數量應該大於軟限制數量,否則沒有實際的意義
D././boot/./proc./mnt/cdrom 等不要使用配額
E.quota 實際在運作的時候,是針對整個分區進行限制的,例如:如果你的 /dev/sdb5 是掛載在/mnt/ftp底下,那么在 /mnt/ftp底下的所有目錄都會受到限制!
來源: http://viong.blog.51cto.com/844766/269485