Linux series explanation - [scp] command line to transfer files between two hosts

Sometimes it is inconvenient to create a shared folder to copy files. At this time, the scp command can be used to realize the file copy operation between two hosts.

1. Preparatory work - install ssh service:

When scp copies the files of the remote computer, it will access the remote computer through the ssh service, so the remote computer must have the ssh service installed. The following introduces the methods of installing ssh service on linux and windows platforms respectively

1.1 windows platform (Win10)

  1. Install ssh client and server
    insert image description here
  2. Start and shut down the ssh service
    Call the cmd or powershell terminal, and then execute the following commands according to the situation
    Start the service: net start sshd
    Close the service: net stop sshd

1.2 Linux platform (Ubuntu18.04)

1. Install openssh-server

sudo apt install openssh-server

2. Open service and close service

#启动服务
sudo systemctl start ssh

#关闭服务
sudo systemctl stop ssh

3. View service status

sudo systemctl status sshd

On state:
insert image description here

Disabled:
insert image description here

2. Example of using the scp command

Basic syntax:

#文件拷贝
scp 用户名@文件源ip地址:文件源路径 目标路径(本机)

#文件夹拷贝,加-r即可
scp -r 用户名@文件夹源ip地址:文件夹源路径 目标路径(本机)

Note:
1. The host to be accessed must open the ssh service.
2. The local machine above refers to the operating machine. For example, if you log in to machine A through the terminal, then this A is the local machine at this time.

2.1 When the remote machine is a linux system

Example: Copy the files in the computer with ip 192.100.10.3 /home/sun/.gitconfigto the local current working directory

scp [email protected]:/home/sun/.gitconfig ./

2.2 Remote machine time windows system time

Example: Copy the files in the computer with ip 192.100.10.4 D:\1.txtto the local current working directory

scp [email protected]:/D:/1.txt ./

Note:

  1. The above examples are to copy the files of other computers to the local, if you want to copy the local files to other computers, you only need to exchange the source path and the target path;
  2. The destination address and source address cannot both be remote machines, such as: scp [email protected]:/xxx [email protected]:/xxx.

3. Problems encountered

3.1 connect to host 192.32.1.6 port 22: Connection timed out

sun@pc:~/code$ scp -v 1.txt [email protected]:/E:/share
Executing: program /usr/bin/ssh host 192.32.1.6, user administrator, command scp -v -t /E:/share
OpenSSH_7.6p1 Ubuntu-4ubuntu0.7, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/sun/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.32.1.6 [192.32.1.6] port 22.
debug1: connect to address 192.32.1.6 port 22: Connection timed out
ssh: connect to host 192.32.1.6 port 22: Connection timed out
lost connection

This is because the ssh server of windows has not been started, so it has been unable to connect. The method of starting the windows ssh service is as follows
insert image description here

3.2 Permission denied, please try again.

sun@pc:~/code$ scp [email protected]:/E:/gitcommit_template ~/
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
[email protected]: Permission denied (publickey,password,keyboard-interactive).

When you ensure that the ssh service is enabled, and this problem still occurs, it must be that the user name or password is wrongly written. It should be that one administrtoris administratormissing a. Don't underestimate this kind of problem, it will often trouble you for a long time.

Guess you like

Origin blog.csdn.net/In_engineer/article/details/124961097