关于ssh:SSH免密自动连接

6次阅读

共计 2793 个字符,预计需要花费 7 分钟才能阅读完成。

1. 概述

对于 Linux 操作系统的应用中,SSH 的重要性显而易见。但每次登录都要输出简短的用户名 @主机 IP,而且有时候还会遗记用户名 / 主机 IP,再次翻查记录也是很浪费时间,所以这次咱们就须要配置给服务器的 SSH 登录进行配置,实现免密登录。

2. 配置

2.1 配置的前提是本地用户机和服务器都有 SSH 密钥公钥对

# Windows 下
ssh-keygen.exe # 始终按回车即可
#Linux 下
ssh-keygen # 同样始终回车 

2.2 实现后~/.ssh 目录下会生成两个文件夹

known_hosts: 是已连贯的主机信息
id_rsa: 私钥(不可外露)
id_rsa.pub
config:SSH 配置文件

2.3 服务器端批改 /etc/ssh/sshd_config

增加 PubkeyAuthentication yes,如果原有的话,勾销正文即可

实现之后,重启 ssh 服务

/etc/init.d/ssh restart
# 有的零碎会是上面的,不影响的,只有能重启 SSH 服务即可
/etc/init.d/sshd restart

2.4 创立 authorized_keys 文件

a. 复制主机的 SSH 公钥到服务器上

scp ~/.ssh/id_rsa.pub user@xx.xx.xx.xx:~/.ssh/authorized_keys

b. 批改 authorized_keys 的权限

零碎不能让所有者之外的用户对 authorized_keys 文件有写权限,否则,sshd 将不容许应用该文件,因为它可能会被其余用户篡改。所以我须要给 authorized_keys 更改权限 600 即可

root@iZuf633xawg78i05qrd9v9Z:~# chmod 600 .ssh/authorized_keys 
root@iZuf633xawg78i05qrd9v9Z:~# ll .ssh/
total 24
drwx------  2 root root 4096 Mar 18 10:58 ./
drwx------ 11 root root 4096 Mar 18 10:58 ../
-rw-------  1 root root  808 Mar 18 10:58 authorized_keys
-rw-------  1 root root 1675 Aug  6  2019 id_rsa
-rw-r--r--  1 root root  410 Aug  6  2019 id_rsa.pub
-rw-r--r--  1 root root 1990 Mar 18 10:53 known_hosts
root@iZuf633xawg78i05qrd9v9Z:~# 

2.5 免密登录

实现之后即可应用 ssh user@ip 免密登录

➜  ~ ssh root@xxxx.xxxx.xxxx.xxxx
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-52-generic x86_64)

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

 * Latest Kubernetes 1.18 beta is now available for your laptop, NUC, cloud
   instance or Raspberry Pi, with automatic updates to the final GA release.

     sudo snap install microk8s --channel=1.18/beta --classic

 * Multipass 1.1 adds proxy support for developers behind enterprise
   firewalls. Rapid prototyping for cloud operations just got easier.

     https://multipass.run/

 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

Welcome to Alibaba Cloud Elastic Compute Service !

2.6 参考链接

2.6.1 对于 SSH 的配置

  • 设置 SSH 免密登录近程服务器
  • SSH 设置公钥认证
  • 配置自定义 SSH 连贯以简化近程拜访
  • SSH 配置 AUTHORIZED_KEYS 后依然须要输出明码的问题

如果您应用的是 WSL,那么 WSL 和 Windows 共用一套端口的事件您肯定要晓得

  • WSL 使用指南 – 共用端口

3. 优化登录形式

3.1 操作方法

惯例的 ssh user@IPaddress 还是略显麻烦,尤其是在不带历史命令提醒的 Powershell 中,而 SSH 则刚好有此配置项

该文件的目录构造为:

➜  ~ type .\.ssh\config
#Ali Server
Host ali  # 主机名,任意起
  HostName xx.xx.xxx.xx  # 连贯地址,个别为 ssh user@xxx.xx 的后部
  User root  # 要登录的用户
# Embed Linux Server
Host lite
  HostName 192.168.107.130
  User lite
➜  ~

这个 config 可能够当作是一个脚本文件,官网曾经将程序写好了,你须要做的是,增加脚本中用到的参数。实现后保留,当然要给它 600 的权限,最初试一下成果:

➜  ~ ssh lite # 再也不必输出简短的指令,小小一句即可实现
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 5.3.0-42-generic x86_64)

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


 * Canonical Livepatch is available for installation.
   - Reduce system reboots and improve kernel security. Activate at:
     https://ubuntu.com/livepatch

0 packages can be updated.
0 updates are security updates.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Wed Mar 18 05:25:17 2020 from 192.168.107.1

3.2 致谢

感激这几个老哥的教程,还有一个老哥把 SSH 连贯的过程讲得很具体。

  • 简化 ssh 连贯服务器流程
  • <!–SSH Config 那些你所晓得和不晓得的事 –>
正文完
 0