天天看點

vsftpd安裝,配置虛拟使用者

vsftpd安裝,配置虛拟使用者,問題總結

    • 準備工作
      • 檢查是否有安裝vsftpd
      • 檢查是否有db_load
      • 關閉SElinux
      • 下載下傳vsftpd( 這 是 我 使 用 的 版 本 v s f t p d − 3.0.2 − 25. e l 7. x 86 _ 64. r p m \color{red}{這是我使用的版本 vsftpd-3.0.2-25.el7.x86\_64.rpm} 這是我使用的版本vsftpd−3.0.2−25.el7.x86_64.rpm)并上傳到伺服器
    • 安裝vsftpd
    • 配置vsftpd
      • 建立一個宿主使用者,用于vsftpd虛拟使用者登入關聯映射
      • 生成虛拟使用者資料檔案
      • 配置虛拟使用者驗證檔案
      • 配置vsftpd
        • readwriteUser
        • readUser
      • 啟動
      • 測試
    • 問題總結

準備工作

檢查是否有安裝vsftpd

# 執行指令無輸出,表示沒有安裝
rpm -qa | grep vsftp
           

如果已經安裝,則不需要再重複安裝,到此結束。

檢查是否有db_load

# 執行指令有輸出,表示已安裝
db_load
           
vsftpd安裝,配置虛拟使用者

如果沒有安裝,則執行指令進行安裝

yum -y install db4
           

關閉SElinux

# 臨時關閉
setenforce 0
# 永久關閉
vi /etc/sysconfig/selinux
# SELINUX=enforcing 改為 SELINUX=disabled
           
vsftpd安裝,配置虛拟使用者

下載下傳vsftpd( 這 是 我 使 用 的 版 本 v s f t p d − 3.0.2 − 25. e l 7. x 86 _ 64. r p m \color{red}{這是我使用的版本 vsftpd-3.0.2-25.el7.x86\_64.rpm} 這是我使用的版本vsftpd−3.0.2−25.el7.x86_64.rpm)并上傳到伺服器

安裝vsftpd

# 注意需要将安裝包名稱替換成自己下載下傳的包名稱
rpm -ivh vsftpd-3.0.2-25.el7.x86_64.rpm
           
vsftpd安裝,配置虛拟使用者

如 果 安 裝 報 錯 \color{red}{如果安裝報錯} 如果安裝報錯

error: Failed dependencies:

xxxxxx is need by ${vsftpd包名}

則執行如下指令,進行安裝時不再分析包之間的依賴關系(自己試過不分析包的方式安裝,暫未發現問題)

rpm -ivh vsftpd-3.0.2-25.el7.x86_64.rpm --nodeps --force
           

配置vsftpd

建立一個宿主使用者,用于vsftpd虛拟使用者登入關聯映射

# 添加名為ftpuser的宿主使用者
useradd -g root -M -d /home/vsftpd -s /sbin/nologin ftpuser
# 給宿主使用者設定密碼
passwd ftpuser
mkdir /home/vsftpd
chown -R ftpuser.root /home/vsftpd
           

生成虛拟使用者資料檔案

  • 進入vsftpd配置檔案目錄
    cd /etc/vsftpd/
               
  • vi vuser_passwd

    添加虛拟使用者資料, 奇 數 行 為 用 戶 名 , 偶 數 行 為 用 戶 名 密 碼 \color{red}{奇數行為使用者名,偶數行為使用者名密碼} 奇數行為使用者名,偶數行為使用者名密碼
    readwriteUser
    123
    readUser
    123
               
    vsftpd安裝,配置虛拟使用者
  • 使用db_load生成使用者資料檔案
    db_load -T -t hash -f /etc/vsftpd/vuser_passwd /etc/vsftpd/vuser_passwd.db
               

