linux命令:ssh scp ssh-keygen -t rsa用法

时间:2022-08-30 14:25:42

   

SSH远程连接命令:

  用法格式:ssh IPADDR 不指定用户则以当前主机登录系统的用户身份去登录远程主机

    ssh USERNAME@IPADDR  通过ssh协议以某用户身份远程登录到某主机

Usage: ssh root@192.168.1.100  以root身份通过ssh协议远程登录到192.168.1.100主机

    ssh -l USERNAME IPADDR 相当于ssh USERNAME@IPADDR登录模式一样

    ssh USERNAME@IPADDR 'COMMOND' 以USERNAME用户在远程主机上执行COMMOND命令。

 

SCP远程复制命令:

  用法格式:scp SRC原文件或目录 DEST目标目录  跟cp命令一样

      -r:目录也复制

      -a:隐藏文件,链接文件都一起复制

 scp USERNAME@IPADDR:/path/to/somefile /path/to/local  从远程主机复制到本地主机来

 scp /path/to/local USERNAME@IPADDR:/path/to/somefile  从本地主机复制到远程主机上


ssh-keygen -t rsa  生成密钥

    生成的密钥保存在以下路径

    私钥密钥保存至:~/.ssh/id_rsa  当前生成用户的家目录下

    公钥密钥保存至:~/.ssh/id_rsa.pub 

   -t rsa|dsa 指定加密算法rsa算法或dsa算法

   -f /path/to/KEY_FILE 指定密码保存路径和文件名

   -N 'password':指定加密私钥的密码


 公钥追加保存至远程主机相对应用户的家目录下的~/.ssh/authorized_keys文件或  ~/.ssh/authorized_keys2文件中,一般追加保存至authorized_keys文件中


ssh-copy-id:将公钥传输至远程服务器 ~/.ssh/id_rsa.pub公钥的保存位置

   ssh-copy-id  该命令并不是所有linux版本都支持

       -i ~/.ssh/id_rsa.pub USERNAME@IPADDR

Usage: ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.100.1


实例:如何实现通过ssh远程登录主机不需要输入密码认证

    方法一:把生成的公钥追加保存至远端服务器相对应的家目录下的

         ~/.ssh/authorized_keys文件中即可


[root@localhost ~]# ssh-keygen -t rsa   生成密钥

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

/root/.ssh/id_rsa already exists.

Overwrite (y/n)? y

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:

c6:30:84:08:94:ff:91:58:82:56:25:d4:4b:53:98:e3 root@localhost.localdomain

[root@localhost ~]# ls /root/.ssh/id_rsa

/root/.ssh/id_rsa

[root@localhost ~]# ls /root/.ssh/id_rsa.pub 

/root/.ssh/id_rsa.pub

[root@localhost ~]# scp /root/.ssh/id_rsa.pub root@10.109.134.248:/root  首先把本地生成的                             公钥复制到远程主机的家目录中

[root@localhost ~]# ssh root@10.109.134.248   先远程登录到主机

[root@localhost ~]# ls 

id_rsa.pub   install.log   install.log.syslog

[root@localhost ~]# mkdir .ssh 远程主机没有.ssh目录需要新建该目录,且目录权限必须为700

[root@localhost ~]# chmod 700 .ssh/  更改权限为700

[root@localhost ~]#cat ~/id_rsa.pub >> ~/.ssh/authorized_keys 追加保存

[root@localhost ~]#exit  退出远程主机后再通过ssh登录后及可不需要输入密码就登录


  方法二:先生成密钥,然后通过命令直接复制至远程主机中


[root@localhost ~]# ssh-keygen -t rsa   生成密钥

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

/root/.ssh/id_rsa already exists.

Overwrite (y/n)? y

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:

c6:30:84:08:94:ff:91:58:82:56:25:d4:4b:53:98:e3 root@localhost.localdomain

[root@localhost ~]# ls /root/.ssh/id_rsa

/root/.ssh/id_rsa

[root@localhost ~]# ls /root/.ssh/id_rsa.pub 

/root/.ssh/id_rsa.pub

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsd.pub root@10.109.134.248

[root@localhost ~]# ssh root@10.109.134.248重新通过ssh登录远端主机后不用输入密码即可登录 







本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1884297