zoukankan      html  css  js  c++  java
  • linux命令(28):Linux下SCP无需输入密码传输文件,python 中scp文件

    python 中scp文件:(如果下面的发送免密码已经完成的话,就直接能用下面这个)

    os.system('scp "%s" "%s:%s"' % ("123.txt", "spider@10.142.97.92", "/search/name") )

    正文:

    在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  root@localhost.domain 

    2. 将 .ssh 目录中的 id_rsa.pub 文件复制到 主机B 的 ~/.ssh/ 目录中,并改名为  authorized_keys。
    scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/authorized_keys
    以后从A主机scp到B主机就不需要密码了。

    3.使用此方法需要注意:复制的两台计算机需要用相同的账户名,这里都是用的root。为了安全起见,需要在两台机器中创建相同的账号,然后在A上用su命令切换至账号下,执行第1步,第二步中复制文件时可能.ssh目录并不存在,需要手动创建。

    在第2步中如果机器中已经存在authorized_keys文件,则需注意,这个文件可以包含多个SSH验证信息,这时可用 cat >>命令将验证文件内容附加上去。

    比如复制到scp .ssh/id_rsa.pub 192.168.10.2:/root/.ssh/a.pub

    然后执行cat ~/.ssh/a.pub >> ~/.ssh/authorized_keys

  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/lovychen/p/6429844.html
Copyright © 2011-2022 走看看