Linux sftp command to transfer files

SFTP is a secure file transfer protocol based on SSH. It uses an encrypted SSH connection to access, manage, and transfer files

4 min read
By myfreax
Linux sftp command to transfer files
Linux sftp command to transfer files

SFTP is a secure file transfer protocol based on SSH. It uses an encrypted SSH connection to access, manage, and transfer files.

Compared with the traditional FTP protocol, SFTP provides all the functions of FTP and is easier to configure.

SFTP Unlike the scp command, which only allows file transfer, SFTP allows you to perform a series of operations on a remote file and resume file transfer.

In this tutorial, we'll show you how to use the Linux sftp command.

To transfer and manage files through SFTP, you must have the write permission of the remote system.

To transfer large files, you are advised to run the sftp command screen, tmux, and nohup.

SFTP architecture is a client-server model architecture. It is a subsystem of SSH and supports all SSH authentication mechanisms.

sftp does not have its own independent server. The SSH service includes the sftp service. Procedure.

You only need to install SSH on the remote server to use sftp to connect to the remote server. You can follow our tutorial on how to install the SSH service on Linux.

Although the default configuration is to use traditional password authentication. But easy to use.

However, if you often use SSH or SFTP to connect to the server, this section describes how to configure ssh login without a password.

You can use the sftp command to open an SFTP connection to the remote system, followed by the remote server username and IP address or domain name.

If you are using password authentication, you will be prompted for your user password. After connecting, the remote server will display a confirmation message and the sftp> prompt.

If your SSH server is not listening on the default port 22, use the oPort option to specify the port.

sftp remote_username@server_ip_or_hostname

sftp -oPort=custom_port remote_username@server_ip_or_hostname #specify port
Connected to remote_username@server_ip_or_hostname.sftp>

SFTP command

Most SFTP commands are similar or identical to those you use in a Linux shell. You can type help at the sftp> prompt or? Gets a list of all available SFTP commands.

After logging in to the remote server, the current working directory is the remote user's home directory. You can view the current working directory by typing the command pwd.

As with Linux, you can list files and directories using the ls command. Use the cd command to navigate to another directory.

The commands above are used to navigate and process remote locations. The sftp prompt also provides commands for local navigation and file management.

In theory, commands that operate local files only need to be prefixed with l.

For example, to print a local working directory, you can run the command lpwd. SFTP also allows you to execute some basic file manipulation commands.

Here are some examples of how to use the SFTP shell.

For example, use the df command to display statistics on remote system disk usage. mkdir creates a new directory on the remote server.

rename Renames a file on the remote server, and rm deletes the file on the remote server. rmdir Deletes directories on the remote server, chmod changes file permissions on the remote system.

chown changes the owner of the file on the remote system, and you must provide the user identity for the chown and chgrp commands.

Use the chgrp command to change the owner of a remote file group. When you are done, you can close the connection by typing bye or quit.

lpwd # Print the current local directory 

lls # List the files in the current local directory 

rename file_name new_file_name # rename remote file 

rm file_name # Delete the remote server file 

rmdir directory_name # Delete remote server directory 

chmod 644 file_name # Change file permission 
chown user file_name # Change file owner 

chgrp group file_name # Change file owning group

Transfer files

SFTP allows you to securely transfer files between two computers.

In most cases, you will use a desktop SFTP client such as WinSCP or FileZilla to connect to a remote server to download or upload files.

However, when you are working on a server without a GUI, and you want to transfer files or you want to do something else with remote files, then you need to use the command line mode.

After logging in to the remote server, the current working directory is the remote user's home directory.

When you run the sftp command to download a file, the file is downloaded to the directory where you type the sftp command.

Use the get command to download a single file from a remote server. If no file name is specified, get will save the file with the original name.

If you want to save the downloaded file by another name, specify the name later.

Use the -r option to download the directory from the remote system, and if the file transfer fails or is interrupted. you can restore it using the reget command. The reget syntax is the same as the get syntax.

get filename.zip #download file
get filename.zip local_filename.zip #download file with custom name
get -r remote_directory #download directory to local
reget filename.zip # restore sftp download

You can run the put command to upload files from the local directory to the remote FTP server.

If you want to upload a file that is not in the current working directory, use the absolute path to the file.

The put option is the same as the get option. The -r option allows you to upload a local directory to a remote server. The reput command restores the interrupted upload.

zip # Upload file put -r locale_directory # Upload directory reput filename.zip # Restore file uploads

conclusion

In this tutorial, you learned how to use the sftp command to download files and upload them to a remote SFTP server.

Related Articles