配置虛拟使用者驗證檔案

  • 備份檔案/etc/pam.d/vsftpd, 注 意 : 文 件 名 可 能 不 叫 這 個 , 需 要 進 入 到 / e t c / p a m . d 目 錄 進 行 查 看 具 體 文 件 名 \color{red}{注意:檔案名可能不叫這個,需要進入到/etc/pam.d目錄進行檢視具體檔案名} 注意:檔案名可能不叫這個,需要進入到/etc/pam.d目錄進行檢視具體檔案名,因為在網上看到有叫vsftpd.vu的
    cp /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
               
  • vi /etc/pam.d/vsftpd

    清空vsftpd的内容,加如下内容
    #%PAM-1.0
    auth        required    pam_userdb.so   db=/etc/vsftpd/vuser_passwd
    account     required    pam_userdb.so   db=/etc/vsftpd/vuser_passwd
               
    說明:/etc/vsftpd/vuser_passwd路徑是剛才生成的使用者資料檔案去掉**.db**字尾後的路徑

配置vsftpd

  • 備份檔案/etc/vsftpd/vsftpd.conf
    cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
               
  • 清空/etc/vsftpd/vsftpd.conf的内容
    echo '' > vsftpd.conf
               
  • 添加如下内容
    local_enable=YES
    # 這裡的寫包括了執行指令,是以不能直接設定為NO
    write_enable=YES
    anon_umask=022
    # 不允許匿名登入
    anonymous_enable=NO
    # ** 啟用宿主使用者 **
    guest_enable=YES
    # ** 宿主使用者名 **
    guest_username=ftpuser
    # ** 剛才配置的虛拟使用者驗證檔案的名字 **
    pam_service_name=vsftpd
    # 将使用者限制在其主目錄下
    chroot_local_user=NO
    # 當使用者被限制在其主目錄下時,允許執行寫操作
    allow_writeable_chroot=YES
    chroot_list_enable=NO
    # ftp根目錄
    local_root=/data/ftp/
    # 日志檔案儲存路徑
    xferlog_file=/etc/vsftpd/logs/xferlog
    # 使用者單獨配置檔案存放目錄,該目錄下使用者的檔案名就是對應使用者名
    user_config_dir=/etc/vsftpd/vuser_conf
               
  • 如果需要對使用者進行特殊的配置,可在 vsftpd.conf中的user_config_dir對應的目錄, 建立一個和虛拟使用者名相同的檔案,進行單獨處理。

    由于我的虛拟使用者有readwriteUser和readUser

    分别進行配置

readwriteUser

  • 進入虛拟使用者配置檔案存放目錄
    cd /etc/vsftpd/vuser_conf
               
  • 建立readwriteUser檔案
    vi readwriteUser
               
  • 加入如下内容
    local_root=/data/ftp/
    anon_world_readable_only=NO
    chroot_local_user=YES
    write_enable=YES
    local_umask=022
    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
               

readUser

  • 建立readUser檔案
    vi readUser
               
  • 加入如下内容
    # 設定readUser的根目錄
    local_root=/data/ftp/aa
    anon_world_readable_only=NO
    local_umask=022
    # 不允許上傳
    anon_upload_enable=NO
    # 不允許建立檔案夾
    anon_mkdir_write_enable=NO
    # 不允許其他的寫操作
    anon_other_write_enable=NO
               

啟動

# 為什麼執行restart而不是start,因為restart的時候,沒有啟動會自己啟動,啟動了會停掉服務重新啟動
systemctl restart vsftpd.service
           

測試

  • 添加測試資料
    cd /data/ftp
    echo "this is index" > index.txt
    mkdir aa
    echo aa > aa/aa.txt
    mkdir aa/bb
    echo bb > aa/bb/bb.txt
    chown -R ftpuser /data/ftp
               
  • 使用readwriteUser登入并上傳檔案
    vsftpd安裝,配置虛拟使用者
  • 使用readUser登入并測試上傳檔案
    vsftpd安裝,配置虛拟使用者

問題總結

1、pam_service_name設定存在問題,因為/etc/pam.d/下面對應的檔案可能不是vsftpd.vu,而是vsftpd

2、500 OOPS: vsftpd: refusing to run with writable root inside chroot (),因為vsftpd的檢查機制,使得被限制使用者主目錄的使用者,不能擁有寫權限。 需 要 加 上 a l l o w _ w r i t e a b l e _ c h r o o t = Y E S \color{red}{需要加上allow\_writeable\_chroot=YES} 需要加上allow_writeable_chroot=YES

3、虛拟使用者登入後,出現空白頁。因為SELinux開啟的原因,是以需要關閉SELinux