tftp-server(含 tftp 客戶端)、xinetdyum install -y tftp-server xinetd
/etc/xinetd.d/tftp# 預設內容通常 disable=yes,需改為 no
cat > /etc/xinetd.d/tftp <<'EOF'
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot # -s 啟用安全模式,限制根目錄
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
EOF
關鍵參數說明:
| 參數 | 說明 |
|---|---|
server_args = -s /path | -s (secure) 啟用 chroot,限制僅能存取指定目錄下檔案(安全必開) |
wait = yes | UDP 服務單執行緒模式,xinetd 啟動一個 in.tftpd 處理所有封包 |
user = root | 以 root 權限啟動(需讀取 TFTP 根目錄檔案) |
per_source | 單一 IP 最大並發連線數 |
cps | 每秒最大連線數 / 達標後暫停毫秒數(防洪) |
# 建立目錄(對應 server_args -s 參數)
mkdir -p /var/lib/tftpboot
chmod 755 /var/lib/tftpboot
# 放置測試檔案
echo "TFTP test file" > /var/lib/tftpboot/test.txt
# 或複製啟動檔案:cp /boot/vmlinuz-* /var/lib/tftpboot/
systemctl start xinetd
systemctl enable xinetd
# 檢查 UDP 69 埠
netstat -anpu | grep :69
# 預期輸出:
# udp 0 0 0.0.0.0:69 0.0.0.0:* /xinetd
# 或使用 ss
ss -ulnp | grep :69
tftp 192.168.1.100
tftp> get test.txt
tftp> quit
# 檢查當前目錄下載的 test.txt 內容
tftp -g -r test.txt 192.168.1.100
# -g: get (下載),-r: 遠端檔名
# Windows 內建 tftp.exe(需啟用「TFTP 用戶端」功能)
tftp -i 192.168.1.100 GET test.txt
# -i: 二進制模式(避免文字轉換損壞二進制檔)
| 現象 | 原因 | 解決 |
|---|---|---|
| 客戶端「Transfer timeout」/ 無回應 | 防火牆阻擋 UDP 69 / xinetd 未啟動 / server_args 路徑錯誤 | 開放 UDP 69(firewall-cmd --add-port=69/udp)、重啟 xinetd、核對路徑 |
| 「Access violation」/「File not found」 | 檔案不在 -s 指定根目錄下 / 檔案權限不足 / SELinux 阻擋 | 確認檔案在 /var/lib/tftpboot、chmod 644、restorecon -R |
| 上傳失敗 | 預設唯讀 / 目錄無寫入權限 / 缺少 -c 參數 | TFTP 通常僅供下載;如需上傳,server_args 加 -c 並確保目錄可寫 |
| 大檔案傳輸失敗 | TFTP 區塊編號 16-bit 限制(最大 32MB),無窗口機制 | 大檔案改用 FTP/HTTP/NFS;或啟用 blocksize 選項(需雙端支援) |
# 檢查 TFTP 相關布林值
getsebool -a | grep tftp
# tftp_anon_write --> off
# 若需允許上傳
setsebool -P tftp_anon_write=1
# 恢復檔案上下文
restorecon -R /var/lib/tftpboot
# 1. TFTP 根目錄結構
/var/lib/tftpboot/
├── pxelinux.0 # Syslinux 引導載入器
├── pxelinux.cfg/
│ └── default # 啟動選單配置
├── vmlinuz # 核心映像
└── initrd.img # 初始化 ramdisk
# 2. DHCP 伺服器配置 (dhcpd.conf)
subnet 192.168.1.0 netmask 255.255.255.0 {
next-server 192.168.1.100; # TFTP 伺服器 IP
filename "pxelinux.0"; # 引導檔案
# ... 其他選項
}
-s 參數(chroot 限制根目錄)/var/log/messages 或 journalctl -u xinetd),異常存取告警TFTP 雖簡單,但在 PXE 啟動、網路設備管理等場景中不可替代。掌握 xinetd 管理模式、安全模式(-s)、防火牆/SELinux 配置與客戶端測試,即可快速部署穩定可用的 TFTP 服務。