Linux FTP Server自动化运维实践
一 架构与工具选型
二 自动化部署与配置管理
#!/usr/bin/env bash
set -e
FTP_USER=${FTP_USER:-ftpuser}
FTP_PASS=${FTP_PASS:-ChangeMeNow!}
FTP_HOME=/home/$FTP_USER
# 安装
yum install -y vsftpd && systemctl enable --now vsftpd
# 基线配置
cat >/etc/vsftpd/vsftpd.conf <<'EOF'
listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen_ipv6=NO
userlist_enable=YES
userlist_file=/etc/vsftpd/user_list
userlist_deny=NO
EOF
# 用户与权限
if ! id -u "$FTP_USER" >/dev/null 2>&1; then
useradd -m -d "$FTP_HOME" -s /sbin/nologin "$FTP_USER"
echo "$FTP_USER:$FTP_PASS" | chpasswd
fi
chown -R "$FTP_USER:$FTP_USER" "$FTP_HOME"
# 防火墙与SELinux
firewall-cmd --permanent --add-service=ftp && firewall-cmd --reload
setsebool -P ftp_home_dir on 2>/dev/null || true
systemctl restart vsftpd
echo "FTP deployed: user=$FTP_USER home=$FTP_HOME"
三 自动化传输与任务编排
# 每天 02:00 将本地目录反向镜像到远端(--reverse),删除远端多余文件
0 2 * * * lftp -e 'mirror --reverse --delete /local/path /remote/path; quit' -u user,pass ftp.example.com
#!/usr/bin/env bash
inotifywait -m /local/path -e create,modify,delete |
while read path action file; do
lftp -e 'mirror --reverse --delete /local/path /remote/path; quit' -u user,pass ftp.example.com
done
import ftplib
def upload():
with ftplib.FTP('ftp.example.com','user','pass') as ftp:
ftp.cwd('/remote')
with open('/local/file.txt','rb') as f:
ftp.storbinary('STOR file.txt', f)
[Unit]
Description=FTP Automation
After=network.target
[Service]
ExecStart=/usr/local/bin/ftp_sync.sh
Restart=always
User=ftpuser
[Install]
WantedBy=multi-user.target
四 监控 日志与告警
top/htop、vmstat 1 5、iostat -xz 1、df -h、ss -tulnpjournalctl -u vsftpd -f;vsftpd日志常见路径:/var/log/vsftpd.log 或 /var/log/syslog#!/usr/bin/env bash
if ! systemctl is-active --quiet vsftpd; then
echo "vsftpd DOWN" | mail -s "FTP Alert" ops@example.com
fi
if grep -i "error" /var/log/vsftpd.log >/dev/null 2>&1; then
echo "vsftpd ERROR found" | mail -s "FTP Error" ops@example.com
fi
五 安全与合规要点
ftp_home_dir)或调整策略以匹配业务。