How to Use SFTP (SSH File Transfer Protocol) (2024)

SSH

Mar 20, 2024

Edward S.

6min Read

FTP is the standard method of transferring files or other data between computers, but it’s becoming more and more outdated in today’s security-conscious environment. Fortunately, that’s where SFTP comes in, which is particularly useful for VPS hosting users.

In this guide, we’re going to show how to use SFTP for secure file transfer, talk about some other useful commands and elaborate more on how it works.

Download Ultimate SSH Commands Cheat Sheet

How to Connect Using SFTP?

SFTP is a subsystem of SSH. Hence, it supports all SSH authentication methods. While it’s easier to set up and use password authentication, it’s much more convenient and safer to create SSH keys for a passwordless SFTP login.

You can check this tutorial on how to set up your SSH keys. Once you’re ready, follow the steps below to connect with SFTP:

  1. Check your SSH access using one of these commands:
    ssh user@server_ipaddressssh user@remotehost_domainname
  2. Once that is done, leave the session if no errors occurred.
  3. Initiate an SFTP connection with the following commands:
    sftp user@server_ipaddresssftp user@remotehost_domainname
  4. If you’re using a custom SSH port, use one of these commands to change the SFTP port:
    sftp -oPort=customport user@server_ipaddresssftp -oPort=customport user@remotehost_domainname
  5. Here’s how it should look like:
    sftp -oPort=49166 user@31.220.57.32

Once you’re connected, you will see an SFTP prompt.

How to Transfer Files Using SFTP?

Here we’re going to show you how to transfer remote files to the local system using SFTP and vice versa.

Pro Tip

You can also transfer your files using SFTP clients, such as FileZilla. Learn how to use FileZilla with our comprehensive guide.

Transferring Remote Files From a Server to the Local System

To start, let’s check which local and which remote working directory we are using. To do this, we’ll use these SFTP commands:

sftp> lpwdLocal directory: /LocalDirectorysftp> pwdRemote directory: /RemoteDirectory

Now, let’s see how to transfer a file from a remote server to your local machine using the get command. Here’s the basic syntax of the get command:

get /RemoteDirectory/filename.txt

For example, to copy the file /etc/xinetd.conf from the remote server to your local machine, you would use:

get /etc/xinetd.conf

Once the download is complete, you can now find that the file xinetd.conf isin the /user/home directory of your local machine.

To download multiple files with SFTP, use the mget command. To download all files in a directory called /etc that have the .conf extension to your current working directory, you will use the following command:

