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
What is the 10% Savings rule? - Texas Regional Bank
Kohl's Credit Card
Ohio Houses With Land for Sale - 1,591 Properties
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
Legacy First National Bank
LA Times Studios Partners With ABC News on Randall Emmett Doc Amid #Scandoval Controversy
Whitley County Ky Mugshots Busted
Everything You Need to Know About Holly by Stephen King
6th gen chevy camaro forumCamaro ZL1 Z28 SS LT Camaro forums, news, blog, reviews, wallpapers, pricing – Camaro5.com
Oro probablemente a duna Playa e nomber Oranjestad un 200 aña pasa, pero Playa su historia ta bay hopi mas aña atras
Void Touched Curio
Lima Funeral Home Bristol Ri Obituaries
Saberhealth Time Track
Becu Turbotax Discount Code
Apne Tv Co Com
97226 Zip Code
Exterior insulation details for a laminated timber gothic arch cabin - GreenBuildingAdvisor
Who is Jenny Popach? Everything to Know About The Girl Who Allegedly Broke Into the Hype House With Her Mom
Panola County Busted Newspaper
Getmnapp
Synergy Grand Rapids Public Schools
Intel K vs KF vs F CPUs: What's the Difference?
Maisons près d'une ville - Štanga - Location de vacances à proximité d'une ville - Štanga | Résultats 201
Chelsea Hardie Leaked
Toonkor211
Inmate Search Disclaimer – Sheriff
Tire Pro Candler
Ridge Culver Wegmans Pharmacy
Www.craigslist.com Syracuse Ny
Luciipurrrr_
Workday Latech Edu
Hermann Memorial Urgent Care Near Me
Carespot Ocoee Photos
School Tool / School Tool Parent Portal
2024 Ford Bronco Sport for sale - McDonough, GA - craigslist
Buhsd Studentvue
ATM Near Me | Find The Nearest ATM Location | ATM Locator NL
Studio 22 Nashville Review
Pokemon Reborn Locations
Top 40 Minecraft mods to enhance your gaming experience
What to Do at The 2024 Charlotte International Arts Festival | Queen City Nerve
8776725837
Silicone Spray Advance Auto
Citizens Bank Park - Clio
Po Box 101584 Nashville Tn
Craigslist St Helens
News & Events | Pi Recordings
A Man Called Otto Showtimes Near Cinemark Greeley Mall
25100 N 104Th Way
Myhrkohls.con
O.c Craigslist
Southern Blotting: Principle, Steps, Applications | Microbe Online
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6246

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.