Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 7 安裝與配置 vsftpd FTP 服務完整教學

編輯:Linux 教學

前言

vsftpd(Very Secure FTP Daemon)是 Linux 系統中最廣泛使用的 FTP 伺服器軟體,以輕量、高效且安全性高著稱。本文將詳細說明在 CentOS 系統上安裝、配置與管理 vsftpd 的完整流程,包含防火牆設定、用戶權限控制與常見問題排解。

什麼是 vsftpd?

vsftpd 全名為「Very Secure FTP Daemon」,由 Chris Evans 開發,安全性是其核心設計目標。它支援 IPv6、SSL/TLS 加密傳輸、虛擬用戶映射等進階功能,是架設 FTP 服務的首選方案。

安裝 vsftpd

1. 使用 yum 安裝

yum install vsftpd -y

2. 設定開機自動啟動

systemctl enable vsftpd

註:CentOS 7 以上請使用 systemctl,舊版才用 chkconfig。

3. 啟動服務

systemctl start vsftpd

服務管理常用指令

  • 停止服務:systemctl stop vsftpd
  • 重啟服務:systemctl restart vsftpd
  • 查看狀態:systemctl status vsftpd

配置防火牆開放 FTP 端口

FTP 預設使用 TCP 21 端口,需在防火牆開放:

firewalld(CentOS 7+)

firewall-cmd --permanent --add-service=ftp
firewall-cmd --permanent --add-port=21/tcp
firewall-cmd --reload

iptables(舊版)

vi /etc/sysconfig/iptables
# 在 REJECT 行之前添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
# 重啟防火牆
service iptables restart

核心配置文件說明

主配置文件位於 /etc/vsftpd/vsftpd.conf,建議備份後編輯:

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak
vi /etc/vsftpd/vsftpd.conf

關鍵安全設定

# 禁止匿名登入
anonymous_enable=NO
# 允許本地用戶登入
local_enable=YES
# 允許寫入操作
write_enable=YES
# 限制用戶在自己的家目錄(chroot)
chroot_local_user=YES
# 允許 chroot 列表例外
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
# 被動模式端口範圍(建議設定)
pasv_min_port=40000
pasv_max_port=40100

建立 FTP 專用用戶並限制權限

1. 建立用戶群組與目錄

groupadd ftpusers
mkdir -p /home/ftpdata/user1
chown -R user1:ftpusers /home/ftpdata/user1
chmod 755 /home/ftpdata/user1

2. 新增用戶並禁止 Shell 登入

useradd -d /home/ftpdata/user1 -g ftpusers -s /sbin/nologin user1
passwd user1

3. 配置 chroot 例外清單

vi /etc/vsftpd/chroot_list
# 內容每行一個用戶名,例如:
user1
user2

4. 重啟服務生效

systemctl restart vsftpd

常見問題排解

錯誤:500 OOPS: cannot change directory

原因:SELinux 限制 FTP 存取家目錄。

解決方法

# 方法一:開啟 FTP 家目錄存取布林值
setsebool -P ftp_home_dir on
# 方法二:關閉 FTP SELinux 保護(較不安全)
setsebool -P ftpd_disable_trans 1
systemctl restart vsftpd

被動模式連線失敗

需在防火牆額外開放被動模式端口範圍(如 40000-40100),並在 vsftpd.conf 設定對應的 pasv_min_port/pasv_max_port。

驗證測試

使用 FTP 客戶端(FileZilla、WinSCP 或命令行 ftp)連線測試:

ftp your_server_ip
# 輸入用戶名密碼後,嘗試上傳/下載檔案

總結

vsftpd 配合 chroot 與 SELinux 設定,能提供安全且易管理的 FTP 服務。生產環境建議啟用 SSL/TLS 加密傳輸(需額外配置 ssl_enable=YES 並指定憑證),並定期檢查日誌 /var/log/vsftpd.log 監控異常登入。

Copyright © Windows教程網 All Rights Reserved