Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 18, 2024
The Secure File Transfer Protocol (SFTP) is a tool for safely transferring files over the Internet. The SFTP tool can be accessed on a Linux system using the sftp command. It allows us to manage files remotely.
In this tutorial, we’ll learn how to use the sftp command effectively. We’ll cover the basics of the command and common options you can use with it.
The sftp command is part of the OpenSSH package. It’s our go-to tool for secure file transfers on Linux. Unlike regular FTP, sftp uses the SSH protocol to encrypt the connection and the data we send. This helps to protect our files from prying eyes.
Now, let’s take a look at the basic way to use the command:
sftp [options] [user@]host
Let’s break this down:
When we connect to sftp, it opens an interactive session. Here, we can type commands to manage files on the remote system.
Beyond just transferring files, sftp allows us to perform various tasks on the remote machine. We can list files and directories, delete files, create new folders, and more.
To make the most of sftp, we should understand some of the commonly used options that can enhance our experience with the tool. We need to add these options before the destination address when running the sftp command.
The sftp command offers several options to control how we connect to remote servers and authenticate ourselves. Let’s learn about some of these options:
These options ensure we can use sftp to flexibly connect to different servers with different configurations and security requirements.
sftp also offers us several options to help tweak the actual file transfer process. Now, let’s see some of them:
With these options, we have control over how files are handled during transfer. This makes sftp adaptable to different needs and situations.
We’ve learned a few options we can use with sftp. Now, let’s see some of these options in action. These scenarios will cover connecting to a server, transferring files, and managing files on the remote system.
Connecting to an SFTP server is the first step to transferring or managing files remotely. Let’s look at a few ways we can establish a secure connection.
First, let’s make a basic connection using the sftp command and a destination:
$ sftp [email protected]
The above command initiates a connection to the SFTP server on the host, 191.22.235.149, using the username baeldung. We’ll typically be prompted to enter a password unless we’ve set up SSH keys for authentication.
Also, we can specify a non-default port when connecting to our SFTP server:
$ sftp -P 22222 [email protected]
Here, we used the -P option to specify a custom port. This is helpful when our server isn’t running on the default port 22.
In addition, when we don’t want to connect to our server using a password, we can use a private key:
$ sftp -i ~/.ssh/id_rsa [email protected]
This improves security and convenience. In the example above, we specify the path to our private key file (often stored in ~/.ssh/id_rsa) using the -i option. This eliminates the need to type our password each time we want to connect.
Transferring files with the sftp command is easy. It gets even better with options that help us adjust the process.
Let’s say we want to upload a large file, but our internet isn’t stable. We can use the -a option with sftp:
$ sftp -a [email protected]
sftp> put datafile.txt /remote/directory/remote_datafile.txt
With the -a option, if our internet connection breaks at any point, we don’t have to start the upload from scratch. Instead, sftp will pick up right where it left off.
In the interactive sftp session above, the put command uploads datafile.txt from our local machine to the /remote/directory/ on the server. Also, it renames the file to remote_datafile.txt on the server.
Furthermore, we can preserve file attributes such as timestamps and permissions from the original files in the transferred files using the -p option:
$ sftp -p [email protected]
sftp> put datafile.txt /remote/directory/remote_datafile.txt
With this option, our remote_datafile.txt file retains the attributes of the original datafile.txt file. This ensures consistency in the file attributes.
Next, if we have a fast internet connection, we can speed up transfers by allowing sftp to handle multiple file requests at the same time using the -R option:
$ sftp -R 10 [email protected] sftp> put datafile.txt /remote/directory/remote_datafile.txt
The -R option in this example tells sftp to allow up to 10 simultaneous requests. This will make our file transfer much faster.
Once connected to an SFTP server, we can easily manage files directly on the remote system. This can be quite helpful when we need to tidy things up, rename files, or delete unwanted files without downloading them first.
For example, we can change the directory using the cd command:
$ sftp [email protected]
sftp> cd /new/directory
This is like changing folders on our local computer. The cd command allows us to navigate to a different directory on the remote server. In the above example, we’re moving to a folder called /new/directory/.
Also, we can list files in a remote directory using ls:
$ sftp [email protected]
sftp> ls
Just like on our local machine, the ls command displays a list of all the files in the current or specified directory. This is helpful when we want to see what’s in a particular folder on the remote server.
In addition, when we need to get rid of files we no longer need on the server, the rm command comes to the rescue:
$ sftp [email protected]
sftp> rm remote_datafile.txt
In this case, the command deletes the file remote_datafile.txt. However, we have to be careful, as files deleted using sftp are gone for good.
Lastly, we can create a new directory using the mkdir command:
$ sftp [email protected]
sftp> mkdir new_directory
The command creates a new folder called new_directory on the server.
In this article, we explored the Linux sftp command. We began by establishing a secure connection with various authentication methods. This is to ensure the privacy and integrity of our data during transit.
Furthermore, we took a walk through the range of options available for customizing file transfers, including resuming interrupted transfers, preserving file attributes, and more. We also got our hands dirty with some practical examples to help us understand how to use these options.
By using the power of sftp, we can confidently transfer sensitive data, knowing it’s protected by a robust encryption system.