1. Overview

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.

2. The sftp Command

The sftp command is part of the OpenSSH package. It’s our go-to tool for secure file transfers on Linux. Unlike regular FTPsftp 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:

  • options: are extra settings we can add to customize how sftp works
  • user@host: tells sftp where to connect. It’s made up of user – username on the remote computer and host that specifies the address of the remote computer

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.

3. Common sftp Command Options

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.

3.1. Connection and Authentication Options

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:

  • -P [port]: allows us to specify the port to connect to on the remote host if it uses a port other than the default port (22) for SSH connections
  • -i [identity_file]: is helpful when working with servers that require key-based authentication (a more secure way to log in than passwords)
  • -o [ssh_option]: gives us even more control over how sftp connects using SSH. We can use this option to set things like proxy commands, host key checking, and where to look for known host files

These options ensure we can use sftp to flexibly connect to different servers with different configurations and security requirements.

3.2. File Transfer Options

sftp also offers us several options to help tweak the actual file transfer process. Now, let’s see some of them:

  • -a: allows us to pick up from where we left off after an interruption
  • -p: comes in handy when we need to keep the exact timestamps (the last time we modified or accessed the file) and permissions of the files we’re transferring
  • -R [num_requests]: allows us to control how many file transfer requests that can happen at the same time
  • -b [batchfile]: is a time-saver when we need to transfer multiple files or execute a series of commands. Instead of typing each command manually, we can create a text file (batchfile) containing the commands we want to run, and sftp will execute them automatically
  • -l [limit]: is helpful in situations where we need to set a limit on how fast sftp can transfer data. We can specify the limit in Kbits/s (kilobytes per second). This way, we don’t have to worry about sftp using up all our bandwidth

With these options, we have control over how files are handled during transfer. This makes sftp adaptable to different needs and situations.

4. Common sftp Command Examples

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.

4.1. Connecting to an SFTP Server

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.

4.2. Transferring Files

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.

4.3. Managing Remote Files

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.

5. Conclusion

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.