Home > Article > System Tutorial > How to securely transfer files via ssh using scp command under Linux
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.
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:
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!