TFTP(Trivial File Transfer Protocol,簡單檔案傳輸協定)是 TCP/IP 協定族中用於用戶端與伺服器之間進行簡易檔案傳輸的協定,使用 UDP 連接埠 69,開銷小、實作簡單。
做 ARM-Linux 嵌入式開發時,經常需要用 TFTP 在 Linux 主機與開發板之間傳輸核心與 rootfs,以下以 Ubuntu 為例說明完整配置。
需要三個:xinetd(服務管理器)、tftpd(伺服器端)、tftp(用戶端)。
sudo apt-get install xinetd tftpd tftp
也可以用 openbsd-inetd 取代 xinetd(Debian 5.0 之後即以 openbsd-inetd 管理):
sudo apt-get install openbsd-inetd tftpd tftp
若使用 xinetd,在 /etc/xinetd.d/ 下建立 tftp 檔案並寫入:
service tftp {
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
其中 /tftpboot 就是檔案服務目錄,之後不論上傳或下載都在此目錄進行。server_args 的 -s 代表把該目錄設為 root,客戶端看到的檔案樹以此為根,這也是較安全的做法。
若使用 openbsd-inetd,不需新建檔案,直接編輯 /etc/inetd.conf。其格式為:
<service_name> <sock_type> <proto> <flags> <user> <server_path> <args>
把原本的:
tftp dgram udp wait nobody /usr/sbin/tcpd /usr/sbin/in.tftpd /srv/tftp
改為:
tftp dgram udp wait root /usr/sbin/in.tftpd /usr/sbin/in.tftpd -s /tftpboot
sudo mkdir /tftpboot sudo chmod 777 /tftpboot -R
TFTP 沒有認證機制,因此 777 是為了讓 u-boot 能寫入檔案;這也意味著該服務只適合放在內部網路,切勿直接對公網開放。
sudo /etc/init.d/xinetd restart # 使用 xinetd sudo /etc/init.d/openbsd-inetd restart # 使用 openbsd-inetd
在 systemd 版本的系统上,也可以用 sudo systemctl restart xinetd。
在本機測試是否可讀取檔案:
tftp 127.0.0.1 tftp> get somefile tftp> quit
若是在板子上透過 U-Boot 內建的 tftp 指令與主機傳輸,只要接好網線並設定好 ipaddr、serverip、gateway、netmask 即可,例如:
set ipaddr 192.168.1.244 set serverip 192.168.1.93 tftp 30008000 uImage
注意 ipaddr 與 serverip 必須同一網段,且主機防火牆需放行 UDP 69;TFTP 使用 UDP,若主機端有 iptables 限制,可能還需允許由 tftp 伺服器回送的資料流。