TFTP (Trivial File Transfer Protocol) was first described in 1980. It is a fairly old protocol published in June 1981 as TFTP Protocol revision 2 in RFC 783 (Request For Comments) by Karen R. Sollins.

In the early days, the main goal of TFTP was to send and receive files over a network. In particular, it was used to transfer the files needed during boot to enable systems to boot over a network.

Here's how you can set up a TFTP server on a Linux machine.

What Is TFTP?

TFTP is still used for file transfer purposes and there is no fundamental change in the features it supports. TFTP is used to download and send files over UDP/IP. It has no additional functions such as identity and authorization control, file listing, deletion, or renaming, which are usually found in other file transfer protocols.

Unlike the advanced file transfer protocols that use TCP in the transmission layer, it works on the UDP protocol and doesn't have features such as checking whether or not the packets belonging to the file are going to the other side. Because of this limitation, it is more suitable for use in local networks rather than the internet or wide area networks.

Despite all these seemingly negative features listed above, one aspect of the TFTP protocol that is very strong is its simplicity. The implementation of the protocol is quite easy compared to its alternatives, even for environments that do not have an operating system on them. Due to this feature, it has a wide usage area in embedded systems.

Installing a TFTP Server on Linux

When working with embedded devices, it's important to have the TFTP server service installed. On Linux systems, several TFTP server implementations can run. If you're using a Debian-based distribution, you can install the tftpd-hpa, tftpd, or atftpd packages. If you're not sure which one to choose, consider installing the tftpd-hpa package.

        sudo apt-get install tftpd-hpa
tftpd-hpa-install-debian-distro

After installation, the TFTP service will start listening on the UDP port 69. To serve the files to other systems via the TFTP server, you need to keep in mind a few prerequisites:

  • Copying the required file to the TFTP home directory or a directory below that home directory
  • Making file permissions visible to the public

To find out what the TFTP server home directory is, you can look at the TFTP_DIRECTORY variable in the /etc/default/tftpd-hpa file. Usually, you'll see directories like /var/lib/tftpboot or /srv/tftp. If you want, you can change this directory and restart the service.

        cat /etc/default/tftpd-hpa
tftpd-hpa-content-for-directory-select

For ease of use, if you change the owner of the relevant TFTP home directory to your user account, you won't need to add the sudo prefix to every command that you run. Use the chown command to change the ownership from root to the current user:

        sudo chown -R $USER /srv/tftp

The TFTP server package names and default home directories may differ depending on the Linux distribution used.

Sending Files With the TFTP Server

Sometimes there are situations where TFTP is the only option to move a file from your embedded Linux system to the external environment. For example, sometimes the system might not support any writable media using which you can transfer the file.

In such cases, since the TFTP client will probably be compiled in busybox, you can send a file saved in the system to a TFTP server on a network.

To use the TFTP client application, issue the busybox tftp command:

        busybox tftp                                                                                

To send a sample file to the TFTP server, you need to use a command like this:

        busybox tftp -l example.bin -p 192.168.1.100

Although the above command is correct, you will get an error while transferring the file to your TFTP server. Since the error message returned is not self-explanatory, it is difficult to understand what the real problem is.

The problem here is because of some security procedures on the TFTP server. TFTP requires that a file with the same name should be in the directory where the file will be written as a prerequisite for a file upload and that the write access for this file should be available to everyone.

In other words, it is not possible to upload a file that does not exist on the TFTP server via TFTP clients. If you create an empty file with the same name and edit its access rights, the above upload process will be successful. For this, you must run the following commands in the relevant TFTP server home directory:

        cd /srv/tftp 
touch example.bin
chmod 666 example.bin

Now you can perform your upload successfully.

It is also possible to disable the above security measure and have the TFTP server create a file that doesn't exist. For this, you can use the -c or --create parameter when starting the tftpd-hpa application. It is sufficient to add this parameter to the existing TFTPD_OPTIONS variable in the /etc/default/tftpd-hpa file:

        # /etc/default/tftpd-hpa 

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/srv/tftp"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="--secure --create"

Why Use a TFTP Server for File Transfer?

The most important advantage of TFTP is that it is fast, and helps you save time. It is an ideal option for transferring configuration files of network devices to other systems. Moreover, it has very simple usage criteria. It functions comfortably with software on both Windows and Linux-based operating systems. Finally, TFTP is always there to save the day in situations where you can't technically use FTP.

The biggest disadvantage is, of course, that it is not safe. Therefore, you must take great care while transferring files using a TFTP server.

Apart from file transfers, you cannot perform functions such as file deletion, editing, and modification using a TFTP server. This feature is a major disadvantage for those who use or seek advanced systems. Finally, it does not require authentication, which is a major drawback if you're serious about your security.

Setting Up TFTP on Other Operating Systems

If you are going to use TFTP on Windows, you do not need to install any third-party software. You can enable TFTP with the Turn Windows features on or off option in the control panel.