免密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系统里面了
相关推荐
码间留白18 分钟前
手把手把 GitHub 推上阿里云 ECS:Actions CI/CD 全链路避坑实录(折腾好几天,到 2 分钟见绿)
运维·阿里云·ci/cd·docker·devops·github actions
丰色木夕1 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
吹什么轩2 小时前
linux系统复习:僵尸进程与孤儿进程
linux·运维·php
殷忆枫2 小时前
告别依赖地狱!在 Ubuntu 上用 Docker 极速部署 Home Assistant 与 MQTT,并接入 Siri 与嵌入式 C 终端
linux·ubuntu·docker
第404号工位2 小时前
手机到底有没有偷听我们?我抓包了 72 小时,抓到了真凶
linux·人工智能·架构
老李IT笔记2 小时前
连不上不一定是网不好:新系统不认的服务端配置卡在哪一档
运维·服务器·网络
字节跳动开源2 小时前
ByteDance 2026 上半年 Linux 内核贡献回顾
linux·开源
小此方3 小时前
Linux网络(二十八):从NAT转换到正反向代理:图解内网通信、NAPT端口映射
linux·运维·网络
刚入门的大一新生3 小时前
Linux-进程池
linux·运维·服务器
数据库小学妹3 小时前
数据库运维五级成熟度:监控、备份、高可用怎么串起来
运维·数据库·监控