Linux下構建FTP服務器

ftp是什麼

FTP是 File Transfer Protocol 文件傳輸協議的英文名稱,用於在Internet上控制文件的雙向傳輸. 同時它也是一個應用程序.一般的Linux系統默認帶有ftp軟件或者是vsftpd。

搭建ftp服務器目的

最近Linux服務器端硬盤容量受限,文件也多了,需要遠程拷貝,找了一圈沒有合適的軟件,自己搞一個基於FTP服務協議的文件,讓他自動遠程同步,這樣也方便,順便搞個FTP服務器,對以後可以公開的資料放到FTP服務器上做共享。其實,我越來越發現,交流和共享能提升學習效率,事半功倍。

檢查安裝vsftpd軟件

查看所有的安裝的軟件包 並在結果中查找包含vsftp 的文件

rpm -qa | grep vsftpd

如果沒有裝則使用yum命令安裝

yum -y install vsftpd 

或者vsftpd有問題的可以重新裝

yum -y reinstall vsftpd 

具體步驟

1、創建用戶

useradd yangxi -s /sbin/nologin -d /home/yangxi #1、創建個普通用戶用於連接ftp,-d的家目錄是ftp連接上默認訪問的。這個禁止登錄,是禁止遠程連接服務器,不是禁止登錄ftp

最好是禁止登錄,這樣的話這個用戶可以只用作FTP服務。

或者通過下面的命令進行模式修改:

usermod -s /sbin/nologin ftpname //限定用戶ftpname不能使用telnet,只能使用ftp
usermod -s /sbin/bash ftpname //用戶test恢復正常
usermod -d /ftp ftpname //更改用戶ftpname的主目錄爲/ftp

2、配置 vsftpd.conf  配置文件

anonymous_enable=YES  #允許匿名用戶登錄,改成no

//在配置文件中添加下面這2行

local_root=/home/yangxi  //修改默認根目錄

chroot_local_user=YES //限制住了,只允許查看他自己的家目錄
其中這三個參數主要作用如下表

如果要對用戶做限制需要修改下面參數中的一個,如下:
userlist_enable=YES     #啓動用戶列表
userlist_deny=NO        #決定是否對用戶列表的用戶拒絕訪問ftp 

同時,還需要編輯相對應的用戶文件,將用戶名加入即可。

userlist_file=/etc/vsftpd/user_list

4、關閉SELINUX

臨時關閉

setenforce 0

永久關閉

vim /etc/selinux/config

SELINUX=disabled

3、重啓vsftpd服務

查看vsftpd 服務的運行狀態
systemctl status vsftpd
如果最後一條狀態是stoped則啓動vsftpd

systemctl start vsftpd 

如果最後一條狀態是started則重啓vsftpd

systemctl restart vsftpd 

經常會遇到的問題:

原vsftpd服務器的系統從centos6.8升級到centos7.2。vsftpd使用yum方式安裝,用戶採用系統用戶登錄。由於系統升級到centos7,yum安裝的vsftpd版本改變。因此按centos6的設置,登錄時報錯。配置文件未修改,主要是修改了ftp的主目錄權限。

 

這個問題發生在最新的這是由於下面的更新造成的:

- Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chroot_local_user but such is life.

從2.3.5之後,vsftpd增強了安全檢查,如果用戶被限定在了其主目錄下,則該用戶的主目錄不能再具有寫權限了!如果檢查發現還有寫權限,就會報該錯誤。

要修復這個錯誤,可以用命令chmod a-w /home/user去除用戶主目錄的寫權限(採用該方式,將用戶主目錄文件熟悉修改爲#chmod 555)。

FTP連接報錯530 Permission denied解決方法

故障排除:

1.首先檢查系統是否開啓了vsftp服務,如果沒有開啓,先開啓該服務。

2.查看配置

vsftpd的配置,配置文件中限定了vsftpd用戶連接控制配置。
vsftpd.ftpusers:位於/etc/vsftpd目錄下。它指定了哪些用戶賬戶不能訪問FTP服務器,例如root等。
vsftpd.user_list:位於/etc/vsftpd目錄下。該文件裏的用戶賬戶在默認情況下也不能訪問FTP服務器,僅當vsftpd .conf配置文件裏啓用userlist_enable=NO選項時才允許訪問。
vsftpd.conf:位於/etc/vsftpd目錄下。來自定義用戶登錄控制、用戶權限控制、超時設置、服務器功能選項、服務器性能選項、服務器響應消息等FTP服務器的配置。

3.配置修改完成後,執行service vsftpd restart重啓vsftpd服務。

500 OOPS: vsftpd: cannot locate user specified in 'ftp_username':ftp

500 OOPS: vsftpd: cannot locate user specified in 'ftp_username':ftp

的錯誤消息。我在google上搜了一把後來發現有一封mail非常有用:
http://lists.debian.org/debian-user/.../msg02648.html 

其中有一段話提到:

Yep, I did. There is no occurrence of the string "tunable" anywhere in the vsftpd.conf man page ...

However, I've been delving into the source code, and now understand this - the author uses the name of a config file directive *prefixed by* "tunable_" inside the source code as a variable name. So what I needed to do was define "ftp_username" in vsftpd.conf. For some reason the Debian package leaves a little to be desired in this area, and neither asks you how you want this directive set, nor creates an account for use with vsftpd anonymous connections. The default for this directive is the name "ftp", but there has never been an "ftp" account on my Woody system, nor was one created by the vsftpd installation.

作者提到收到一個500 Oops但是其中提到的tunable_ftp_username哪裏也找不到。後來他查看源碼,發現源碼中是使用一個以"tunable_"爲前綴的變量,實際上相應在vsftpd.conf中應該是ftp_username這個directive。所以他在vsftpd.conf中加入了ftp_username=xxx(用戶)這一行。但是他也抱怨Debian的發行版沒有在任何地方標識這樣的問題。而且這個ftp_username的缺省用戶應該是ftp,但是woody系統缺省安裝後並沒有這個ftp用戶存在,而且vsftpd安裝後也沒有添加這個用戶。

注:我是用ftp_username=nobody的,然後/etc/init.d/vsftpd restart後成功登陸。

 

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