SSH免密自动连接

我又开了 -
SSH免密自动连接
1.概述

对于Linux操作系统的使用中,SSH的重要性不言而喻。但每次登录都要输入冗长的用户名@主机IP,而且有时候还会忘记用户名/主机IP,再次翻查记录也是很浪费时间,所以这次我们就需要配置给服务器的SSH登录进行配置,实现免密登录。

2.配置2.1配置的前提是本地用户机和服务器都有SSH密钥公钥对
# Windows下
ssh-keygen.exe # 一直按回车即可
#Linux下
ssh-keygen # 同样一直回车
2.2完成后~/.ssh目录下会生成两个文件夹

.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则刚好有此配置项

.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 那些你所知道和不知道的事-->
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

sshlinux

扩展阅读

加个好友,技术交流

1628738909466805.jpg