前言
簡單來說,我們在 Linux 上面架設 tftp server,方便於相關設備 (例如 Switch、Router) 將「設定檔備份上傳、設定檔下載還原」等動作。
實作環境
- CentOS 6.2 (Linux 2.6.32-71.el6.i686)
- tftp server: tftp-server-0.49-7.el6.i686
- tftp client: tftp-0.49-7.el6.i686
安裝及設定
步驟1.安裝 tftp、tftp-server 套件
安裝 tftp (tftp client) 以及 tftp-server 套件。
#yum -y install tftp tftp-server
步驟2.修改 tftp-server 設定檔
接著修改 tftp-server 設定檔 (/etc/xinetd.d/tftp) 內容,其中要確認的部份有二分別是 disable = no 以及 server_args 內容:
#cat /etc/xinetd.d/tftpservice tftp{disable = no //確認 tftp 設定為啟動socket_type = dgramprotocol = udpwait = yesuser = rootserver = /usr/sbin/in.tftpdserver_args = -s /var/lib/tftpboot -c //指定 TFTP 儲存路徑 (-c 允許上傳)per_source = 11cps = 100 2flags = IPv4}
雖然在設定檔中已經加上 -c 參數允許上傳,但是資料夾預設權限並沒有允許 w(write) 的權限,所以仍要設定資料夾權限,否則屆時上傳檔案時會得到 Error code 0: Permission denied 錯誤。
#chmod -R 777 /var/lib/tftpboot
步驟3.啟動 tftp-server 服務
由於 tftp 服務是由 xinetd 服務所帶起的,所以屆時啟動服務是要啟動 xinetd 才行。
#chkconfig tftp on //設定 tftp 開機自動啟動#chkconfig xinetd on //設定 xinetd 開機自動啟動#chkconfig --list | grep "xinetd\|tftp" //確認服務啟動狀態xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:offtftp: on#service xinetd start //啟動 xinetd 服務#netstat -tunpl | grep ':69' //確認 Service Port 有開啟udp 0 0 0.0.0.0:69 0.0.0.0:* 3263/xinetd
服務啟動後記得開啟 IPtables (UDP Port 69),如果儲存路徑不是在預設的 /var/lib/tftpboot 的話,有開啟 SELinux 安全機制的記得要執行 restorecon (否則屆時會發生 Error code 0: Permission denied 錯誤)。
測試 tftp server 上傳及下載功能
下列測試指令當中 tftp server IP 為 192.168.1.101 其中參數 get 為下載檔案而 put 為上傳檔案。
從 tftp server 中下載一個名為 test111 的檔案
#tftp -v 192.168.1.101 -c get testConnected to 192.168.1.101 (192.168.1.101), port 69getting from 192.168.1.101:test111 to test111 [netascii]
從 tftp client 中上傳一個名為 test222 的檔案
#tftp -v 192.168.1.101 -c put test222Connected to 192.168.1.101 (192.168.1.101), port 69putting test222 to 192.168.1.101:test222 [netascii]Sent 48574752 bytes in 5.0 seconds [78093609 bit/s]
參考
[CentOS 6 Forums -TFTP Timeout Occurred while setting up PXE]
Me FAQ
Q.可以從 tftp server 上下載檔案,但是無法上傳 Error code 1: File not found?
Error Meaage:
可以從 tftp server 上下載檔案,但是當要上傳檔案時發生如下錯誤訊息?
#tftp -v 192.168.1.101 -c put test222Connected to 192.168.1.101 (192.168.1.101), port 69putting test222 to 192.168.1.101:test222 [netascii]Error code 1: File not found
Ans:
請在 tftp-server 設定檔 (/etc/xinetd.d/tftp) 中 server_args 加上 -c 參數,以允許檔案上傳。
#grep server_args /etc/xinetd.d/tftpserver_args = -s /var/lib/tftpboot -c //加上 -c 參數以允許檔案上傳
Q.可以從 tftp server 上下載檔案,但是無法上傳 Error code 0: Permission denied?
Error Meaage:
可以從 tftp server 上下載檔案,但是當要上傳檔案時發生如下錯誤訊息?
#tftp -v 192.168.1.101 -c put test222Connected to 192.168.1.101 (192.168.1.101), port 69putting test222 to 192.168.1.101:test222 [netascii]Error code 0: Permission denied
Ans:
因為 /var/lib/tftpboot 資料夾預設權限為 755,而 Switch 預設上傳時會使用 nobody 這個帳號進行上傳的動作所導致,另外如果您有開啟 SELinux 機制的話請記得執行 restorcon 指令。
#chmod -R 777 /var/lib/tftpboot //修改資料夾權限#tftp -v 192.168.1.101 -c put test222 //上傳檔案成功Connected to 192.168.1.101 (192.168.1.101), port 69putting test222 to 192.168.1.101:test222 [netascii]Sent 48574752 bytes in 5.0 seconds [78093609 bit/s]