免密SSH登录Ubuntu

文章目录

  • 免密SSH登录Ubuntu
    • [1 安装openssh-server](#1 安装openssh-server)
    • [2 启动SSH服务](#2 启动SSH服务)
    • [3 生成公私钥对](#3 生成公私钥对)
      • [3.1 在客户端生成公私钥对](#3.1 在客户端生成公私钥对)
      • [3.2 在服务端生成公私钥对](#3.2 在服务端生成公私钥对)
    • [4 拷贝客户端公钥到服务端内](#4 拷贝客户端公钥到服务端内)
    • [5 远程免密登录](#5 远程免密登录)

免密SSH登录Ubuntu

1 安装openssh-server

在ubuntu内安装ssh server包:

sh 复制代码
pzs@pzs-jammy:~$ sudo apt-get install -y openssh-server

2 启动SSH服务

在Ubuntu内启动SSH服务:

sh 复制代码
pzs@pzs-jammy:~$ sudo systemctl start sshd 
# 或者使用如下命令启动:
pzs@pzs-jammy:~$ sudo service sshd start

3 生成公私钥对

3.1 在客户端生成公私钥对

假设我们的客户端是windows, 我们需要打开cmd命令窗口并执行如下命令来运行:

sh 复制代码
C:\Users\a2647>ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\a2647/.ssh/id_ed25519): y # 输入y然后回车
Enter passphrase (empty for no passphrase): # 回车
Enter same passphrase again: # 回车
Your identification has been saved in y
Your public key has been saved in y.pub
The key fingerprint is:
SHA256:pj..... a2647@pzs
The key's randomart image is:
+--[ED25519 256]--+
|o  .o.       .   |
....
+----[SHA256]-----+

之后就会生成C:\Users\a2647/.ssh/id_ed25519.pub(公钥)和C:\Users\a2647/.ssh/id_ed25519(私钥)

3.2 在服务端生成公私钥对

sh 复制代码
pzs@pzs-jammy:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pzs/.ssh/id_rsa):  # 回车
Created directory '/home/pzs/.ssh'.
Enter passphrase (empty for no passphrase):   # 回车
Enter same passphrase again:   # 回车
Your identification has been saved in /home/pzs/.ssh/id_rsa
Your public key has been saved in /home/pzs/.ssh/id_rsa.pub
The key fing... pzs@pzs-jammy
The key's randomart image is:
+---[RSA 3072]----+
|  ...            |
|.  o   .         |
...
+----[SHA256]-----+

之后就会生成$HOME/.ssh/id_rsa.pub(公钥)和$HOME/.ssh/id_rsa(私钥)

4 拷贝客户端公钥到服务端内

拷贝服务端C:\Users\a2647/.ssh/id_ed25519.pub追加到虚拟机 的~/.ssh/authorized_keys内

sh 复制代码
pzs@pzs-resolute:~$ touch ~/.ssh/authorized_keys # 注意不要使用sudo命令创建,否则会导致免密操作失效
pzs@pzs-resolute:~$ chmod 700 ~/.ssh
pzs@pzs-resolute:~$ chmod 600 ~/.ssh/authorized_keys
pzs@pzs-resolute:~$ vim ~/.ssh/authorized_keys # 添加服务端的.ssh\id_ed25519.pub的内容到里面

5 远程免密登录

之后就可以通过服务端的hostname和IP地址进行免密远程登录了,操作如下:

sh 复制代码
C:\Users\a2647>ssh pzs@192.168.20.128
The authenticity of host '192.168.20.128 (192.168.20.128)' can't be established.
ED25519 key fingerprint is SHA256:5cVJBKOz0cxCJOtZIbLcJMswQfn50gEiNUvtCneMsNE.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 如果需要的话,就输入yes
Warning: Permanently added '192.168.20.128' (ED25519) to the list of known hosts.
Welcome to Ubuntu 22.04.5 LTS (GNU/Linux 6.8.0-138-generic x86_64)

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

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

pzs@pzs-jammy:~$ # 没有输入密码就登录到ubuntu系统里面了
相关推荐
꯭自꯭闭꯭1 小时前
达梦守护集群手工切换及故障切换
linux·运维·服务器·数据库
哈__1 小时前
日志散在多台服务器怎么查?用 Promtail + Loki + Grafana 搭一套集中检索平台
运维·服务器·grafana
CHENKONG_CK1 小时前
恶劣工况下 RFID 赋能喷涂产线自动化分拣与作业
运维·网络·人工智能·自动化·汽车·rfid·rfid
wdfk_prog1 小时前
Wi-Fi Direct 教程 04:Interface 协议子系统初始化——WPA/EAPOL、WPS、DPP/NAN、GAS 与 P2P callback
android·运维·服务器·ubuntu·p2p·wps·wifi-direct
云飞云共享云桌面1 小时前
有个10人的SolidWorks设计团队,服务器什么配置合适,如何落地
运维·服务器·3d·自动化·电脑·制造
逆向编程1 小时前
VMware仅主机(Host‑Only,VMnet1)物理机‑虚拟机文件共享完整实操
运维
ai小陈1 小时前
GPU算力平台远程训练监控:TensorBoard与SSH隧道配置实战
运维·人工智能·深度学习·ai·ssh·gpu算力
cuijiecheng20181 小时前
RK3588 DEP 接入 Dante Domain Manager(DDM)完整测试记录
linux
adminwolf1 小时前
抖音个人号私信自动化回复浏览器扩展,非破解协议,合规的RPA手段
运维·自动化·rpa