[转]Linux:如何使得执行scp命令时不需要输入用户密码

转自:[url]http://blog.tianya.cn/blogger/post_read.asp?BlogID=3668847&PostID=32922633[/url]
由于很多部门需要从公司服务器后台的数据库中去拉报表,
  
    且因为每个部门对数据库表的需求、字段的需求不一样,
  
    例如:
  
    客服可能需要用户表、仓库产品表。
  
    而产品部门却需要产品列表等。
  
    这样会导致数据库反复的去检索查询数据库中的数据,并且在前段用户同时访问的情况下,这样就无形的导致
  
    数据库系统以及操作系统的压力过大。
  
    为解决这个问题,所以准备将数据库整个目录定时备份到另一台机器上,然后使得需要拉报表的人员从这台backup的机器上去拉报表。
  
    ========================================================================================================
  
    问题出现:
  
    但是这时候写脚本,问题就出现,因为我要从这个“执行脚本”的机器上面备份到另一台“接收数据”的机器上去。
  
    这时候就想到了SCP命令。
  
    但是我们知道在使用SCP命令的时候,其中因为安全机制的问题会跳出输入密码的部分,而且如果两台机器如果是第一次互相通信,
  
    那么还会出现如下的需要确认的信息:
  
    # scp -rv 1.txt roo@192.168.2.70:/root/scripts/
  
    Executing: program /usr/bin/ssh host 192.168.2.70, user roo, command scp -v -r -t /root/scripts/
  
    OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
  
    debug1: Reading configuration data /etc/ssh/ssh_config
  
    debug1: Applying options for *
  
    debug1: Connecting to 192.168.2.70 [192.168.2.70] port 22.
  
    debug1: Connection established.
  
    debug1: permanently_set_uid: 0/0
  
    debug1: identity file /root/.ssh/identity type -1
  
    debug1: identity file /root/.ssh/id_rsa type -1
  
    debug1: identity file /root/.ssh/id_dsa type -1
  
    debug1: loaded 3 keys
  
    debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
  
    debug1: match: OpenSSH_4.3 pat OpenSSH*
  
    debug1: Enabling compatibility mode for protocol 2.0
  
    debug1: Local version string SSH-2.0-OpenSSH_4.3
  
    debug1: SSH2_MSG_KEXINIT sent
  
    debug1: SSH2_MSG_KEXINIT received
  
    debug1: kex: server->client aes128-cbc hmac-md5 none
  
    debug1: kex: client->server aes128-cbc hmac-md5 none
  
    debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
  
    debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
  
    debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
  
    debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
  
    The authenticity of host '192.168.2.70 (192.168.2.70)' can't be established.
  
    RSA key fingerprint is 3d:33:ce:58:2c:27:6a:bb:93:19:14:f7:7b:bc:42:8a.
  
    Are you sure you want to continue connecting (yes/no)?
  
    上面最后一行,就已经提示输入yes或是no了。这是两台机器建立临时信任机制的过程。
  
    通过信息我们可以想到,scp命令使用是SSH服务的22端口进行通信。所以这一步输入“yes”
  
    Are you sure you want to continue connecting (yes/no)? yes
  
    Warning: Permanently added '192.168.2.70' (RSA) to the list of known hosts.
  
    debug1: ssh_rsa_verify: signature correct
  
    debug1: SSH2_MSG_NEWKEYS sent
  
    debug1: expecting SSH2_MSG_NEWKEYS
  
    debug1: SSH2_MSG_NEWKEYS received
  
    debug1: SSH2_MSG_SERVICE_REQUEST sent
  
    debug1: SSH2_MSG_SERVICE_ACCEPT received
  
    debug1: Authentications that can continue: publickey,gssapi-with-mic,password
  
    debug1: Next authentication method: gssapi-with-mic
  
    debug1: Unspecified GSS failure. Minor code may provide more information
  
    No credentials cache found
  
    debug1: Unspecified GSS failure. Minor code may provide more information
  
    No credentials cache found
  
    debug1: Unspecified GSS failure. Minor code may provide more information
  
    No credentials cache found
  
    debug1: Next authentication method: publickey
  
    debug1: Trying private key: /root/.ssh/identity
  
    debug1: Trying private key: /root/.ssh/id_rsa
  
    debug1: Trying private key: /root/.ssh/id_dsa
  
    debug1: Next authentication method: password
  
    roo@192.168.2.70's password:
  
    这时候,想如果使用没有密码的用户,是否就不会跳出输入密码的部分了呢?
  
    尝试操作:
  
    1、去掉接收文件机器上的密码:
  
    # passwd -d root
  
    Removing password for user root.
  
    passwd: Success
  
    #
  
    2、尝试登录:
  
    login as: root
  
    root@192.168.2.70's password:
  
    Access denied
  
    3、执行脚本试一试:
  
    # scp -rv 1.txt roo@192.168.2.70:/root/scripts/
  
    Executing: program /usr/bin/ssh host 192.168.2.70, user roo, command scp -v -r -t /root/scripts/
  
    OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
  
    (中间信息略)
  
    roo@192.168.2.70's password:
  
    debug1: Authentications that can continue: publickey,gssapi-with-mic,password
  
    Permission denied, please try again.
  
    roo@192.168.2.70's password:
  
    4、说明:
  
    删除root密码后,从机器终端是可以登录的,但在远程是无法登录的。
  
    早期版本的linux如ReadHat8以前的版本,只要删除了root密码,在执行脚本后,需要输入密码的时候,输入任意字符就可以登录。
  
    ========================================================================================================
  
    按照以上的方法是不成功的,查阅了大量资料后发现有一篇文章提及“scp和rsync的实际应用”,文章大致内容如下:
  
    http://www.lslnet.com/linux/dosc1/30/linux-238181.htm
  
    近段时间工作需要做了些scp和rsync的异地安全备份,整理了一下,希望多大家有用。
  
    scp的备份指南:
  
    需求:用scp自动完成异地文件的定时copy
  
    难点:如何让异地主机建立安全的信任关系
  
    1、在A机器上用root运行ssh-keygen,将生成/root/.ssh/identity和/root/.ssh/identity.pub两个文件;
  
    2、在A机器上运行scp /root/.ssh/identity.pub test@192.168.1.10:/upload/.ssh/authorized_keys(假设B机器的IP地址为192.168.1.10,B机器上开了用户test,要上传到的目录是/upload,需输入密码)。目的是将本地的identity.pub放到远程机器上并改名为authorized_keys,这样就建立了新任主机,以后就不需要每次ssh都需要输入密码了;
  
    3、通过crontab建立脚本,例:30 02 * * 1-5 scp /home/data/* test@192.168.1.10:/upload。
  
    (每天2:30am自动将/home/data下面的所有文件以test用户名传到远程主机192.168.1.10的/upload目录中);
  
    认证原理:
  
    如果你希望从A作为用户user1 SSH 到B 作为用户user2, 若要用RSA键对法认证且不需要密码,则B上的ssh 后台程序拿出~user2/.ssh/authorized_keys中的与A有关的RSA公钥来对A上的以user1身份运行的ssh客户程序进行挑战,前面我们已经把A上的identity.pub拷贝到B上作为authorized_keys了。则A保持私钥identity,而B上的sshd又拿出A的公钥来挑战,因此成功,你可以加很多行到authorized_keys以允许其它服务器的公钥可以加进来。
  
    scp特点:
  
    使用ssl加密,安全性高。
  
    rsync的备份指南:
  
    需求:用rsync完成异地文件的同步
  
    难点:如何建立异地信任关系
  
    1、在A主机上(rsync服务器)上编译安装rsync,需要版本在2.4.3以上(http://rsync.samba.org),在/etc目录下建立rsyncd.conf文件,内容如下:
  
    uid = nobody
  
    gid = nobody
  
    use chroot = no # 不使用chroot
  
    max connections = 4 # 最大连接数为4
  
    log file = /var/log/rsyncd.log
  
    pid file = /var/run/rsyncd.pid
  
    lock file = /var/run/rsync.lock # 日志记录文件
  
    [test] # 这里是认证的模块名,在客户端需要指定
  
       path = /home/test # 需要同步的目录
  
       comment = test folder
  
       uid = root
  
       ignore errors # 可以忽略一些无关的IO错误
  
       read only = yes # 只读
  
       list = no # 不允许列文件
  
       auth users = rsynctest # 认证的用户名,如果没有这行,则表明是匿名
  
       secrets file = /etc/test.scrt # 认证用户密码文件
  
    2、在/etc下建立test.scrt文件,输入:
  
    用户名:密码
  
    例:rsynctest:testrsync
  
    将文件属性修改为600(千万注意)
  
    3、启动rsync服务:rsync --daemon (rsync运行在tcp 873端口,可以通过netstat -an|grep LISTEN察看)。
  
    4、在B主机上(rsync客户机)上建立/etc/test文件,内容为A主机的密码,例:
  
    testsync
  
    5、用crontab建立脚本,例:0 21 * * 1-5 rsync -vzrtp --progress --delete --password-file=/etc/test rsynctest@192.168.1.10::test /home/rsynctest
  
    rsync中的参数:v是verbose,z是压缩,r是recursive,tp都是保持文件原有属性如属主、时间
  
    的参数。--progress是指显示出详细的进度情况,--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。--password-file=/etc/test来指定密码文件,这样就可以在脚本中使
  
    用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读(600)。
  
    rsynctest@192.168.1.10中,rsynctest是指定密码文件中的用户名,192.168.1.10是A主机的IP地址::test是指模块名[test],也就是在/etc/rsyncd.conf中自定义的名称。最后的/home/rsynctest是备份到本地的目录名。
  
    (也可以用-e ssh的参数建立起加密的连接,然后和scp中信任主机的办法一样如法炮制)
  
    (在上面实例中的rsynctest并不是真实的用户,可以根据自己需要文本定义,这也是使用rsync的一大好处)
  
    rsync的特点:
  
    特性如下:
  
    1、可以镜像保存整个目录树和文件系统。
  
    2、可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  
    3、无须特殊权限即可安装。
  
    4、优化的流程,文件传输效率高。
  
    5、可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  
    6、支持匿名传输。
  
    rsync的功能还很强大,深入的眼就可以到官方网站上了解。
  
    ==========================================================================================================资料中介绍:
  
    在两台机器的两个用户之间建立安全的信任关系后,可实现执行scp命令时不需要输入用户密码。
  
    ==========================================================================================================下面实现操作:
  
    1、首先在“执行脚本”的机器上,root用户执行ssh-keygen命令,生成建立安全信任关系的证书:
  
    # ssh-keygen -b 1024 -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:
  
    b5:2c:45:9a:6e:de:ff:f2:e0:1e:e6:bb:00:78:e6:f8 root@test2.rsync.com
  
    #
  
    注意:
  
    在程序提示输入passphrase时直接输入回车,表示无证书密码。
  
    上述命令将生成私钥证书id_rsa和公钥证书id_rsa.pub,存放在用户家目录的.ssh子目录中。
  
    # cd /root/.ssh/
  
    # pwd
  
    /root/.ssh
  
    # ls
  
    id_rsa id_rsa.pub known_hosts
  
    #
  
    2、将公钥证书id_rsa.pub复制到机器“接收数据的机器”的root家目录的.ssh子目录中,同时将文件名更换为authorized_keys。
  
    # scp -p id_rsa.pub root@192.168.2.70:/root/.ssh/authorized_keys
  
    #输入机器“接收数据的机器”的root用户密码
  
    root@192.168.2.70's password:
  
    id_rsa.pub 100% 230 0.2KB/s 00:00
  
    #
  
    可以看到这个步骤,还是需要输入密码的。
  
    说明:
  
    在执行传输文件“id_rsa.pub”所需要的命令时,两台机器的root用户之间还未建立安全信任关系,所以还需要输入机器“接收数据的机器”的root用户密码。
  
    经过以上2步,就在机器“执行脚本”的root和机器“接收数据的机器”的root之间建立安全信任关系。
  
    下面我们看看效果:
  
    # pwd
  
    /usr/local/mysql/var/idaphne
  
    # ls
  
    1.txt 2.txt 3.txt
  
    # scp -p 1.txt root@192.168.2.70:/usr/local/mysql/var/idaphne
  
    1.txt 100% 2 0.0KB/s 00:00
  
    #
  
    到这里,实验结束,结果是成功的,这样将scp命令中就不用在输入密码了。
  
    下面另加一段:
  
    当执行scp命令的时候,如果出现了如下错误:
  
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  
    @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
  
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
  
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
  
    It is also possible that the RSA host key has just been changed.
  
    The fingerprint for the RSA key sent by the remote host is
  
    62:5a:5b:e3:be:5e:fa:ec:94:35:d2:de:db:c9:b1:f4.
  
    Please contact your system administrator.
  
    Add correct host key in /root/.ssh/known_hosts to get rid of this message.
  
    Offending key in /root/.ssh/known_hosts:3
  
    RSA host key for 192.168.2.70 has changed and you have requested strict checking.
  
    Host key verification failed.
  
    lost connection
  
    这是因为:/root/.ssh/known_hosts记录rsa相关信息,删除,其中关于,上面错误中显示主机:192.168.2.70的记录即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值