Linux下scp免密传输文件

在Linux环境下,两台主机之间传输文件一般使用scp命令,通常用scp命令通过ssh获取对方linux主机文件的时候都需要输入密码确认。

不过通过建立信任关系,可以实现不输入密码。

假设A的IP:10.134.9.106

B的IP:10.134.9.107

需要从A免密码输入复制文件至B。
1. 在主机A上执行如下命令来生成配对密钥:
ssh-keygen -t rsa

按照提示操作,注意,不要输入passphrase。提示信息如下
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:
ff:8e:85:68:85:94:7c:2c:46:b1:e5:2d:41:5c:e8:9b [email protected]

2. 将 .ssh 目录中的 id_rsa.pub 文件复制到 主机B 的 ~/.ssh/ 目录中,并改名为 authorized_keys。
以下这个语句就是复制并改名的语句
scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/authorized_keys

Linux下scp的命令格式

scp	/home/test/test.zip	[email protected]:/home/test/
scp	文件源地址	目的主机用户名@目的主机IP地址:目的主机文件地址

Python代码执行scp传输

import subprocess
# 从本机scp传输到10.222.0.88
subprocess.call(['scp', '/home/test/test.zip','[email protected]:/home/test/'])

猜你喜欢

转载自blog.csdn.net/msq16021/article/details/113140850