文章目录免密SSH登录Ubuntu1 安装openssh-server2 启动SSH服务3 生成公私钥对3.1 在客户端生成公私钥对3.2 在服务端生成公私钥对4 拷贝客户端公钥到服务端内5 远程免密登录免密SSH登录Ubuntu1 安装openssh-server在ubuntu内安装sshserver包:pzspzs-jammy:~$sudoapt-getinstall-yopenssh-server2 启动SSH服务在Ubuntu内启动SSH服务pzspzs-jammy:~$sudosystemctl start sshd# 或者使用如下命令启动:pzspzs-jammy:~$sudoservicesshd start3 生成公私钥对3.1 在客户端生成公私钥对假设我们的客户端是windows, 我们需要打开cmd命令窗口并执行如下命令来运行C:\Users\a2647ssh-keygen Generating public/private ed25519 key pair. Enterfileinwhichto save the key(C:\Users\a2647/.ssh/id_ed25519): y# 输入y然后回车Enter passphrase(emptyforno passphrase):# 回车Enter same passphrase again:# 回车Your identification has been savediny Your public key has been savediny.pub The key fingerprint is: SHA256:pj..... a2647pzs The keys randomart image is: --[ED25519256]--|o .o..|....----[SHA256]-----之后就会生成C:\Users\a2647/.ssh/id_ed25519.pub公钥和C:\Users\a2647/.ssh/id_ed25519(私钥)3.2 在服务端生成公私钥对pzspzs-jammy:~$ ssh-keygen Generating public/private rsa key pair. Enterfileinwhichto save the key(/home/pzs/.ssh/id_rsa):# 回车Created directory/home/pzs/.ssh.Enter passphrase(emptyforno passphrase):# 回车Enter same passphrase again:# 回车Your identification has been savedin/home/pzs/.ssh/id_rsa Your public key has been savedin/home/pzs/.ssh/id_rsa.pub The key fing... pzspzs-jammy The keys randomart image is: ---[RSA3072]----|...||.o.|... ----[SHA256]-----之后就会生成$HOME/.ssh/id_rsa.pub公钥和$HOME/.ssh/id_rsa(私钥)4 拷贝客户端公钥到服务端内拷贝服务端C:\Users\a2647/.ssh/id_ed25519.pub追加到虚拟机 的~/.ssh/authorized_keys内pzspzs-resolute:~$touch~/.ssh/authorized_keys# 注意不要使用sudo命令创建否则会导致免密操作失效pzspzs-resolute:~$chmod700~/.ssh pzspzs-resolute:~$chmod600~/.ssh/authorized_keys pzspzs-resolute:~$vim~/.ssh/authorized_keys# 添加服务端的.ssh\id_ed25519.pub的内容到里面5 远程免密登录之后就可以通过服务端的hostname和IP地址进行免密远程登录了操作如下C:\Users\a2647ssh pzs192.168.20.128 The authenticity ofhost192.168.20.128 (192.168.20.128)cant 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 Ubuntu22.04.5 LTS(GNU/Linux6.8.0-138-generic x86_64)* Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro Expanded Security MaintenanceforApplications is not enabled.0updates can be applied immediately. Enable ESM Apps to receive additional future security updates. See https://ubuntu.com/esm or run:sudopro status The programs included with the Ubuntu system arefreesoftware;the exact distribution termsforeach program are describedinthe individual filesin/usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. pzspzs-jammy:~$# 没有输入密码就登录到ubuntu系统里面了