How to Use SFTP Commands for Secure File Transfers

SFTP commands are used to securely transfer files over an encrypted connection. These commands, such as get and put, allow users to manage and transfer data efficiently while ensuring security.

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

🤖 AI Overview:

SFTP commands provide a secure way to transfer files by using SSH encryption. These commands include operations such as file upload, download, and directory navigation, ensuring safe data management over a single encrypted channel. SFTP commands are crucial for maintaining data integrity during transfer processes.

Quick Steps to Use SFTP Commands

  1. Connect to the server using sftp user@server_ipaddress.
  2. Verify your current directory with pwd for remote and lpwd for local.
  3. Download files from remote to local using get /RemoteDirectory/filename.txt.
  4. Upload files from local to remote with put /home/user-name/example.txt /root.
  5. Navigate directories using cd for remote and lcd for local.
  6. View available commands with sftp> help.
  7. Exit SFTP session using bye or exit.

What Is SFTP and What Does It Do?

Previously, you got familiar with the SCP command that supports only file transfers, but SFTP performs a range of operations on remote files.

SFTP (SSH File Transfer Protocol) is a secure file protocol that allows users to access, manage, and transfer files over an encrypted connection. Built on SSH (Secure Shell), SFTP uses a single secure channel to transmit data, unlike FTP which relies on multiple, unencrypted channels.

While traditional commands like SCP are limited to basic file transfers, SFTP supports a broader range of operations, including directory listing, file renaming, and remote file deletion. It is supported by many graphical tools, but it also provides full functionality via the command line for users comfortable with terminal-based operations.

What is SFTP Command

Why Use SFTP Instead of FTP?

The primary reason to choose SFTP is security. FTP, though widely used in older systems, is vulnerable to eavesdropping and unauthorized access due to its lack of encryption. In contrast, SFTP ensures that every file transferred is encrypted, making it unreadable during transit unless decrypted by the intended recipient.

SFTP also simplifies network configuration by operating over a single port (usually port 22), reducing complexity and potential vulnerabilities. For businesses or individuals concerned about data protection, SFTP is a smarter, safer replacement for legacy protocols like FTP.

Moreover, if you’re transferring files to or from a remote server, using a VPS (Virtual Private Server) enhances security and gives you more control over your file management environment.

How to Use SFTP Command to Securely Transfer Files

Secure file transfer is essential in today’s digital environments, especially when managing sensitive data across remote servers. The SFTP command, which stands for SSH File Transfer Protocol, offers a reliable and encrypted method to transfer and manage files. In this article, we’ll explore what SFTP is, why it’s preferred over alternatives like FTP, and how you can use it to ensure safe and efficient data transfer.

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.

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

FAQ

To connect using SFTP, enter the command <code>sftp user@server_ipaddress</code> with your login information and server details.

Unlike FTP, SFTP provides a single encrypted channel for secure file transfers, enhancing security and data integrity.

Yes, you can change remote directories with <code>cd</code> and local directories using <code>lcd</code> for efficient file management.

Use the <code>get</code> command to download files from a remote server to your local system.

The <code>put</code> command allows you to upload files from your local machine to a remote server securely.

Yes, list files on the remote server with <code>ls</code>, and on the local machine with <code>lls</code>.

Verify your connection details and check permissions. If the issue persists, consult your technical support team for further assistance.

An SSH key provides an additional layer of security beyond passwords, ensuring a more secure authentication process. 

To exit an SFTP session, use the commands <code>bye</code> or <code>exit</code> to return to the 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.