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

Linux TFTP 服務快速部署指南:xinetd 管理、UDP 69 埠與網啟/無盤啟動應用

編輯:FTP 伺服器

TFTP 協議簡介

  • 全稱: Trivial File Transfer Protocol
  • 傳輸層: UDP(埠口 69)
  • 特點: 無認證、無目錄列表、無加密、極簡指令集(RRQ/WRQ/DATA/ACK/ERROR)
  • 典型應用: 網路啟動(PXE)、無盤工作站、網路設備固件/配置備份升級、嵌入式設備檔案傳輸

部署環境

  • RHEL / CentOS / Fedora / Rocky Linux / AlmaLinux
  • 套件:tftp-server(含 tftp 客戶端)、xinetd

安裝與配置

1. 安裝套件

yum install -y tftp-server xinetd

2. 修改 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 = yesUDP 服務單執行緒模式,xinetd 啟動一個 in.tftpd 處理所有封包
user = root以 root 權限啟動(需讀取 TFTP 根目錄檔案)
per_source單一 IP 最大並發連線數
cps每秒最大連線數 / 達標後暫停毫秒數(防洪)

3. 準備 TFTP 根目錄與測試檔案

# 建立目錄(對應 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/

4. 啟動 xinetd 服務

systemctl start xinetd
systemctl enable xinetd

5. 驗證服務監聽

# 檢查 UDP 69 埠
netstat -anpu | grep :69
# 預期輸出:
# udp  0  0  0.0.0.0:69  0.0.0.0:*  /xinetd

# 或使用 ss
ss -ulnp | grep :69

客戶端測試

1. 使用 tftp 客戶端(互動模式)

tftp 192.168.1.100
tftp> get test.txt
tftp> quit
# 檢查當前目錄下載的 test.txt 內容

2. 使用 tftp 非互動模式(腳本友善)

tftp -g -r test.txt 192.168.1.100
# -g: get (下載),-r: 遠端檔名

3. Windows 客戶端測試

# 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 選項(需雙端支援)

SELinux 處理

# 檢查 TFTP 相關布林值
getsebool -a | grep tftp
# tftp_anon_write --> off

# 若需允許上傳
setsebool -P tftp_anon_write=1

# 恢復檔案上下文
restorecon -R /var/lib/tftpboot

進階:PXE 網路啟動整合

# 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 限制根目錄)
  • 僅在受信任內網段開放 UDP 69,邊界防火牆封鎖
  • 定期清理 TFTP 根目錄舊檔案,防止磁碟佔滿
  • 重要設備配置備份建議改用 SFTP/SCP(加密、認證)
  • 監控 xinetd 日誌(/var/log/messages 或 journalctl -u xinetd),異常存取告警

TFTP 雖簡單,但在 PXE 啟動、網路設備管理等場景中不可替代。掌握 xinetd 管理模式、安全模式(-s)、防火牆/SELinux 配置與客戶端測試,即可快速部署穩定可用的 TFTP 服務。

Copyright © Windows教程網 All Rights Reserved