TFTP (Trivial FTP) 是無驗證、基於 UDP 69 埠的簡易檔案傳輸協議,常用於網路設備韌體更新、無盤啟動、嵌入式開發。Ubuntu 推薦使用功能增強版 tftpd-hpa 搭配 xinetd 超級伺服器運行。
sudo apt-get update
sudo apt-get install tftp-hpa tftpd-hpa xinetd
sudo vi /etc/xinetd.d/tftp
內容如下:
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot -c
per_source = 11
cps = 100 2
}
參數說明: -s /tftpboot 指定根目錄(chroot),-c 允許建立新檔案(上傳)。
sudo mkdir /tftpboot
sudo chmod 777 /tftpboot -R
sudo vi /etc/default/tftpd-hpa
修改為:
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -c -s"
TFTP_DIRECTORY 必須與 xinetd server_args 的 -s 參數一致-l 監聽所有介面,-c 允許建檔,-s 啟用 chroot 安全sudo /etc/init.d/xinetd reload
sudo /etc/init.d/xinetd restart
檢查埠口監聽:
netstat -a | grep tftp
# 應顯示:udp 0 0 *:tftp *:*
# 伺服器端建立測試檔
echo "test content" > /tftpboot/test.txt
# 客戶端測試下載
cd /tmp
tftp 192.168.123.202
tftp> get test.txt
tftp> quit
# 檢查內容
cat test.txt

| 錯誤 | 原因 | 解決 |
|---|---|---|
| Error code 0: Permission denied | 目錄/檔案權限不足 | sudo chmod 777 -R /tftpboot |
| Error code 1: File not found | 1.檔案不存在 2.缺少 -c 參數 3.目錄不一致(預設 /var/lib/tftpboot) | 1.確認檔案存在 2.檢查 TFTP_OPTIONS 含 -c 3.統一目錄至 /tftpboot 並 重啟機器 生效 |
| 上傳失敗 | 未加 -c 或目錄不可寫 | 同上,確認 server_args 與 TFTP_OPTIONS 都有 -c |
tftp-hpa tftpd-hpa xinetd/etc/xinetd.d/tftp,server_args = -s /tftpboot -c/etc/default/tftpd-hpa,TFTP_DIRECTORY="/tftpboot"、TFTP_OPTIONS="-l -c -s"mkdir /tftpboot && chmod 777 /tftpbootxinetd(reload + restart)get/put,若仍報錯 → 重啟主機