scp 免密传输文件

如果要从A服务器将文件传送到B服务器上,就可以将A称为Client端,B称为Server端。

新建秘钥对

在Client端上新建秘钥对:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root@ap-redis01:~/.ssh# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f4:c6:5f:d0:db:0c:6a:f2:b5:c0:da:6e:9d:1b:48:ff root@ap-redis01
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
| . . o |
| . o . o = |
| S = * + o|
| . O * . |
| . =.+. |
| .. oo |
| .. ..E|
+-----------------+

更新秘钥

将Client上的id_rsa.pub内容追加到Server端上的~/.ssh/authorized_keys 中:

Client:

1
2
root@ap-redis01:~/.ssh# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAxxxxxxxxxxxxxxxxAAABAQCu1HJp2RklfK1SuEaobKolsQ1EAqm2cOkV+/eOmbddVb8GYsc/bS4NwFcg4fJTHx8SFMQyfjQzBxQLRIQO7PsMbuoC0TjGH1BdDuTDLmWbrLMryR5PN6FoK2HmBoYRi0pmSpOd/cHAwoKIfji4/A97xxT2lO9kegrCIqX3i0SdCazjJsCcQtrP62MUSZCI9b++Fw74COOlDmS3tI57NnbWfGxLYPt/gO4Ux8lwWhwkSsvxnig778jBuiMOkj52cdA0m0NWCWFepH+TWrlSZrU/tPbmSRJH2AJ0vqgttoeUV4mRBSCjNZNo0FXArCySXu2GMB5e/etRFESGQYJFX601 root@ap-redis01.xxx.xxxxxxx.com

Server:

1
2
root@ap-redis01-tx:~/.ssh# cat >> authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAxxxxxxxxxxxxxxxxAAABAQCu1HJp2RklfK1SuEaobKolsQ1EAqm2cOkV+/eOmbddVb8GYsc/bS4NwFcg4fJTHx8SFMQyfjQzBxQLRIQO7PsMbuoC0TjGH1BdDuTDLmWbrLMryR5PN6FoK2HmBoYRi0pmSpOd/cHAwoKIfji4/A97xxT2lO9kegrCIqX3i0SdCazjJsCcQtrP62MUSZCI9b++Fw74COOlDmS3tI57NnbWfGxLYPt/gO4Ux8lwWhwkSsvxnig778jBuiMOkj52cdA0m0NWCWFepH+TWrlSZrU/tPbmSRJH2AJ0vqgttoeUV4mRBSCjNZNo0FXArCySXu2GMB5e/etRFESGQYJFX601 root@ap-redis01.xxx.xxxxxxx.com

修改ssh策略

一般ubuntu会禁用root登录,所以要修改一下:

1
root@ap-redis01-tx:~# vim /etc/ssh/sshd_config

PermitRootLogin no 修改为 PermitRootLogin yes,然后重启服务。

1
root@ap-redis01-tx:~# systemctl restart ssh.service

上传文件

1
root@ap-redis01:~# scp -r /usr/prometheus/ root@ap-redis01-tx:/usr/prometheus/

后续如果为了安全,可以将秘钥从B服务器上删除!

Done!


scp 免密传输文件
https://randzz.cn/89b83855b990/scp-免密传输文件/
作者
Ezreal Rao
发布于
2022年8月24日
许可协议