天天看點

linux下架設FTP伺服器(vsftpd)

這兩天在LINUX下架設了FTP伺服器,具體步驟和大家分享一下

我的LINUX發行版本是fedora14, FTP采用vsftpd RPM包形式!

1、安裝RPM包

#yum install vsftpd

這樣安裝比較友善,不用使用者太多的配置

在安好該服務之後,使用者可以通過修改/etc/rc.d來設定讓其是否開機自動運作

也可以通過setup 設定系統服務

2、現在我們看一下vsftpd的配置檔案,其存儲在/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

#是否允許匿名使用者登陸FTP 伺服器

# Uncomment this to allow local users to log in.

local_enable=YES

#是否允許本地使用者登陸FTP 伺服器,其能正常登陸還需要看pam的配置情況

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

write_enable=YES

#僅僅表示vsftpd接受寫指令,能為操作成功還需要看權限設定情況,但設為NO之後,用戶端将不能上傳檔案

# 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

#使用者進入目錄後的提示資訊,儲存在.messages方件中

# Activate logging of uploads/downloads.

xferlog_enable=YES

#啟用日志功能,其預設的存儲路徑為:/var/log/vsftpd.log,後面有專門的設定參數

# Make sure PORT transfer connections originate from port 20 (ftp-data).

connect_from_port_20=YES

#是否一值使用20,

# 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 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.

# Note that the default log file location is /var/log/xferlog in this case.

xferlog_std_format=YES

#日志采用标準模式

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

#idle_session_timeout=600

#如果在600秒内沒有請求,則斷開,目的是為了減輕伺服器的負擔

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

#data_connection_timeout=120

#資料連接配接的逾時值為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

#處于非特權狀态,預設是nobody

# 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

#表示上傳和下載下傳允許ASCII模式,容易被DOS攻擊,預設是NO

# 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)

#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

#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

# 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

#以獨立方式啟用,但ipv4,ipv6不能在同一設定中同時設為yes

pam_service_name=vsftpd

#pam 認證  /etc/pam.d/

userlist_enable=YES

#表示拒拒絕在以下檔案中的使用者

tcp_wrappers=YES

3、上面是vsftpd伺服器的配置檔案,下面我們來具體的操作

3.1首先是匿名使用者的設定:

在安好之後,匿名使用者預設是允許登陸的,其取決于配置檔案中的:

#anonymous_enable=YES

但是匿名使用者預設情況下沒有上傳功能的。下面修改配置檔案中的

#write_enable=YES       //增加寫功能

#anon_upload_enable =YES  //允許上傳

這樣在配置檔案中已經是充許匿名使用者上傳檔案了,為什麼還是不能呢,則肯定是使用者權限不足,是以需要修改使用者權限,假如有一檔案夾upload,我們用以下指令使其享有權限

#chown  ftp.ftp upload  //此時一般況下,就可以上傳,但有些地方安全級别過高,由于SELINUX的阻止,不能上傳,這時使用者隻需要修改自己的SELINUX的設定就可以了

匿名使用者可以上傳檔案了,但是一般情況下,使用者不能删除檔案,如要開放其功能,則加入以下配置

#anon_other_write_enable=YES  充許另外的寫指令,包括删除指令

#anon_mkdir_write_enable=YES 充許建立子目錄(這些可以根據伺服器具體的用途開放相應的功能)

3.2虛拟使用者的配置

vsftpd的本地使用者本身是作業系統的使用者,除了可以登陸FTP伺服器之處,還可以登陸作業系統。而虛拟使用者則是FTP服務的專用使用者,虛拟使用者隻能通路FTP伺服器資源。對于隻需要通過FTP對系統有讀寫權限,而不需要其他系統資源的使用者或情況來說,采用虛拟使用者是最合适的

vsftpd的虛拟使用者彩用單獨的使用者名/密碼儲存方式,與作業系統帳号分開存放,可提高系統的安全性。vsftpd可以采用資料庫檔案來儲存使用者/密碼。下面我們簡單的配置一下

首先我們需要一個使用者認證資料庫我們可以使用如下指令

#vi ftpusr.txt

   其中的内容按行存儲,單行為使用者名,雙行為密碼

 如 lopn1 

       123456

       lopn2

       abcdef                               

# db_load -T -t hash -f ./ftpuser.txt /etc/vsftpd/ftpusr.db  生成ftpusr.db認證資料庫

#chmod 600 /etc/vsftpd/ftpuser.db

 注:進行此操作之前必須要確定本機db環境以安裝,可以使用

# rpm -qa | grep db4

或#yum install db4

之後建立pam認證檔案

#vi /etc/pam.d/vsftpd_login

錄入以下兩行内容

auth required /lib/security/pam_userdb.so db = /etc/vsftpd/ftpusr.db

account required /lib/security/pam_userdb.so db = /etc/vsftpd/ftpusr.db

然後為虛拟使用者建立一個本地帳号

# useradd -d /home/ftpsite =s /sbin/nologin ftp_virt  此含義為:建立一個ftp_virt帳号,主目錄為/home/ftpsite 不能登陸系統

修改配置檔案:

# guest_enable = YES   表示啟用虛拟使用者帳号

# guest_username = ftp_virt  表示虛拟使用者賬号對應的系統帳号

#pam_service_name=vsftpd_login

#此時系統使用者不能登陸,因為認證檔案隻認證虛拟使用者

此時虛拟使用者就可以登陸了!

 3.3虛拟使用者權限設定

#virtual_use_local_privs = YES  使虛拟使用者的權限由本地使用者的權限來決定

我們也可以為虛拟使用者分别設定權限,此處隻做簡單的介紹,我們為每個虛拟設定不同的主目錄

在配置檔案中加入

  user_config_dir = /etc/vsftpd   使用者的認證配置

#cd /home/ftpsite

#mkdir lopn1

#mkdir lopn2

#chown ftp_virt lopn1

#chown ftp_virt lopn2

#vi /etc/vsftpd/usr1  

錄入: local_root = /home/ftpsite/lopn1

#vi /etc/vsftpd/usr2

錄入:local_root = /home/ftpsite/lopn2

繼續閱讀