Home  >  Article  >  System Tutorial  >  How to securely transfer files via ssh using scp command under Linux

How to securely transfer files via ssh using scp command under Linux

WBOY
WBOYforward
2024-02-09 13:39:30882browse

How to securely transfer files via ssh using scp command under Linux

On Unix or Linux operating systems, the scp utility (secure copy) is similar to the more famous command cp, but is used to copy between hosts over a secure and encrypted network. Transfer files and directories between

Since it relies on ssh for data transfer, it provides the same security and uses the same authentication as ssh. Unlike rcp, the scp command will prompt you for a password for authentication if required.

In this article, we will delve into secure file transfer in Linux and learn how to use the scp command. With detailed explanations and example use cases of common scp switches and options, you'll learn how to use this utility.

It’s important to know the following before you start

  1. Since scp relies on ssh for data transfer, an ssh key or password is required to authenticate on the remote system (Linux file transfer ssh).
  2. Confirm that you have write permissions on the target system and read permissions on the source file.
  3. Note: The scp tool uses a colon (:) to differentiate between remote and local locations.
  4. When copying a file with the same location and name on both sides, scp will overwrite the file without prior warning.

SCP Linux command syntax

In order to start using the scp command, you need to understand its basic syntax, which is in the following format:

less复制代码
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

Options: Represents scp options such as ssh configuration, password, restrictions, ssh port, recursive copy, etc.

[user@]SRC_HOST:]file1: Indicates the source file.

[user@]DEST_HOST:]file2: represents the target file

Like any other command, the scp tool has some options for controlling its behavior. The most common are the following:

  • -P: Indicates the ssh port of the remote host to connect to.
  • -p: Specifies to preserve file modifications, mode and access time from the original file.
  • -q: Need to suppress non-error messages and progress meters.
  • -C: Force scp to compress data when passing the -C flag to ssh to enable compression on encrypted secure connections.
  • -r: Tells the scp command to copy the entire directory recursively.
  • -S: The name of the program used to encrypt the connection. Since ssh is used, the program must understand the ssh switch.
  • -v: Verbose mode. Enables scp and ssh to output messages about their progress. This is useful when debugging connection, configuration and authentication issues.

Transfer local files to remote destination

Before we start using scp, let us use cp to copy files from one folder to another. We all know how to use the cp command to achieve this:

bash复制代码
cp /home/linuxmi/linuxmi.mp3 /home/linuxmi/audio

It copies the file linuxmi.mp3 in the home directory of user linuxmi to the home folder of the same user, which is the audio directory of linuxmi.

In a similar manner, an example of the scp command that can be used is as follows:

typescript复制代码
scp /home/linuxmi/linuxmi.mp3 linuxmi@linuxmi.com:/home/linuxmi/audio

Use linuxmi as the login name to upload the linuxmi.mp3 file to the server linuxmicom to the remote directory /home/linuxmi/audio���. Please note that scp will prompt you for the remote password for linuxmi before starting the upload. If no remote directory is provided, the files will be copied to the remote user's home directory. If you don't provide a filename on the destination folder, scp will be made to copy the file with the original name. If you want to save the file under a different name, you need to specify a new file name.

Transfer remote files to local target

In order to download the file, you can mention a remote location as the source location. For example :

scp linuxmi@linuxmi.com:/home/linuxmi/audio/* /home/linuxmi/downloads

It will download all audio files in the remote directory /home/linuxmi/audio on linuxmi.com to the local directory /home/linuxmi/downloads.

Note the file name:

The file name can be composed of user and host to indicate the transfer of the file to or from that host. To avoid scp processing filenames containing ":" as a host specifier, local filenames can be either absolute or relative. However, the remote filename should include both user and host.

Default port number:

If on the remote host, SSH is listening on a different port (i.e. a port other than the default port 22), you can use the -P switch to indicate that port.

Transfer local directory to remote target

Copy a directory to a remote location, similar to copying files. You just need to use the -r flag for recursive transfers.

To copy the entire directory to the remote system, run the command with the -r option, for example:

scp -r /home/linuxmi/local_folder linuxmi@linuxmi.com:/home/linuxmi/remote_folder

File transfer between two remote systems

The following command will copy the file /folder/linuxmi.mp3 from the remote host linuxmi.com to the directory /folder on the remote host www.linuxmi.com.

scp linuxmi@linuxmi.com:/folder/linuxmi.mp3 linuxmi@www.linuxmi.com:/anotherfolder

After initiating this operation, you will be asked to provide the passwords for both remote targets.

In order to route traffic through the local host (i.e. the computer on which the command is executed), use the -3 switch as follows:

scp -3 linuxmi@linuxmi.com:/folder/linuxmi.mp3 linuxmi@www.linuxmi.com:/anotherfolder

The above is the detailed content of How to securely transfer files via ssh using scp command under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete