How to use SFTP (SSH File Transfer Protocol)

FTP is the standard method for transferring files or other data between computers, but it is becoming increasingly obsolete, due to today’s security-conscious environment. Fortunately, that’s where SFTP comes in, which is particularly useful for .

In this guide, we’ll show you how to use SFTP for secure file transfer, talk about other useful commands, and explain more about how it works.

How to connect using SFTP?

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

You can consult on how to configure your SSH keys. Once you’re ready to move on, follow the steps below to connect with SFTP:

  1. Verify your SSH access using one of these commands: ssh user@server_ipaddress ssh user@remotehost_domainname
  1. Once this is done, it leaves the session if no errors occurred.
  2. Start an SFTP connection with the following commands: sftp user@server_ipaddress sftp user@remotehost_domainname
  1. If you are using a custom SSH port, use one of these commands to change the SFTP port: sftp -oPort=customport user@server_ipaddress sftp -oPort=customport user@remotehost_domainname
  1. This is what it should look like: sftp -oPort=49166 user@31.220.57.32

Once you’re connected, you’ll see an SFTP message.

How to transfer files using SFTP?

Here we will show you how to transfer remote files to local system using SFTP and vice versa.

NOTE: You can also transfer your files using SFTP clients, such as or . If you’re interested in the latter, check out our tutorial.

Transferring remote files from a server to the local system

To start with, check which local working directory and which remote directory you are using. To do this, use these SFTP commands:

sftp> lpwd Local directory: /LocalDirectory sftp> pwd Remote directory: /RemoteDirectory

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

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 will see that the file xinetd.conf is in the directory /user/home from your local machine.

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

See also  VPS - Virtual Server In Spain |

mget /etc/*.conf

After downloading, you will find all the files *.conf In the address book /user/home from your local machine.

Transfer files from local machine to remote server

To copy a file from the local machine to the remote server, use the command get again. In this case, the command syntax get 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 root directory of the remote server. You can also try to transfer multiple files with the command put. It works almost the same as mget:

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

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

NOTE: Note that to download and upload the files with SFTP, you will need to type the command put either get and press the key tab.

Commands for browsing with SFTP

Some commands can be used to navigate through local and remote servers more efficiently with SFTP. They are similar to what you would use in the .

For example, the command pwd it is always useful to know what working directory you are currently in.

sftp> pwd Remote Directory: /RemoteDirectory

either

sftp> lpwd Local Directory: /LocalDirectory

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

ls

Similarly, for the local working directory:

lls

For example, the output will look similar to this:

Pictures Templates Media Text.txt Documents

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

cd directory_name lcd directory_name

Finally, use ! Y exit to exit the commands, return to the local shell, and exit SFTP.

Basics of file maintenance using SFTP

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

To check the disk space of the remote server in gigabytes, use the function df So:

df -h

Here is an example of the output:

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

See also  How to use the tar command in Linux

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

mkdir directory_name lmkdir directory_name

You can remove a directory from the remote server using the command rmdir:

rmdir directory_name

Likewise, renaming a remote file is also quite easy:

rename old_filename new_filename

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

rm filename

Although the command chown is used to replace the owner of a file:

chown userid filename

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

chown UserOne FileName chown 1234 FileName

chgrp is used to change the group owner of a file:

chgrp groupid filename

For example:

chgrp NewGroup FileExample

Finally, you will need to use the interactive command chmod To change the permission of a file:

chmod 764 Filename

In this example, the three-digit value represents the Usernameandthe group Y others file users.

Regarding permissions to read, write (w) Y execute(x)their values ​​are 4, 2, 1, respectively. 0 can also be used to not grant permissions.

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

chmod ugo FileName # u represents the User who will be able to read, write and execute the file. # g is for Groups, here we have given you permission to write and execute the file. # or is that Others will only be able to read the file.

List of useful SFTP commands

If you need a command sheet, here is a list of all available SFTP commands. You can find this list yourself by simply entering the commands help either ?both give the same result.

bye Exit sftp cd path Change remote directory to ‘path’ chgrp grp path Change group of file ‘path’ to ‘grp’ chmod mode path Change permissions of file ‘path’ to ‘mode’ chown own path Change owner from file ‘path’ to ‘own’ df Show statistics for the current directory or file system containing ‘path’ exit Exit sftp get remote Download help file Show this help text lcd path Change local directory to ‘ path’ lls ]Show local directory list lmkdir path Create local directory ln oldpath newpath Link to remote file (-s for symlink) lpwd Show local working directory ls Show remote directory list lumask umask Set local umask to ‘umask’ mkdir path Make remote directory progress Toggle progress meter display put local Upload pwd file Show remote working directory quit Quit sftp reget remote Resume download file rename oldpath newpath Rename remote file reput local Resume upload file rm path Delete remote file rmdir path Delete remote directory symlink oldpath newpath Remote file from symlink version Show SFTP version !command Run ‘command’ in local shell ! Escape to the local shell

See also  Website Builder - Your Website In 5 Min

What is SFTP?

SFTPeither SSH File Transfer Protocol, it is a much safer way to move files. The , supports encryption and other security methods used to better protect file transfers. It is 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 data is split into packets and sent over a single secure connection.

Sensitive information will be encrypted and rendered unreadable when transferred between client and server. In other words, the original content (plain text) will be replaced by a string of inconsistent characters (cipher text).

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

File Transfer Protocol (FTP) has two different channels for exchanging data: the command channel and the data channel. In contrast, SFTP has only one encrypted channel where data is exchanged in encrypted formatted packets.

conclusion

That’s pretty much it for the basics of how to use SFTP for secure file transfer. We hope this tutorial has been useful to you. However, if you need more information about just FTP, you can find more tutorials.

If you have any more questions, feel free to leave a comment below.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her spare time, she…

Loading Facebook Comments ...
Loading Disqus Comments ...