Linux tftp Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux tftp (Trivial File Transfer Protocol) command and learn how to configure a tftp server to transfer files between a client and a server. We will start by understanding the basic usage of the tftp command, including its various options and commands. Then, we will proceed to set up a tftp server and use it to transfer files. This lab aims to provide a practical understanding of the tftp protocol and its application in network file transfers.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") subgraph Lab Skills linux/apt -.-> lab-422956{{"`Linux tftp Command with Practical Examples`"}} linux/mkdir -.-> lab-422956{{"`Linux tftp Command with Practical Examples`"}} linux/chown -.-> lab-422956{{"`Linux tftp Command with Practical Examples`"}} end

Understanding the tftp Command

In this step, we will explore the tftp (Trivial File Transfer Protocol) command in Linux. The tftp command is a simple file transfer protocol that allows you to transfer files between a client and a server over a network.

First, let's check the version of the tftp command installed on your system:

$ tftp --version
tftp-hpa version 5.2

The tftp command has several options and commands that you can use to interact with a tftp server. Let's explore some of the most common ones:

$ tftp
tftp> ?
Commands:
 connect     connect to remote tftp
 mode        set transfer mode
 put         send file
 get         receive file
 quit        exit tftp
 verbose     toggle verbose mode
 trace       toggle packet tracing
 status      show current status
 binary      set mode to octet
 ascii       set mode to netascii
 rexmt       set per-packet retransmission timeout
 timeout     set total retransmission timeout
 ? or help   print help information

As you can see, the tftp command supports various commands such as connect, put, get, quit, and more. Let's try a simple file transfer using the get command:

tftp> connect 192.168.1.100
tftp> get example.txt
Received 123 bytes in 0.0 seconds

In the example above, we first connected to a tftp server at the IP address 192.168.1.100, and then used the get command to download the example.txt file from the server.

You can also use the put command to upload a file to the tftp server:

tftp> put myfile.txt
Sent 456 bytes in 0.0 seconds

The tftp command is a simple and lightweight file transfer protocol, but it lacks some of the advanced features of other file transfer protocols like FTP or SFTP. In the next step, we'll learn how to configure a tftp server and use it to transfer files.

Configuring the tftp Server

In this step, we will configure a tftp server on our Ubuntu 22.04 Docker container.

First, let's install the tftp server package:

$ sudo apt-get update
$ sudo apt-get install -y tftpd-hpa

Once the installation is complete, we need to configure the tftp server. The configuration file is located at /etc/default/tftpd-hpa.

Open the configuration file using the nano editor:

$ sudo nano /etc/default/tftpd-hpa

In the configuration file, you can set the following options:

  • TFTP_USERNAME: The user that the tftp server will run as. We'll use the labex user.
  • TFTP_DIRECTORY: The directory where the tftp server will look for files to serve. We'll use the /srv/tftp directory.
  • TFTP_ADDRESS: The IP address and port the tftp server will listen on. We'll use the default 0.0.0.0:69.
  • TFTP_OPTIONS: Additional options for the tftp server. We'll use the default options.

Update the configuration file to match the following:

TFTP_USERNAME="labex"
TFTP_DIRECTORY="/srv/tftp"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-l -c -s"

Save and close the file.

Next, create the /srv/tftp directory and set the appropriate permissions:

$ sudo mkdir -p /srv/tftp
$ sudo chown -R labex:labex /srv/tftp

Finally, start the tftp server:

$ sudo systemctl start tftpd-hpa

Your tftp server is now configured and running. In the next step, we'll learn how to transfer files using the tftp client.

Transferring Files Using the tftp Client

In this step, we will learn how to use the tftp client to transfer files to and from the tftp server we configured in the previous step.

First, let's create a sample file to transfer:

$ echo "This is a test file." > ~/project/test.txt

Now, let's use the tftp client to upload the test.txt file to the tftp server:

$ tftp 192.168.1.100
tftp> put ~/project/test.txt
Sent 21 bytes in 0.0 seconds

In the example above, we first connected to the tftp server at the IP address 192.168.1.100, and then used the put command to upload the test.txt file from the local ~/project directory to the tftp server.

You can also use the get command to download a file from the tftp server:

tftp> get example.txt ~/project/downloaded.txt
Received 123 bytes in 0.0 seconds

In this example, we downloaded the example.txt file from the tftp server and saved it as downloaded.txt in the local ~/project directory.

The tftp client also supports other commands, such as mode to set the transfer mode (binary or ASCII), verbose to enable verbose output, and quit to exit the tftp client.

Let's try one more example, this time using the binary mode to transfer a binary file:

tftp> binary
tftp> put ~/project/image.jpg
Sent 456789 bytes in 0.5 seconds

In this example, we first set the transfer mode to binary using the binary command, and then used the put command to upload the image.jpg file to the tftp server.

That's it! You now know how to use the tftp client to transfer files to and from a tftp server. In the next step, we'll review what we've learned in this lab.

Summary

In this lab, we first explored the tftp (Trivial File Transfer Protocol) command in Linux, understanding its various options and commands such as connect, put, get, and quit. We learned how to use the get and put commands to download and upload files from and to a tftp server. Next, we configured a tftp server on an Ubuntu 22.04 Docker container, installed the necessary packages, and set up the server to allow file transfers. Finally, we demonstrated how to transfer files using the tftp client, both uploading and downloading files between the client and the server.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like