Linux命令:ssh,scp使用及免密码登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux命令:ssh,scp使用及免密码登录相关的知识,希望对你有一定的参考价值。

一、ssh使用:


            ssh  [email protected]

            ssh  -l  USERNAME HOST

            ssh  [email protected] ‘COMMAND‘  单引号

 [[email protected] ~]# ssh 110.119.131.204 #第一次连接需发送主机认证

Theauthenticity of host ‘110.119.131.204 (110.119.131.204)‘ can‘t be established.

RSAkey fingerprint is 91:a3:75:4b:3c:50:fd:5a:60:1a:39:ab:d5:0b:83:16.

Areyou sure you want to continue connecting (yes/no)? y

[[email protected]]# cat known_hosts

110.119.131.195ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEA7WecSyXB82qLwTOmh/Mhh8ic80Oj/lhS2xTjVHLz5jy8aq6XxS8gOVErcJZU4qL/eG0aDEMiyUQ5OChq1IUu3U+dO0eHLgpCt6UMd/LAxYjB5rvXE6pCvESJpTRK/oJEhnnzjHXLxZG2tD8t2xy1Bvt8+6K+2TBX64z2uEav8Yy5DcH2zQzMP/4bkn7my0WPDlwRsFSDW2CE/0GWFqKL8PWt2r9DoUS4c5umwqPe4cqyvvLA960YmeNuMlnNBKp2XOBrTpLsigsIXO6lVqhEcWUU1bV7JRPmgQbZjCu5onw1Ez4929XkVroxX02E9IS6DBGN6U6tMIC8fMwUjjrbXQ==


二、scp使用:


            scpSRC DEST      -r递归        -a

            [email protected]:/path/to/somefile /path/to/local

            scp/path/to/local [email protected]:/path/to/somewhere

[[email protected] ~]# scp 10.109.131.204:/etc/fstab ./
[email protected]‘s password:
Permission denied, please try again.
[email protected]‘s password:
fstab                                                                 100%  854     0.8KB/s   00:00    
[[email protected] ~]# ls
anaconda-ks.cfg  Desktop      install.log.syslog  minsysbak   rc.sysreboot
bincopy.sh       fstab        mbox                rc.reboot   test
cpbin.sh         install.log  minitest            rc.sysdone  testiso
[[email protected] ~]# ll fstab
-rw-r--r-- 1 root root 854 Dec  5 16:53 fstab
[[email protected] ~]# date
Mon Dec  5 16:53:28 CST 2016


ssh-keygen

     -trsa 

          ~/.ssh/id_rsa

           ~/.ssh/id_rsa.pub

     -f /path/to/KEY_FILE

      -P ‘‘: 指定加密私钥的密码


三、自动保存登录密码:


    多命令实现自动保存登录密码:

[[email protected] ~]# ssh -keygen -t rsa
Bad escape character ‘ygen‘.
[[email protected] ~]# 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:
d8:f0:bb:be:4b:aa:c0:59:4d:55:de:77:13:36:d7:11 [email protected]
[[email protected] ~]# ls .ssh/
id_rsa  id_rsa.pub  known_hosts
[[email protected] ~]# scp .ssh/id_rsa.pub [email protected]:/root
[email protected]‘s password:
Permission denied, please try again.
[email protected]‘s password:
id_rsa.pub                                                            100%  391     0.4KB/s   00:00    
[[email protected] ~]# ssh 10.109.131.204
[email protected]‘s password:
Last login: Mon Dec  5 16:18:26 2016 from 10.109.131.209

[[email protected] ~]# cat id_rsa.pub >> .ssh/authorized_keys
[[email protected] ~]# exit
logout
Connection to 10.109.131.204 closed.
[[email protected] ~]# ssh 10.109.131.204  #无密码进入
Last login: Mon Dec  5 17:09:57 2016 from 10.109.131.209


    单命令实现自动保存登录密码:

[[email protected] ~]# ssh-keygen -t rsa -f .ssh/id_rsa -P ‘‘
Generating public/private rsa key pair.

Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
f8:b2:7d:64:20:ec:5f:09:3f:3f:2b:96:df:33:60:d3 [email protected]


[[email protected] ~]# ssh 10.109.131.204
Last login: Mon Dec  5 17:11:14 2016 from 10.109.131.209
[[email protected] ~]# ssh-copy-id  -i  ~/.ssh/id_rsa.pub [email protected]
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
  .ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.

[[email protected] ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:57583               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:25                      :::*                        LISTEN      
tcp        0      0 :::45668                    :::*                        LISTEN



---end---

以上是关于Linux命令:ssh,scp使用及免密码登录的主要内容,如果未能解决你的问题,请参考以下文章

Linux机器间ssh免密登录

Windows ssh 免密登录

Linux ssh安全设置,及免密码登录

linux上ssh免密登录原理及实现

详述 iTerm2 配色及免密登录 SSH 的方法

极智开发 | linux 下 ssh 或 scp 免密连接配置方法