mget /etc/*.conf

After the download, you can find all *.conf files in /user/home directory of your local machine.

Transferring Files From the Local Machine to a Remote Server

To copy a file from the local machine to the remote server, we’ll use the get command again. In this case, the syntax of get command will be:

get file.txt /RemoteDirectory

To move the file example.txt from a local machine to the remote machine, enter the following command:

put /home/user-name/example.txt /root

Now we will find the file in the remote server’s root directory. You can also try transferring multiple files using themput command. It works nearly the same as mget:

mput /home/user-name/*.txt /root

This command would move all files with the .txt extension in the /home/user-name from the local machine to the remote /root directory.

Pro Tip

Keep in mind that to download and upload the files with SFTP, you will need to type the command put or get and press the TAB key.

Commands for Navigating With SFTP

Some commands can be used to navigate through the remote and local servers more efficiently with SFTP. They’re similar to the ones you’d use in the Linux shell prompt.

For example, the pwd command is always useful to let you know in which working directory you are currently on.

sftp> pwdRemote directory: /RemoteDirectory

or

sftp> lpwdLocal directory: /LocalDirectory

You can also display the list of files and directories you’re using for the remote directory:

ls

Similarly, for the local working directory:

lls

For instance, the output will look similar to this:

PicturesTemplates Media Text.txt Documents

To switch from one remote working directory to another local working directory, enter the following commands:

cd name_of_directorylcd name_of_directory

Finally, use the ! and exit commands to go back to the local shell and quit SFTP.

Basics of File Maintenance Using SFTP

With SFTP, you can also manage directories and files using specific commands.

To check the remote server’s disk space in gigabytes, use the df function like so:

df -h

Here’s an output example:

Filesystem Size Used Avail Use% Mounted on/dev/ploop29212p1 59G 2.5G 56G 5% /none 1.5G 0 1.5G 0% /sys/fs/cgroupnone 1.5G 0 1.5G 0% /devtmpfs 1.5G 0 1.5G 0% /dev/shmtmpfs 1.5G 568K 1.5G 1% /runtmpfs 308M 0 308M 0% /run/user/0

Use the mkdir command to create a new directory on either the remote and local server :

mkdir name_of_directorylmkdir name_of_directory

You can delete one from the remote server using the rmdir command:

rmdir name_of_directory

Meanwhile, renaming a remote file is also rather straightforward:

rename filename new_filename

Here’s an example:

rename Old_FileExample New_FileExample

If you want to remove a remote file, use the rm command:

rm filename

While the chown command is used to replace a file’s owner:

chown userid filename

userid can either be a username or a numeric user ID. For instance:

chown UserOne FileExamplechown 1234 FileExample

chgrp is used for changing a file’s group owner:

chgrp groupid filename

For instance:

chgrp NewGroup FileExample

Finally, you will need to change a file permission using the chmod interactive command:

chmod 764 FileExample

In this example, the three-digit value stands for the file’s user, group, and other users.

As for the permissions to read (r), write (w), and execute (x), their values are 4, 2, 1, respectively. 0 can also be used to provide no permissions.

To assign permissions, simply calculate the total values for each user class. Here’s a breakdown of the example:

chmod ugo FileExample# u represents the User who'll be able to read, write and execute the file.# g is for Groups, here we've given the permission to write and execute the file.# o or Others will only be able to read the file.

List of Useful SFTP Commands

If you need a quick cheat sheet, here’s a list of all the available SFTP commands. You can find this list yourself by simply entering the help or ? command — both will prompt the same result.

bye Quit sftpcd path Change remote directory to 'path'chgrp [-h] grp path Change group of file 'path' to 'grp'chmod [-h] mode path Change permissions of file 'path' to 'mode'chown [-h] own path Change owner of file 'path' to 'own'df [-hi] [path] Display statistics for current directory or filesystem containing 'path'exit Quit sftpget [-afpR] remote [local] Download filehelp Display this help textlcd path Change local directory to 'path'lls [ls-options [path]] Display local directory listinglmkdir path Create local directoryln [-s] oldpath newpath Link remote file (-s for symlink)lpwd Print local working directoryls [-1afhlnrSt] [path] Display remote directory listinglumask umask Set local umask to 'umask'mkdir path Create remote directoryprogress Toggle display of progress meterput [-afpR] local [remote] Upload filepwd Display remote working directoryquit Quit sftpreget [-fpR] remote [local] Resume download filerename oldpath newpath Rename remote filereput [-fpR] local [remote] Resume upload filerm path Delete remote filermdir path Remove remote directorysymlink oldpath newpath Symlink remote fileversion Show SFTP version!command Execute 'command' in local shell! Escape to local shell

What Is SFTP?

SFTP, or SSH File Transfer Protocol for short, is a much more secure way to move files. Using the SSH protocol, it supports encryption and other security methods used to better protect file transfers. It’s the only secure file transfer protocol that protects against attacks at any point in the data transfer process, making it the preferred protocol.

During file transfer, all of the data is divided into packets and sent through a single secure connection.

Sensitive information will be encrypted and made unreadable when being transferred between the client and the server. In other words, the original content (plaintext) will be replaced by an incoherent string of characters (ciphertext).

Only the recipient with the required decryption key will be able to see the original content. This prevents any unauthorized access during file transfer.

Regular file transfer protocol (FTP) has two different channels to exchange data — the command channel and the data channel. In contrast, SFTP has only one encrypted channel where the data is exchanged in encrypted, formatted packets.

Conclusion

That pretty much covers the basics of how to use SFTP for secure file transfer. We hope this tutorial has proved to be useful. However, if you need more information on FTP alone, you can find more tutorials here.

If you have any more questions, don’t hesitate to leave a comment down below.

How to Use SFTP (SSH File Transfer Protocol) (2)

The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

More from Edward S.

How to Use SFTP (SSH File Transfer Protocol) (2024)
Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6169

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.