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 24drwx------  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_hostsroot@iZuf633xawg78i05qrd9v9Z:~# 

2.5免密登录

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

➜  ~ ssh root@xxxx.xxxx.xxxx.xxxxWelcome 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/livepatchWelcome 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 ServerHost ali  # 主机名,任意起  HostName xx.xx.xxx.xx  # 连贯地址,个别为ssh user@xxx.xx的后部  User root  # 要登录的用户# Embed Linux ServerHost 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/livepatch0 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 那些你所晓得和不晓得的事-->