SSH远程免密别名登陆

目录

    • SSH的安装
    • 本地宿主机的操作
    • 上传密钥-公钥
    • 通过编写配置文件,别名访问远程服务器

SSH的安装

   通常本地宿主机想要访问远程的服务器时,需要借助ssh这种类似的加密协议进行传输。
   SSH运行和使用分别为openssh-client和openssh-server。因此,我们需要查看是否安装该类软件包。
➜  ~ dpkg -l | grep ssh
ii  libssh-4:amd64                             0.8.0~20170825.94fa1e38-1ubuntu0.2           amd64        tiny C SSH library (OpenSSL flavor)
ii  libssh-gcrypt-4:amd64                      0.8.0~20170825.94fa1e38-1ubuntu0.2           amd64        tiny C SSH library (gcrypt flavor)
ii  openssh-client                             1:7.6p1-4ubuntu0.3                           amd64        secure shell (SSH) client, for secure access to remote machines
ii  openssh-server                             1:7.6p1-4ubuntu0.3                           amd64        secure shell (SSH) server, for secure access from remote machines
ii  openssh-sftp-server                        1:7.6p1-4ubuntu0.3                           amd64        secure shell (SSH) sftp server module, for SFTP access from remote machines
ii  ssh                                        1:7.6p1-4ubuntu0.3                           all          secure shell client and server (metapackage)
ii  ssh-import-id                              5.7-0ubuntu1                                 all          securely retrieve an SSH public key and install it locally

如果没有安装SSH的服务:
通常本地宿主机想要访问远程的服务器时,通过在命令行输入命令:

ssh -p port root@ip地址    
sudo apt install openssh-server
sudo vim /etc/ssh/sshd_config  //进行ssh配置文件的修改。
systemctl start sshd

本地宿主机的操作

ssh-keygen -t rsa //生成一对rsa的密钥。

如果你想要查看生成密钥内容的话:

➜  ~ ls -a ~/.ssh
.  ..  config  id_rsa  id_rsa.pub  known_hosts  known_hosts.old  known_hosts.pu

上传密钥-公钥

➜  ~ proxychains ssh-copy-id [email protected]       //proxychains是提前安装地Terminal代理服务软件     
ProxyChains-3.1 (http://proxychains.sf.net)
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 2 key(s) remain to be installed — if you are prompted now it is to install the new keys
|S-chain|-<>-127.0.0.1:1080-<><>-xx.xx.xxx.162:22-<><>-OK
[email protected]'s password: 

Number of key(s) added: 2

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

结果

➜  ~ proxychains ssh [email protected]                 
ProxyChains-3.1 (http://proxychains.sf.net)
|S-chain|-<>-127.0.0.1:1080-<><>-xx.xx.xxx.162:22-<><>-OK
Welcome to Ubuntu 19.04 (GNU/Linux 5.0.0-13-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu May  9 16:53:15 UTC 2019

  System load:  0.0                Processes:           145
  Usage of /:   0.6% of 315.03GB   Users logged in:     0
  Memory usage: 1%                 IP address for ens3: xx.xx.xxx.162
  Swap usage:   0%

 * Ubuntu's Kubernetes 1.14 distributions can bypass Docker and use containerd
   directly, see https://bit.ly/ubuntu-containerd or try it now with

     snap install microk8s —classic

16 updates can be installed immediately.
12 of these updates are security updates.


Last login: Thu May  9 16:04:30 2019 from xx.xx.xxx.345
root@vultr:~# ls -a ~/.ssh
.  ..  authorized_keys   //上传文件自动生成的文件。里面存有的本地宿主机的公钥。

通过编写配置文件,别名访问远程服务器

touch ~/.ssh/config    //新建一个config文件。
➜  ~ vim ~/.ssh/config

编写config文件

  1 Host docker_server
  2                 Hostname xx.xx.xxx.162
  3                 Port 22
  4                 User root
  5 Host syy_vps 
  6                 Hostname xx.xx.xxx.162
  7                 Port 28905
  8                 User root
  9 Host vps  
 10                 Hostname xx.xx.xxx.162
 11                 Port 29979
 12                 User root
 13 StrictHostKeyChecking no    //最不安全的级别,当然也没有那么多烦人的提示了,相对安全的内网测试时建议使用。
 14 UserKnownHostsFile /dev/null  //这里为了简便,将knownhostfile设为/dev/null,就不保存在known_hosts中了

最后,别名ssh登陆进远程服务器中:

➜  .ssh ssh docker_server             
Welcome to Ubuntu 19.04 (GNU/Linux 5.0.0-13-generic x86_64)

你可能感兴趣的:(linux)