How to Use SFTP Command to Securely Transfer File

How to Use SFTP Command to Securely Transfer File

SFTP stands for SSH File Transfer Protocol Join us with this article to learn How to Use SFTP Command to Securely Transfer File. You can use the SFTP Command as a secure file protocol to access, manage, and transfer files over an encrypted SSH transport. As a secure way to move files, SFTP supports encryption and other security methods using the SSH protocol. Undoubtedly, you are here to improve the security and protection of file transfers. Since SFTP is the most brilliant secure file transfer protocol, you will be safe from attacks.

Also, buying a VPS is a good idea to transfer your considered files with a remote server securely.

What is SFTP Command and How to Connect using it?

Previously, you got familiar with the SCP command that supports only file transfers, but SFTP performs a range of operations on remote files. You might have heard about the FTP file transfer protocol. It is used in legacy applications and limited cases and because of its security issues, SFTP is now its replacement if users need an FTP server. While FTP has two different channels to exchange data, SFTP has only one channel which is encrypted to exchange data in encrypted, formatted packets.

Clearly, SFTP is preferable to FTP because security matters. SFTP is integrated into many graphical tools, but if you feel comfortable with command lines, this guide makes you happy.

SSH File Transfer Protocol is an easy and secure way to transfer files. SFTP will transfer your important data encrypted into divided packets through a single secure connection. So, you can make sure that your files will be transferred unreadable when they are being sent between the client and the server. Finally, a recipient with the related decryption key will be allowed to read the transferred content.

What is SFTP Command

Tutorial Use SFTP Command to Securely Transfer File

During a file transfer, you need to protect your data against attacks at any point in the transfer process. SFTP is a secure file transfer protocol and this article will present the way you can transfer remote files to the local system using SFTP and vice versa. Also, some other useful commands will be taught. So, let’s get started.

To authenticate and establish a secure connection, SFTP uses the SSH protocol. As you know, it is possible to authenticate with passwords but creating an SSH key to access the systems you want to transfer your public key is highly recommended. SFTP is also used to enable particular users to transfer files without SSH access. If you need to freshen your mind to set up SSH keys, refer to the guide.

However, you can check your SSH access using the commands below. (Replace your IP address and hostname)

ssh user@server_ipaddress
ssh user@remotehost_domainname

Then, you can change the SFTP port by running the following command if you are using a custom SSH port.

sftp -oPort=customport user@server_ipaddress
sftp -oPort=customport user@remotehost_domainname

And you will see an SFTP prompt when you are connected.

Get a list of all available SFTP commands

As you will view, most of the SFTP commands are similar to the Linux shell commands. Here is the most useful command which gives you access to a summary of the other SFTP commands.

sftp> help

OR

SFTP> ?

A long list of all available commands will be displayed that is included a short description of each command.

Getting Help in SFTP

Transfer Remote Files from a Server to the Local System Using SFTP Command

First, you need to check which local and remote working directories you are using. Run the commands below to do this.

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

To transfer a file from a remote server to your local machine, use the command below. Here, we use the get command basic syntax.

get /RemoteDirectory/filename.txt

Let’s see an example. You must use the following syntax to copy the file /etc/xinetd.conf from the remote server to your local server. When the download is complete, go to find the file xinetd.conf in the /user/home directory of your local server.

get /etc/xinetd.conf

In the case of downloading multiple files with SFTP, you can use the mget command. And if you need to download all files in a directory called /etc that has the .conf extension to your current working directory, type:

mget /etc/*.conf

Once the download is complete, you can find all *.conf files in the/user/home directory of your local server.

Transfer Files from the Local Machine to a Remote Server Using SFTP Command

In this part, the get command is used again to copy a file from the local machine to the remote server. So, run:

get file.txt /RemoteDirectory

Use the following command to move the file example.txt from a local machine to a remote machine.

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

Enter the command below to transfer multiple files. Here, we use mput command that works almost the same as mget.

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

Running the above command causes moving all files with the .txt extension in the /home/user-name from the local machine to the remote /root directory.

Navigating with SFTP Command

As we mentioned, there are some other useful commands that you will learn in this article. One of them is used to navigate through the remote and local servers.

To know in which working directory you are currently on, type:

sftp> pwd
Remote directory: /RemoteDirectory

OR

sftp> lpwd
Local directory: /LocalDirectory

Or even make a list of files and directories you’re using for the remote directory by running:

ls

And for the local working directory:

lls

Also, you will be able to switch from one remote working directory to another local working directory:

cd name_of_directory
lcd name_of_directory

When you’re all set, use the ! to and exit commands to go back to the local shell and quit SFTP.

How to Manipulation simple Files Using SFTP

Using the SFTP command allows you to perform some kinds of filesystem housekeeping. To change the owner of a file on the remote system, run:

Sftp> chown userID file

The SFTP command does not accept usernames and uses UIDs. Since there is no built-in way to know the appropriate UID from within the SFTP interface, you can read from the /etc/passwd file, which associates usernames with UIDs in most Linux environments:

Sftp> get /etc/passwd
Sftp> !less passwd

Manage Files Using SFTP Command

You can use SFTP to manage directories and files using specific commands. Run the command below to check the remote server’s disk space in gigabytes.

df -h

Then, to create a new directory on either the remote or local server, use the following command.

mkdir name_of_directory
lmkdir name_of_directory

Also, using the rmdir command allows you to delete one from the remote server.

rmdir name_of_directory

To rename a remote file, type:

rename filename new_filename

It is also possible to remove a remote file by running the rm command.

rm filename

Enter the following command to change a file’s group owner.

chgrp groupid filename

Finally, you can change a file’s permission. To do this, run:

chmod 764 FileExample

Useful SFTP Commands

In the latest section, let’s see a list of all the available SFTP commands to help you reach out to useful commands quickly. However, you can find this list if you enter the ”help” or ”?” command.

bye                                Quit sftp
cd 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 sftp
get [-afpR] remote [local]         Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
lls [ls-options [path]]            Display local directory listing
lmkdir path                        Create local directory
ln [-s] oldpath newpath            Link remote file (-s for symlink)
lpwd                               Print local working directory
ls [-1afhlnrSt] [path]             Display remote directory listing
lumask umask                       Set local umask to 'umask'
mkdir path                         Create remote directory
progress                           Toggle display of progress meter
put [-afpR] local [remote]         Upload file
pwd                                Display remote working directory
quit                               Quit sftp
reget [-fpR] remote [local]        Resume download file
rename oldpath newpath             Rename remote file
reput [-fpR] local [remote]        Resume upload file
rm path                            Delete remote file
rmdir path                         Remove remote directory
symlink oldpath newpath            Symlink remote file
version                            Show SFTP version
!command                           Execute 'command' in local shell
!                                  Escape to local shell

Conclusion

In this article, you learned How to Use SFTP Command to Securely Transfer File. To have a secure file transfer, all the mentioned commands will help in any related conditions. If you have already used FTP or SCP to transfer your data, try SFTP to experience the benefits of both at the same time. Other transfer file application using an SFTP client is FileZilla and WinSCP which you can use to transfer your data securely.

If you encounter any problems, please do not hesitate to contact us. Our technical support team will try their best to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked.