mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
翻译完成
This commit is contained in:
parent
88e398c121
commit
6c1b4f6a7c
@ -1,537 +0,0 @@
|
||||
[#]: subject: "How To Securely Transfer Files With SCP In Linux"
|
||||
[#]: via: "https://ostechnix.com/securely-transfer-files-with-scp-in-linux/"
|
||||
[#]: author: "sk https://ostechnix.com/author/sk/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "MjSeven"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
How To Securely Transfer Files With SCP In Linux
|
||||
======
|
||||
|
||||
File transfer over a network can be done in various ways and using different protocols. The most commonly used protocols for **copying files remotely** are **Rsync**, **SCP** and **SFTP**. In this guide, we will look at **what is SCP** and how to **securely transfer files between local and remote computers with SCP** in Linux and Unix-like operating systems.
|
||||
|
||||
### What is SCP?
|
||||
|
||||
SCP, stands for **Secure Copy**, is a command line program to copy files and directories between a local and a remote system or between two remote systems in a secure way in Linux and Unix-like operating systems.
|
||||
|
||||
Using `scp` command, you can securely copy a file or a directory,
|
||||
|
||||
- from your local system to a remote system,
|
||||
- from a remote system to your local system,
|
||||
- between remote systems from your local system.
|
||||
|
||||
When transferring data with the scp command, the files and directories are both encrypted. So even if your network is compromised, the perpetrators can't get any meaningful data.
|
||||
|
||||
SCP is a component of the openSSH program and it uses the SSH protocol to securely transfer files. OpenSSH comes pre-installed with almost all modern Linux and Unix distributions, so don't bother installing it.
|
||||
|
||||
#### A word of caution:
|
||||
|
||||
According to the **official announcement** from the openSSH developers,
|
||||
|
||||
> The **scp protocol is outdated**, inflexible and not readily fixed. We recommend the use of more modern protocols like sftp and rsync for file transfer instead.
|
||||
>
|
||||
> Link - [https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html][1]
|
||||
|
||||
However, the majority of the users still prefer SCP over other protocols. Because, SCP handles remote-to-remote file transfers more efficiently than its counterparts such as SFTP and Rsync.
|
||||
|
||||
And also, SCP works exactly like `cp` command, while `rsync` changes its behavior based on whether the source directory has a **trailing slash** or not. Take a look at the following commands:
|
||||
|
||||
- `rsync source destination/` - would copy the source into the destination folder.
|
||||
- `rsync source/ destination/` - would copy the contents of the source folder into the destination folder.
|
||||
|
||||
So you must always double check if you've put the trailing slash in the path.
|
||||
|
||||
I personally use **[Rsync][2]** for copying large size files between two hosts and SCP for copying single files over a network.
|
||||
|
||||
### SCP Command Syntax
|
||||
|
||||
The general syntax for SCP command is given below:
|
||||
|
||||
```
|
||||
scp [-346ABCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target
|
||||
```
|
||||
|
||||
Depending upon the file transfer path, the syntax will differ. Here I have included some example syntax format.
|
||||
|
||||
Copy a file from your local system to a remote system:
|
||||
|
||||
```
|
||||
scp <options> SourceFile User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
Similarly, to copy a directory from your local system to a remote system, use `-r` flag:
|
||||
|
||||
```
|
||||
scp -r SourceDirectory User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
Copy multiple files to a remote system:
|
||||
|
||||
```
|
||||
scp <options> SourceFile1 SourceFile2 User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
Copy a file from remote system to your local system:
|
||||
|
||||
```
|
||||
scp <options> User@RemoteHost:RemoteFilePath DestinationFile
|
||||
```
|
||||
|
||||
Copy a directory from remote system to local system:
|
||||
|
||||
```
|
||||
scp -r User@RemoteHost:RemoteDirectoryPath DestinationDirectory
|
||||
```
|
||||
|
||||
Copy a file from one remote system to another remote system from your local system:
|
||||
|
||||
```
|
||||
scp <options> User@RemoteHost1:RemoteFile1 User@RemoteHost2:RemotePath
|
||||
```
|
||||
|
||||
Please note that when you copy files between two remote systems, the traffic will not pass through the local system. The operation takes place directly between two remote systems. You can, however, pass the traffic from the system on which you run scp command using `-3` option.
|
||||
|
||||
Copy a directory from one remote system to another remote system from your local system:
|
||||
|
||||
```
|
||||
scp -r User@RemoteHost1:RemoteDirectory User@RemoteHost2:DestinationPath
|
||||
```
|
||||
|
||||
### SCP Command Options
|
||||
|
||||
The most commonly used options of SCP command are:
|
||||
|
||||
- **`-C`** : Enable compression. Here, C stands for Compression. When you use this option, the data transfer speed will be faster, because the data is compressed. SCP will automatically enable the compression at the source system and decompression at the destination system.
|
||||
- **`-c <cipher>`** : c stands for cipher. By default, SCP uses **'AES-128'** encryption method for encrypting data. You can change the encryption method using `-c` option.
|
||||
- **`-i <identity_file>`** : i stands for Identity file or Private key. As you already know, there are password-based and key-based authentication used in SSH. If you want to use key-based authentication while transferring files, you can use the -i option to specify the Identity file or Private key.
|
||||
- **`-l limit`** : l stands for limit bandwidth. Using this option, you can set the maximum bandwidth used for transferring data. The limit should be specified in **`Kbit/s`**.
|
||||
- **`-F <ssh_config>`** : Some times, you may need to use different networks to connect to your Linux systems. Or you may be behind a proxy server. In such situations, you can use different `ssh_config` file using `-F` option.
|
||||
- **`-P port`** - P stands for Port. Please note that it is uppercase P. By default, SSH uses port number 22. You might have changed the port number in the destination host for security reasons. In that case, you should explicitly mention the new port number using `-P` option.
|
||||
- **`-p`** : if you want to preserve modification times, access times, and modes from the original file, you need to use -p option while copying files. Please note that it is lowercase p.
|
||||
- **`-r`** : Recursively copy entire directories.
|
||||
- **`-B`** : B stands for batch mode. It is used for selecting batch mode while transferring files. It prevents asking for passwords or passphrases.
|
||||
- **`-S program`** : Name of the program to use for the encrypted connection.
|
||||
- **`-v`** : v stands for verbose. When using the `-v` option, the command will print the progress in your Terminal screen. So, you will see the what exactly is going on when the files are being transferred. It is useful in debugging connection, authentication, and configuration problems.
|
||||
|
||||
SCP has so many options. You can check the man pages of SCP command to learn about other options. Let us see some **useful scp command examples**.
|
||||
|
||||
### Important Notes to Remember before You Begin
|
||||
|
||||
- The `scp` command relies on `ssh` for secure file transfer. So you must have either a **ssh key** or **password** to authenticate to the remote systems.
|
||||
- To be able to transfer files, you must have **read permission on the source files** and **write permission on the destination** location.
|
||||
- The `scp` command will not check the destination location before writing. Any files in the destination with the same name will be **overwritten without notification**.
|
||||
- To be able to distinguish between local and remote locations, use a **colon** (**`:`**).
|
||||
- When transferring large files, it is recommended to start the task inside a **[Screen][3]** or **[Tmux][4]** session.
|
||||
|
||||
### Transfer Files With SCP in Linux
|
||||
|
||||
As I already mentioned, we can use `scp` command to copy a file or a directory from a local system to a remote system and vice versa and copy files and folders between one remote computer to another remote computer.
|
||||
|
||||
#### 1 Copy Files with SCP from Local System to Remote System
|
||||
|
||||
To copy a file from a local system to a remote system using `scp` command, run:
|
||||
|
||||
```
|
||||
$ scp File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
**Sample Output:**
|
||||
|
||||
```
|
||||
ostechnix@192.168.1.40's password:
|
||||
File1.txt 100% 104 814.0KB/s 00:00
|
||||
```
|
||||
|
||||
Let us break down the above command and see what each option does.
|
||||
|
||||
- `**File1.txt**` - The source file to be copied to the destination.
|
||||
- `**ostechnix**` - The username of the remote system.
|
||||
- `**192.168.1.40**` - The IP address of the remote system.
|
||||
- `**/home/ostechnix/**` - The destination directory in the remote system. This is the absolute path where we want to transfer the source file i.e. `File.txt`.
|
||||
|
||||
You can also copy the file and rename it as well. The following command transfers the **`File1.txt`** to the destination and saves the file with different name **`myfile.txt`**.
|
||||
|
||||
```
|
||||
$ scp File1.txt ostechnix@192.168.1.40:/home/ostechnix/myfile.txt
|
||||
```
|
||||
|
||||
![Copy Files From Local System To Remote System][5]
|
||||
|
||||
Copy Files from Local System to Remote System
|
||||
|
||||
#### 2. Copy Multiple Files with SCP from Local System to Remote System
|
||||
|
||||
To transfer multiple files from a local system to a remote system with `scp` command, run:
|
||||
|
||||
```
|
||||
$ scp File1.txt File2.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
**Sample Output:**
|
||||
|
||||
```
|
||||
ostechnix@192.168.1.40's password:
|
||||
File1.txt 100% 104 689.4KB/s 00:00
|
||||
File2.txt 100% 496 6.3MB/s 00:00
|
||||
```
|
||||
|
||||
![Copy Multiple Files from Local System to Remote System][6]
|
||||
|
||||
Copy Multiple Files from Local System to Remote System
|
||||
|
||||
Here,
|
||||
|
||||
- `**File1.txt**` and **`File2.txt`** - The name of the sources that will be copied to the specified destination.
|
||||
- `**ostechnix@192.168.1.40**` - The username and IP address of the remote system.
|
||||
- `**/home/ostechnix**` - The destination path where we want to put the copied files.
|
||||
|
||||
If the files have same extension, you can use the following alternative commands to achieve the same goal.
|
||||
|
||||
```
|
||||
$ scp {File1,File2}.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
Or,
|
||||
|
||||
```
|
||||
$ scp *.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 3. Recursively Copy Directories with SCP from Local System to Remote System
|
||||
|
||||
To recursively copy an entire directory including the sub-directories and its contents from your local system to a remote system, use **`-r`** flag like below.
|
||||
|
||||
```
|
||||
$ scp -r Documents/ ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
![Copy a Directory from Local System to Remote System][7]
|
||||
|
||||
Copy a Directory from Local System to Remote System
|
||||
|
||||
The above command will copy the entire directory **'`Documents`'** including its contents to the destination system.
|
||||
|
||||
Here,
|
||||
|
||||
- **`-r`** : Copy files and directories recursively including the sub-directories and its contents.
|
||||
- **`Documents`** : The name of the source directory that we want to copy to the destination.
|
||||
- `**ostechnix@192.168.1.40**` : The username and IP address of the remote system.
|
||||
- `**/home/ostechnix**` : The destination path where we want to put the copied directory.
|
||||
|
||||
#### 4. Transfer Files with SCP from Remote System to Local System
|
||||
|
||||
Remember we copied `FIle1.txt` to the remote system from our local system. Let us copy it back to the local system.
|
||||
|
||||
To copy a file from a remote system to your local system with `scp`, run:
|
||||
|
||||
```
|
||||
$ scp ostechnix@192.168.1.40:/home/ostechnix/File1.txt Downloads/
|
||||
```
|
||||
|
||||
Here,
|
||||
|
||||
- `**ostechnix@192.168.1.40**` : The username and IP address of the remote system.
|
||||
- **`/home/ostechnix/File.txt`** : The absolute path of file that we want to copy to the local system.
|
||||
- **`Downloads`** - The location where to save the copied file.
|
||||
|
||||
![Transfer Files from Remote System to Local System][8]
|
||||
|
||||
Transfer Files from Remote System to Local System
|
||||
|
||||
#### 5. Transfer Multiple Files using SCP from Remote System to Local System
|
||||
|
||||
To copy multiple files from a remote system to your local system, mention the absolute path of the files that you want to copy **within the curly braces** as shown below.
|
||||
|
||||
```
|
||||
$ scp ostechnix@192.168.1.40:/home/ostechnix/\{File1.txt,File2.txt\} Downloads/
|
||||
```
|
||||
|
||||
![Transfer Multiple Files from Remote System to Local System][9]
|
||||
|
||||
Transfer Multiple Files from Remote System to Local System
|
||||
|
||||
The above command will copy the `File1.txt` and `File2.txt` from the `/home/ostechnix/` directory of the remote system to the `Downloads` directory of the local system.
|
||||
|
||||
Please note that there is **no spaces after the commas within the curly braces**.
|
||||
|
||||
#### 6. Recursively Copy Directories from Remote System to Local System
|
||||
|
||||
To copy an entire directory including the sub-directories and its contents recursively from a remote computer to your local system using `scp`, use **`-r`** flag.
|
||||
|
||||
```
|
||||
$ scp -r ostechnix@192.168.1.40:/home/ostechnix/Documents Downloads/
|
||||
```
|
||||
|
||||
The above command will copy the entire **`Documents`** from the remote system to the **`Downloads`** directory in your local system.
|
||||
|
||||
#### 7. Copy Files using SCP between Two Remote Computers
|
||||
|
||||
To copy files directly from one remote system to another remote system with `scp`, run:
|
||||
|
||||
```
|
||||
$ scp senthil@192.168.1.40:/home/senthil/File1.txt kumar@192.168.1.20:/home/kumar/
|
||||
```
|
||||
|
||||
You will be asked to enter the password of the both remote systems.
|
||||
|
||||
Here,
|
||||
|
||||
- **`senthil@192.168.1.40`** - The username and IP address of the remote system from the file is currently located.
|
||||
- **`/home/senthil/File1.txt`** - The name the file1 that is being copied and its location.
|
||||
- `**kumar@192.168.1.20**` - The username and IP address of the remote system where we want to copy the file.
|
||||
- **`/home/kumar`** - The location where to save the copied file on the remote system.
|
||||
|
||||
The above command will copy the `/home/senthil/File1.txt` from the remote host `192.168.1.40` to `/home/kumar/` directory on the remote host `192.168.1.20`.
|
||||
|
||||
In this method, the data will be transferred directly from one remote system to another remote system. If you want to route the traffic through the machine on which the command is run, use `**-3**` flag like below.
|
||||
|
||||
```
|
||||
$ scp -3 senthil@192.168.1.40:/home/senthil/File1.txt kumar@192.168.1.20:/home/kumar/
|
||||
```
|
||||
|
||||
#### 8. Enable Compression while Copying Files with SCP
|
||||
|
||||
So far we have transferred files without compressing them. Now we will enable compression while transferring files using **`-C`** flag.
|
||||
|
||||
```
|
||||
$ scp -C File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
The `-C` flag will enable compression of data at the source and automatically decompress the data at destination side.
|
||||
|
||||
By enabling compression, you can increase the file copy or transfer speed significantly.
|
||||
|
||||
#### 9. Limit Bandwidth while Transferring Files using SCP
|
||||
|
||||
We can limit the bandwidth while copying files with SCP using `-l` flag. Please note that the maximum bandwidth is specified in Kbits/s. 1 byte=8 bits. So if you want to limit bandwidth to 200 KB/s, the value for `-l` would be **1600** (200*8).
|
||||
|
||||
```
|
||||
$ scp -l 1600 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
This is useful when transferring large files to prevent SCP from throttling the bandwidth.
|
||||
|
||||
#### 10. Use Different Port while Copying Files using SCP
|
||||
|
||||
As a system admin, you might **[have changed the default port of your SSH protocol][10]** on the remote servers for security reasons. In such cases, you can specify the port number with `-P` flag when transferring files. Please note that this is **uppercase P**.
|
||||
|
||||
```
|
||||
$ scp -P 2022 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 11. Use Different Cipher while Copying Files with SCP
|
||||
|
||||
By default, SCP uses **'`AES-128`'** for encrypting files. If you want to use different cipher, use **`-c`** flag followed by the cipher name.
|
||||
|
||||
For example, if you want to use **'`3des-cbc`'** cipher, the command would be like below:
|
||||
|
||||
```
|
||||
$ scp -c 3des-cbc File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
To view the list of supported ciphers, run:
|
||||
|
||||
```
|
||||
$ ssh -Q cipher localhost | paste -d, -s -
|
||||
```
|
||||
|
||||
**Sample Output:**
|
||||
|
||||
```
|
||||
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
|
||||
```
|
||||
|
||||
#### 12. Copying Files with SCP in Verbose Mode
|
||||
|
||||
if you want to know what's going on behind the scenes while copying files with scp, you can use `**-v**` flag. When transferring files with SCP in Verbose mode, the step by step process of the SCP command execution will be displayed in the terminal. This comes in handy when troubleshooting times.
|
||||
|
||||
```
|
||||
$ scp -v File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
You will see a whole lot of output when sending files in Verbose mode as shown in the following output.
|
||||
|
||||
![Copying Files with SCP in Verbose Mode][11]
|
||||
|
||||
Copying Files with SCP in Verbose Mode
|
||||
|
||||
#### 13. Transferring Files with SCP in Quiet Mode
|
||||
|
||||
We can transfer files in quiet mode with **`-q`** flag. When sharing files in quiet mode, it will not show the copy progress, warning or diagnostic message in the output.
|
||||
|
||||
```
|
||||
$ scp -q File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 14. Preserve File Attributes when Transferring Files with SCP
|
||||
|
||||
To preserve file attributes such as file modification times, access times, and modes when copying files with SCP, use **`-p`** flag. Please note that this is **lowercase p**.
|
||||
|
||||
```
|
||||
$ scp -p File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 15. Use Identity File when Copying Files with SCP
|
||||
|
||||
SSH supports both password-based and key-based authentication. Key-based authentication is most widely used authentication method in Linux environments.
|
||||
|
||||
If you want to use key-based authentication while transferring files, use the **`-i`** option to specify the Identity file or Private key.
|
||||
|
||||
```
|
||||
$ scp -i my_private_key.pem File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 16. Use Different ssh_config File when Transferring Files with SCP
|
||||
|
||||
There are situations where you need to use different networks to connect to your Linux systems. Or you may be behind a proxy server. In such situations, you can use different `ssh_config` file using `**-F**` option.
|
||||
|
||||
```
|
||||
$ scp -F /home/ostechnix/my_ssh_config File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 17. Copy files with SCP using IPv4 or IPv6
|
||||
|
||||
We can force SCP to only use IPv4 or IPv6 addresses when copying files. This can be achieved by adding **`-4`** for IPv4 networks and **`-6`** for IPv6 networks.
|
||||
|
||||
```
|
||||
$ scp -6 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
### Frequently Asked Questions
|
||||
|
||||
#### Question 1: What is SCP?
|
||||
|
||||
**Answer:** SCP is a command line program to securely transfer files and directories from a local system to a remote system and vice versa, or between two remote systems directly.
|
||||
|
||||
#### Question 2: How to copy a file from the local computer to remote computer using SCP?
|
||||
|
||||
To copy a file from your local system to a remote system, the command would be:
|
||||
|
||||
```
|
||||
scp SourceFile.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 3: How to copy recursively copy files and directories?
|
||||
|
||||
To recursively copy a directory including the sub-directories, use `-r` flag.
|
||||
|
||||
```
|
||||
scp -r /some/local/directory User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 4: Can I transfer multiple files using SCP?
|
||||
|
||||
Yes, you can. Just mention the source file names with space separated.
|
||||
|
||||
Copy multiple files from local to remote:
|
||||
|
||||
```
|
||||
scp file1.txt file2.txt file3.txt User@RemoteHost:/some/remote/directory
|
||||
scp {file1,file2,file3}.txt User@RemoteHost:/some/remote/directory
|
||||
scp *.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
Copy multiple files from remote to local:
|
||||
|
||||
```
|
||||
scp User@RemoteHost:/some/remote/directory/\{file1.txt,file2.txt,file3.txt\} /some/local/directory
|
||||
```
|
||||
|
||||
Copy multiple files from remote to remote:
|
||||
|
||||
```
|
||||
$ scp User@RemoteHost1:/some/remote/directory/\{file1.txt,file2.txt,file3.txt\} User@RemoteHost2:/some/remote/directory/
|
||||
```
|
||||
|
||||
#### Question 5: How to transfer all files in a directory?
|
||||
|
||||
To transfer all files in a directory, switch to that directory:
|
||||
|
||||
```
|
||||
cd dir_name
|
||||
```
|
||||
|
||||
```
|
||||
scp *.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 6: Can I compress files?
|
||||
|
||||
Yes, you can. Use **`-C`** to compress files. The files are compressed at source and decompress at destination automatically.
|
||||
|
||||
```
|
||||
scp -C /some/large/file User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 7: Can I preserve file attributes?
|
||||
|
||||
To preserve file attributes such as modification times, access times, and modes from the original file, use `-p` flag.
|
||||
|
||||
```
|
||||
scp -p file.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 8: Can I use different port?
|
||||
|
||||
Yes. SCP allows you to use different port with `-P` flag.
|
||||
|
||||
```
|
||||
scp -P 2022 file.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 9: Can I use different cipher?
|
||||
|
||||
Yes, you can. Use -c flag to use different cipher.
|
||||
|
||||
```
|
||||
scp -c 3des-cbc User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### Question 10: How do I list the supported ciphers by SSH?
|
||||
|
||||
To view the list of supported ciphers by SSH and SCP, use the following command
|
||||
|
||||
```
|
||||
ssh -Q cipher localhost | paste -d, -s -
|
||||
```
|
||||
|
||||
#### Question 11: Is SCP really secure?
|
||||
|
||||
Yes, it is completely secure to use. SCP uses the same SSH mechanism used by openSSH. The data in transit is encrypted at the source side and decrypted at destination.
|
||||
|
||||
#### Question 12: Can I transfer files from a Windows system to a Linux system?
|
||||
|
||||
Yes, you can. Use either **PSCP** program to transfer files from windows platform to Linux platform. You can also use **WinSCP**.
|
||||
|
||||
### Conclusion
|
||||
|
||||
In this comprehensive guide, we learned what is SCP, and how to **securely transfer files with SCP** in Linux. We provided **17 SCP command examples**. We also looked at the commonly asked questions about SCP.
|
||||
|
||||
Whether you're a Linux Admin, or a Developer or a Regular user, you will have to copy files to and from a remote system at some point. Knowing how to **use SCP to securely copy files** will be definitely useful.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://ostechnix.com/securely-transfer-files-with-scp-in-linux/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html
|
||||
[2]: https://ostechnix.com/linux-rsync-command-examples-for-beginners/
|
||||
[3]: https://ostechnix.com/screen-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[4]: https://ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[5]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Files-from-Local-System-to-Remote-System.png
|
||||
[6]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Multiple-Files-from-Local-System-to-Remote-System.png
|
||||
[7]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Directory-from-Local-System-to-Remote-System.png
|
||||
[8]: https://ostechnix.com/wp-content/uploads/2022/11/Transfer-Files-from-Remote-System-to-Local-System.png
|
||||
[9]: https://ostechnix.com/wp-content/uploads/2022/11/Transfer-Multiple-Files-from-Remote-System-to-Local-System.png
|
||||
[10]: https://ostechnix.com/how-to-change-apache-ftp-and-ssh-default-port-to-a-custom-port-part-3/
|
||||
[11]: https://ostechnix.com/wp-content/uploads/2022/11/Copying-Files-with-SCP-in-Verbose-Mode.png
|
@ -0,0 +1,539 @@
|
||||
[#]: subject: "How To Securely Transfer Files With SCP In Linux"
|
||||
[#]: via: "https://ostechnix.com/securely-transfer-files-with-scp-in-linux/"
|
||||
[#]: author: "sk https://ostechnix.com/author/sk/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "MjSeven"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
如何在 Linux 中使用 SCP 安全地传输文件
|
||||
======
|
||||
|
||||
在网络上文件传输可以通过各种不同的方式和协议来完成。**远程复制文件**最常用的协议是 **Rsync**、**SCP** 和 **SFTP**。在本文中,我们将了解**什么是 SCP** 以及如何在 Linux 和类 Unix 操作系统中**使用 SCP 在本地和远程计算机之间安全地传输文件**。
|
||||
|
||||
### 什么是 SCP?
|
||||
|
||||
SCP,代表**安全复制**,它是一个命令行程序,在 Linux 和类 Unix 操作系统中以安全的方式在本地和远程系统之间,或在两个远程系统之间复制文件和目录。
|
||||
|
||||
使用 `scp` 命令,你可以安全地复制文件或目录:
|
||||
|
||||
- 从本地到远程系统
|
||||
- 从远程系统到本地
|
||||
- 在两个远程系统之间
|
||||
|
||||
使用 scp 命令传输数据时,文件和目录都是加密的。因此,即使网络被破坏,犯罪者也无法获得任何有意义的数据。
|
||||
|
||||
SCP 是 openSSH 程序的一个组件,它使用 SSH 协议安全地传输文件。几乎所有现代 Linux 和 Unix 发行版都预装了 OpenSSH,所以不必费心安装它。
|
||||
|
||||
#### 提醒一句:
|
||||
|
||||
根据 openSSH 开发人员的**官方公告**:
|
||||
|
||||
> **scp 协议已经过时了**,它不灵活且不易修复。我们建议使用更现代的协议,如 sftp 和 rsync 来代替。
|
||||
>
|
||||
> 参考 - [https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html][1]
|
||||
|
||||
但是,大多数用户仍然更喜欢 SCP 协议。因为,SCP 处理远程文件传输比同行 SFTP 和 Rsync 更快。
|
||||
|
||||
另外,SCP 的工作原理与 `cp` 命令完全相同,而 `rsync` 则会判断源目录是否有**结尾斜杠**而出现不同的行为。看一看下面的命令:
|
||||
|
||||
- `rsync source destination/` - 将 source 目录复制到 destination 文件夹内。
|
||||
- `rsync source/ destination/` - 将 source 目录的内容复制到 destination 文件夹中。
|
||||
|
||||
所以,你必须反复检查是否在路径中添加了斜杠。
|
||||
|
||||
我个人使用 **[Rsync][2]** 在两台主机之间复制大文件,使用 SCP 在网络上复制单个文件。
|
||||
|
||||
### SCP 命令语法
|
||||
|
||||
SCP 的通用语法如下:
|
||||
|
||||
```bash
|
||||
scp [-346ABCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target
|
||||
```
|
||||
|
||||
根据文件传输路径的不同,语法也会有所不同。这里我罗列了一些语法格式示例。
|
||||
|
||||
从本地复制一个文件到远程系统:
|
||||
|
||||
```
|
||||
scp <options> SourceFile User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
类似的,从本地系统复制一个目录到远程系统,使用 `-r` 参数:
|
||||
|
||||
```
|
||||
scp -r SourceDirectory User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
复制多个文件到远程系统:
|
||||
|
||||
```
|
||||
scp <options> SourceFile1 SourceFile2 User@RemoteHost:RemotePath
|
||||
```
|
||||
|
||||
远程系统复制文件到本地:
|
||||
|
||||
```
|
||||
scp <options> User@RemoteHost:RemoteFilePath DestinationFile
|
||||
```
|
||||
|
||||
远程系统复制目录到本地:
|
||||
|
||||
```
|
||||
scp -r User@RemoteHost:RemoteDirectoryPath DestinationDirectory
|
||||
```
|
||||
|
||||
在本地将文件在两个远程系统之间复制:
|
||||
|
||||
```
|
||||
scp <options> User@RemoteHost1:RemoteFile1 User@RemoteHost2:RemotePath
|
||||
```
|
||||
|
||||
注意,当你在两个远程系统之间复制文件时,流量不会通过本地系统。操作直接在两个远程系统之间进行。但是,你可以使用 `-3` 参数传递运行 scp 命令的系统的流量。
|
||||
|
||||
本地将目录从一个远程系统复制到另一个远程系统:
|
||||
|
||||
```
|
||||
scp -r User@RemoteHost1:RemoteDirectory User@RemoteHost2:DestinationPath
|
||||
```
|
||||
|
||||
### SCP 命令参数
|
||||
|
||||
SCP 命令最常用的参数有:
|
||||
|
||||
- **`-C`** : 启用压缩。C 代表压缩。使用此参数时,数据传输速度会更快,因为数据是压缩的。SCP 将自动在源系统上压缩,并在目标系统上解压缩。
|
||||
- **`-c <cipher>`** : c 代表加密。默认情况下,SCP 使用 **AES-128** 加密方法对数据进行加密。你可以使用 `-c` 参数更改加密方法。
|
||||
- **`-i <identity_file>`** : i 代表身份文件或私钥。如你所知,SSH 中使用基于密码或密钥的身份验证。如果希望在传输文件时使用基于密钥的身份验证,可以使用 -i 参数指定身份文件或私钥。
|
||||
- **`-l limit`** : l 代表极限带宽。通过此参数,可以设置传输数据的最大带宽。它的单位是 **`Kbit/s`**。
|
||||
- **`-F <ssh_config>`** : 有时你可能需要使用不同的网络来连接到 Linux 系统,或你有一个代理服务器,这种情况下,你可以使用 `-F` 参数使用不同的 `ssh_config` 文件。
|
||||
- **`-P port`** - P 代表端口。注意,这是大写的 P。默认情况下,SSH 使用端口 22。但出于安全原因,你可能已经更改了目标主机中的端口号。这种情况下,你应该使用 `-P` 参数显示指定新端口号。
|
||||
- **`-p`** : 如果希望保留原始文件的修改时间、访问时间和模式,你需要使用 -p 参数。注意是小写 p。
|
||||
- **`-r`** : 递归复制整个目录。
|
||||
- **`-B`** : B 代表批处理模式。它用于在传输文件时选择批处理模式。可以防止询问密码。
|
||||
- **`-S program`** : 用于加密连接的程序名称。
|
||||
- **`-v`** : v 代表详细。当使用 `-v` 参数时,命令将会在终端屏幕上打印进度。你会看到文件传输时到底发生了什么。它在调试连接、身份验证和配置问题时非常有用。
|
||||
|
||||
SCP 有很多参数,你可以查看它的手册页来了解其他参数。让我们看一些**有用的 scp 命令示例**。
|
||||
|
||||
### 开始前要记住的重要事项
|
||||
|
||||
- `scp` 命令依赖于 `ssh` 进行安全的文件传输。因此,你必须有一个 **ssh 密钥**或**密码**才能向远程系统进行身份验证。
|
||||
- 为了能传输文件,你必须对**源文件有读权限**,对**目标位置有写权限**。
|
||||
- `scp` 命令在写入前不会检查目标位置。目标位置中具有相同名称的任何文件都将被**覆盖而不通知**。
|
||||
- 为了能够区分本地和远程位置,使用**冒号**(`:`)。
|
||||
- 传输大文件时,建议在 **[Screen][3]** 或 **[Tmux][4]** 会话内启动任务。
|
||||
|
||||
### 在 Linux 中使用 SCP 传输文件
|
||||
|
||||
正如我所说,我们可以使用 `scp` 命令将文件或目录从本地复制到远程系统,反之亦然,或者在两台远程系统之间复制文件或目录。
|
||||
|
||||
### 1. 使用 SCP 从本地系统复制文件到远程系统
|
||||
|
||||
使用 `scp` 命令将文件从本地复制到远程系统,运行:
|
||||
|
||||
```
|
||||
$ scp File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
|
||||
```
|
||||
ostechnix@192.168.1.40's password:
|
||||
File1.txt 100% 104 814.0KB/s 00:00
|
||||
```
|
||||
|
||||
让我们分析一下上面的命令,看看每个参数都做了什么。
|
||||
|
||||
- **`File1.txt`** - 源文件
|
||||
- **`ostechnix`** - 远程系统的用户名
|
||||
- **`192.168.1.40`** - 远程系统的 IP 地址
|
||||
- **`/home/ostechnix/`** - 远程系统中的目标目录。这是我们想要传输源文件的绝对路径,如 `File.txt`。
|
||||
|
||||
你还可以修改目标文件的名称。下面的命令将 `File1.txt` 传输到目的地,保存为 `myfile.txt`。
|
||||
|
||||
```
|
||||
$ scp File1.txt ostechnix@192.168.1.40:/home/ostechnix/myfile.txt
|
||||
```
|
||||
|
||||
![将文件从本地复制到远程系统][5]
|
||||
|
||||
将文件从本地复制到远程系统
|
||||
|
||||
#### 2. 使用 SCP 从本地系统复制多个文件到远程系统
|
||||
|
||||
使用 `scp` 命令将多个文件从本地系统传输到远程系统,运行:
|
||||
|
||||
```bash
|
||||
$ scp File1.txt File2.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
|
||||
```
|
||||
ostechnix@192.168.1.40's password:
|
||||
File1.txt 100% 104 689.4KB/s 00:00
|
||||
File2.txt 100% 496 6.3MB/s 00:00
|
||||
```
|
||||
|
||||
![从本地复制多个文件到远程系统][6]
|
||||
|
||||
从本地复制多个文件到远程系统
|
||||
|
||||
这里:
|
||||
|
||||
- **`File1.txt`** 和 **`File2.txt`** - 源文件名
|
||||
- **`ostechnix@192.168.1.40`** - 远程系统的用户名和 IP 地址
|
||||
- **`/home/ostechnix`** - 目标文件的路径
|
||||
|
||||
如果文件具有相同的扩展名,你可以使用以下替代命令来实现相同的目标。
|
||||
|
||||
```
|
||||
$ scp {File1,File2}.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
或者,
|
||||
|
||||
```
|
||||
$ scp *.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 3. 使用 SCP 从本地到远程系统递归复制目录
|
||||
|
||||
递归地将整个目录(包括子目录及其内容)从本地复制到远程系统,使用 **`-r`** 参数。
|
||||
|
||||
```bash
|
||||
$ scp -r Documents/ ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
![从本地复制目录到远程系统][7]
|
||||
|
||||
从本地复制目录到远程系统
|
||||
|
||||
上述命令将整个 **`Documents`** 目录包括其内容复制到目标系统。
|
||||
|
||||
其中,
|
||||
|
||||
- `-r` : 递归复制文件和目录,包括子目录及其内容
|
||||
- `Documents` : 源目录名称
|
||||
- **`ostechnix@192.168.1.40`** : 远程系统的用户名和 IP 地址
|
||||
- **`/home/ostechnix`** : 目标目录的路径
|
||||
|
||||
#### 4. 用 SCP 将文件从远程系统传输到本地
|
||||
|
||||
还记得我们从本地系统复制了 `File1.txt` 到远程系统,让我们把它复制回本地。
|
||||
|
||||
使用 `scp` 命令从远程系统复制文件到本地,运行:
|
||||
|
||||
```
|
||||
$ scp ostechnix@192.168.1.40:/home/ostechnix/File1.txt Downloads/
|
||||
```
|
||||
|
||||
其中
|
||||
|
||||
- **`ostechnix@192.168.1.40`** : 远程系统的用户名和 IP 地址
|
||||
- `/home/ostechnix/File.txt` : 远程系统文件的绝对路径
|
||||
- `Downloads` - 本地保存复制文件的位置
|
||||
|
||||
![从远程系统传输文件到本地][8]
|
||||
|
||||
从远程系统传输文件到本地
|
||||
|
||||
#### 5. 使用 SCP 将多个文件从远程系统传输到本地
|
||||
|
||||
将多个文件从远程系统复制到本地,在**花括号内**注明文件的绝对路径,如下所示:
|
||||
|
||||
```
|
||||
$ scp ostechnix@192.168.1.40:/home/ostechnix/\{File1.txt,File2.txt\} Downloads/
|
||||
```
|
||||
|
||||
![将多个文件从远程系统传输到本地][9]
|
||||
|
||||
将多个文件从远程系统传输到本地
|
||||
|
||||
上述命令将从远程系统的 `/home/ostechnix/` 目录中复制 `File1.txt` 和 `File2.txt` 到本地的 `Downloads` 目录中。
|
||||
|
||||
注意,**花括号内的逗号后面没有空格**。
|
||||
|
||||
#### 6. 从远程系统递归复制目录到本地
|
||||
|
||||
使用 `scp` 从远程系统递归复制整个目录(包括子目录及其内容)到本地系统,使用 **`-r`** 参数。
|
||||
|
||||
```
|
||||
$ scp -r ostechnix@192.168.1.40:/home/ostechnix/Documents Downloads/
|
||||
```
|
||||
|
||||
上述命令将从远程系统将整个 **`Documents`** 目录复制到本地的 **`Downloads`** 目录。
|
||||
|
||||
#### 7. 使用 SCP 在两台远程计算机之间复制文件
|
||||
|
||||
使用 `scp` 命令将文件从一个远程系统直接复制到另一个远程系统,运行:
|
||||
|
||||
```
|
||||
$ scp senthil@192.168.1.40:/home/senthil/File1.txt kumar@192.168.1.20:/home/kumar/
|
||||
```
|
||||
|
||||
它会要求你输入两个远程系统的密码:
|
||||
|
||||
其中,
|
||||
|
||||
- `senthil@192.168.1.40` - 文件源端远程系统的用户名和 IP 地址
|
||||
- `/home/senthil/File1.txt` - 复制的文件名及其位置
|
||||
- **`kumar@192.168.1.20`** - 复制文件到目标端的用户名和 IP 地址
|
||||
- `/home/kumar` - 在目标端上保存复制文件的位置
|
||||
|
||||
上述命令将从远程主机 `192.168.1.40` 复制 `/home/senthil/File1.txt` 到 `192.168.1.20` 上的 `/home/kumar/` 目录。
|
||||
|
||||
在这种方法中,数据将直接从一个远程系统传输到另一个远程系统。如果你想通过本地机器路由流量,使用 **`-3`** 参数,如下所示:
|
||||
|
||||
```
|
||||
$ scp -3 senthil@192.168.1.40:/home/senthil/File1.txt kumar@192.168.1.20:/home/kumar/
|
||||
```
|
||||
|
||||
#### 8. 使用 SCP 复制文件时启用压缩
|
||||
|
||||
到目前为止,我们在没有压缩的情况下传输了文件。现在我们将使用 **`-C`** 参数在传输文件时启用压缩。
|
||||
|
||||
```
|
||||
$ scp -C File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
`-C` 参数将在源端启用压缩,并在目标端自动解压数据。
|
||||
|
||||
通过启用压缩,可以显著提高文件复制或传输速度。
|
||||
|
||||
#### 9. 使用 SCP 传输文件时限制带宽
|
||||
|
||||
我们可以使用 `-l` 参数限制带宽。注意,最大带宽单位为 Kbits/s。1 字节 = 8 bit。因此,如果你想将带宽限制在 200KB/s,`-l` 的值将是 **1600**(200*8)。
|
||||
|
||||
```
|
||||
$ scp -l 1600 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
这在传输大文件时非常有用,可以防止 SCP 限制带宽。
|
||||
|
||||
#### 10. 使用 SCP 复制文件时使用不同端口
|
||||
|
||||
作为系统管理员,出于安全原因,你可能在远程服务器上[**更改了 SSH 协议的默认端口**][10]。这种情况下,你可以在传输文件时使用 `-P` 参数指定端口号。注意:**大写的 P**。
|
||||
|
||||
```
|
||||
$ scp -P 2022 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 11. 使用 SCP 复制文件时使用不同的加密方法
|
||||
|
||||
默认情况下,SCP 使用 **`AES-128`** 对文件进行加密。如果你想使用不同的加密方法,使用 **`c`** 参数。
|
||||
|
||||
例如,如果你想使用 **3des-cbc** 加密方法,命令如下所示:
|
||||
|
||||
```
|
||||
$ scp -c 3des-cbc File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
要查看支持的密码列表,执行:
|
||||
|
||||
```
|
||||
$ ssh -Q cipher localhost | paste -d, -s -
|
||||
```
|
||||
|
||||
**示例输出:**
|
||||
|
||||
```
|
||||
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
|
||||
```
|
||||
|
||||
#### 12. 在详细模式下使用 SCP 复制文件
|
||||
|
||||
如果你想知道使用 scp 复制文件时幕后发生了什么,你可以使用 **`-v`** 参数。使用详细模式传输文件时,终端上会显示执行 SCP 命令执行的每一步过程。这在故障排除时很方便。
|
||||
|
||||
```
|
||||
$ scp -v File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
在详细模式下发送文件时,你将看到大量输出,如下所示:
|
||||
|
||||
![在 Verbose 模式下使用 SCP 复制文件][11]
|
||||
|
||||
在详细模式下使用 SCP 复制文件
|
||||
|
||||
#### 13. 在安静模式下使用 SCP 传输文件
|
||||
|
||||
我们可以使用 **`-q`** 参数在安静模式下传输文件。在安静模式下共享文件时,不会在输出中显示进度、警告或诊断信息。
|
||||
|
||||
```
|
||||
$ scp -q File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 14. 使用 SCP 传输文件时保留文件属性
|
||||
|
||||
使用 **`-p`** 参数可以保留文件修改时间、访问时间和模式等文件属性。注意,这是**小写的 p**。
|
||||
|
||||
```
|
||||
$ scp -p File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 15. 使用 SCP 复制文件时使用身份文件
|
||||
|
||||
SSH 同时支持基于密码和密钥的身份验证。密钥是 Linux 环境中使用最广泛的身份验证方法。
|
||||
|
||||
如果你想在传输文件时使用基于密钥的身份验证,使用 **`-i`** 参数指定身份文件或私钥。
|
||||
|
||||
```
|
||||
$ scp -i my_private_key.pem File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 16. 使用不同的 ssh 配置文件
|
||||
|
||||
在某些情况下,你需要使用不同的网络来连接到 Linux 系统,或你有一个代理服务器。这在情况下,你可以配合 **`-F`** 参数使用不同的 `ssh_config` 文件。
|
||||
|
||||
```
|
||||
$ scp -F /home/ostechnix/my_ssh_config File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
#### 17. 使用 IPv4 或 IPv6 复制文件
|
||||
|
||||
在复制文件时,我们可以强制 SCP 只使用 IPv4 或 IPv6 地址。IPv4 网络添加 **`-4`** 参数,IPv6 网络添加 **`-6`** 参数可以实现这一点。
|
||||
|
||||
```
|
||||
$ scp -6 File1.txt ostechnix@192.168.1.40:/home/ostechnix/
|
||||
```
|
||||
|
||||
### 常见问题
|
||||
|
||||
#### 问题 1:什么是 SCP?
|
||||
|
||||
**回答:** SCP 是一个命令行程序,旨在将文件和目录从本地系统安全地传输到远程系统,反之亦然,或者直接在两个远程系统之间传输。
|
||||
|
||||
#### 问题 2: 如何使用 SCP 将文件从本地复制到远程计算机?
|
||||
|
||||
将文件从本地复制到远程系统,命令如下:
|
||||
|
||||
```
|
||||
scp SourceFile.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 3:如何递归复制文件和目录?
|
||||
|
||||
递归复制包含子目录的目录,使用 `-r` 参数:
|
||||
|
||||
```
|
||||
scp -r /some/local/directory User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 4:使用 SCP 可以传输多个文件吗?
|
||||
|
||||
当然,只要用空格分隔源文件名即可。
|
||||
|
||||
从本地复制多个文件到远程:
|
||||
|
||||
```
|
||||
scp file1.txt file2.txt file3.txt User@RemoteHost:/some/remote/directory
|
||||
scp {file1,file2,file3}.txt User@RemoteHost:/some/remote/directory
|
||||
scp *.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
从远程复制多个文件到本地:
|
||||
|
||||
```
|
||||
scp User@RemoteHost:/some/remote/directory/\{file1.txt,file2.txt,file3.txt\} /some/local/directory
|
||||
```
|
||||
|
||||
从一个远程系统复制多个文件到另一个远程系统:
|
||||
|
||||
```
|
||||
$ scp User@RemoteHost1:/some/remote/directory/\{file1.txt,file2.txt,file3.txt\} User@RemoteHost2:/some/remote/directory/
|
||||
```
|
||||
|
||||
#### 问题 5:如何传输目录下的所有文件?
|
||||
|
||||
传输整个目录,首先进入该目录:
|
||||
|
||||
```
|
||||
cd dir_name
|
||||
```
|
||||
|
||||
然后,
|
||||
|
||||
```
|
||||
scp *.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 6:可以压缩文件吗?
|
||||
|
||||
当然。使用 **`-C`** 压缩文件。文件会在源端压缩,在目标端自动解压缩。
|
||||
|
||||
```
|
||||
scp -C /some/large/file User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 7:可以保留文件属性吗?
|
||||
|
||||
保留原始文件的修改时间、访问时间和模式等文件属性,使用 **`-p`** 参数。
|
||||
|
||||
```
|
||||
scp -p file.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 8: 可以使用其他端口吗?
|
||||
|
||||
当然。SCP 配合 **`-P`** 参数允许你使用其他端口。
|
||||
|
||||
```
|
||||
scp -P 2022 file.txt User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 9: 可以使用不同的加密方法吗?
|
||||
|
||||
当然。使用 **`-c`** 参数。
|
||||
|
||||
```
|
||||
scp -c 3des-cbc User@RemoteHost:/some/remote/directory
|
||||
```
|
||||
|
||||
#### 问题 10: 如何列出 SSH 支持的加密方法?
|
||||
|
||||
使用以下命令查看 SSH 和 SCP 支持的加密方法列表:
|
||||
|
||||
```
|
||||
ssh -Q cipher localhost | paste -d, -s -
|
||||
```
|
||||
|
||||
#### 问题 11:SCP 真的安全吗?
|
||||
|
||||
当然,它用起来是完全安全的。SCP 和 openSSH 使用相同的 SSH 机制。传输的数据在源端加密,目标端解密。
|
||||
|
||||
#### 问题 12:可以从 Windows 系统传输文件到 Linux 吗?
|
||||
|
||||
当然。使用 **PSCP** 程序将文件从 windows 传输到 Linux 平台,你也可以使用 **WinSCP**。
|
||||
|
||||
### 总结
|
||||
|
||||
在这篇全面指南中,我们了解了什么是 SCP,以及如何在 Linux 中使用 **SCP 安全地传输文件**,其中包括 **17 个 SCP 命令示例**,另外还回答了关于 SCP 的常见问题。
|
||||
|
||||
无论你是 Linux 管理人员、开发人员还是普通用户,你都会面临某个时候将文件复制到远程系统或从远程系统复制文件的情况,知道如何**使用 SCP 安全地复制文件**将是非常有用的。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://ostechnix.com/securely-transfer-files-with-scp-in-linux/
|
||||
|
||||
作者:[sk][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[MjSeven](https://github.com/MjSeven)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://ostechnix.com/author/sk/
|
||||
[b]: https://github.com/lkxed
|
||||
[1]: https://lists.mindrot.org/pipermail/openssh-unix-dev/2019-March/037672.html
|
||||
[2]: https://ostechnix.com/linux-rsync-command-examples-for-beginners/
|
||||
[3]: https://ostechnix.com/screen-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[4]: https://ostechnix.com/tmux-command-examples-to-manage-multiple-terminal-sessions/
|
||||
[5]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Files-from-Local-System-to-Remote-System.png
|
||||
[6]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Multiple-Files-from-Local-System-to-Remote-System.png
|
||||
[7]: https://ostechnix.com/wp-content/uploads/2022/11/Copy-Directory-from-Local-System-to-Remote-System.png
|
||||
[8]: https://ostechnix.com/wp-content/uploads/2022/11/Transfer-Files-from-Remote-System-to-Local-System.png
|
||||
[9]: https://ostechnix.com/wp-content/uploads/2022/11/Transfer-Multiple-Files-from-Remote-System-to-Local-System.png
|
||||
[10]: https://ostechnix.com/how-to-change-apache-ftp-and-ssh-default-port-to-a-custom-port-part-3/
|
||||
[11]: https://ostechnix.com/wp-content/uploads/2022/11/Copying-Files-with-SCP-in-Verbose-Mode.png
|
Loading…
Reference in New Issue
Block a user