access_time
hourglass_empty
person_outline

How to Use SFTP (SSH File Transfer Protocol)

FTP is the standard method of transferring files and data between computers, but it’s becoming more and more outdated in today’s security-conscious environment.

SFTP (SSH File Transfer Protocol), however, is a more secure way to transfer files. Using the SSH protocol, SFTP supports encryption and other security methods used to secure file transfers. This is particularly useful for VPS users. It’s the only file transfer protocol that protects against attacks at any point in the data transfer process, making it the preferred 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.

Working with SFTP

Let’s learn the basic SFTP commands and what you can use them for.

Connecting to a Remote Server Using SFTP

The syntax for connecting to a remote server via SFTP on Linux distribution is:

sftp user@ip-address 
sftp user@domain.com

If we want to connect to a Linux VPS servers with the IP 31.220.57.32 via SFTP we will use the command:

sftp user@31.220.57.32

You will be prompted to enter your password, and now you are successfully connected to the remote server.

Transferring Files from a Remote Server to the Local Machine Using SFTP

To begin, let’s check which local directory and which remote directory we are using. To do check, type in these commands:

sftp> lpwd
Local directory: /printedhere
sftp> pwd
Remote directory: /printedhere

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

get /remote-directory/filename.txt

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

get /etc/xinetd.conf

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

To download multiple files, use the mget command. To download all files in a directory called /etc that have the .conf extension, 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 Using SFTP

To copy a file from the local machine (SFTP connected) to the remote server, use the get command. The syntax of get command is:

get file.txt /RemoteDirectory

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

put /home/edward/example.txt /root

Now we will find the file in the destination machine’s (remote server’s) root directory.

You can also try transferring multiple files using the mput command. It works nearly the same as mget:

mput/home/edward/*.txt /root

This command would transfer all files with the .txt extension in the /home/edward directory from the local machine to the remote machine’s /root directory.

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

Wrap Up

There are many similar commands and functions, especially when looking at the Linux shell. Here are some standard SFTP commands that you can use.

cd – change the directory on the remote host
exit (quit) – close the connection to the remote host, and exit SFTP
get – copy a file from the remote host to the local computer
mget – copy multiple files from local to remote
put – copy a file from the local computer to the remote host
mput – copy multiple files from local to remote
rename – rename a file on the remote host
rm – delete a file on the remote host
help (?) – list of SFTP commands
version – display the SFTP version

pwd – show the present working directory on the remote host
lpwd – show the present working directory on the local computer
mkdir – create a directory on the remote host

ls– list the contents of the current directory on the remote host
lls
– list the contents of the current directory on the local computer

chmod – change the permissions of the files on the remote host
chown – change the owner of the files on the remote host

That’s it! Now you know the SFTP basics. We hope this tutorial helped you out, and these commands will be beneficial for you. Good Luck!

The Author

Author

Edward S. / @edvardasstabinskas

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Related tutorials

Leave a reply

Comment*

Name*

Email*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in Glossary
What Are WordPress Private Pages and Posts?

Close