win11设置SSH免密登录ubuntu服务器
" class="reference-link" href="#">
(一)在windows端生成密钥
===================
本地输入命令:
<pre class="prism-highlight prism-language-bash">ssh-keygen
本地输出一些内容,多按几次enter执行下一步:
<pre class="prism-highlight prism-language-bash">C:\Users\LG\DOCUME~1\MOBAXT~1\home>ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/mobaxterm/.ssh/id_rsa): Created directory '/home/mobaxterm/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/mobaxterm/.ssh/id_rsa. Your public key has been saved in /home/mobaxterm/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX LG@LG-YGZone The key's randomart image is: +---[RSA 2048]----+ | E=. | | .XXXXX o | | o... . + | | .oXXXo | | .o+oS | | XXXXX=+o | | . o.oB... | | + XXXXXX..+ | | . o+X*==*o. | +----[SHA256]-----+
直到输出的内容和上部分差不多,说明密钥已经生成完毕,进入密钥对目录:
<pre class="prism-highlight prism-language-bash">cd ~/.ssh/
可以看到已经生成密钥:
(二)拷贝密钥到服务器
==============
ubuntu服务器输入命令创建ssh公钥保存文件夹,按下enter可以直接创建默认目录:
<pre class="prism-highlight prism-language-bash">ssh-keygen -t rsa
输出如下,表明文件夹已经创建好了:
<pre class="prism-highlight prism-language-bash">Generating public/private rsa key pair. Enter file in which to save the key (/home/ygzone/.ssh/id_rsa): Created directory '/home/ygzone/.ssh'. Enter passphrase (empty for no passphrase):
在windows打开当前用户下的.ssh文件夹,注意此文件夹可能为隐藏文件夹,在powershell中执行此命令添加指令 <span style="color: #C7254E; font-family: Consolas, Inconsolata, Courier, monospace; background-color: #F9F2F4;">ssh-copy-id</span> :
<pre class="prism-highlight prism-language-bash">function ssh-copy-id([string]$userAtMachine, $args){ $publicKey = "$ENV:USERPROFILE" + "/.ssh/id_rsa.pub" if (!(Test-Path "$publicKey")){ Write-Error "ERROR: failed to open ID file '$publicKey': No such file" } else { & cat "$publicKey" | ssh $args $userAtMachine "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" }}
在powershell中使用新建的命令导入公钥到ubuntu服务器:
<pre class="prism-highlight prism-language-bash">ssh-copy-id username@ip
然后会被要求输入ubuntu服务器密码,导入完成。
在windows服务器的~/.ssh/config文件中追加以下内容,这样可以在windows端可以使用别名登录.
<pre class="prism-highlight prism-language-c">Host MSI-GL62(别名) HostName ip地址 User zygone(用户名) Port 22(端口号)
(三)测试ssh登录
登录测试,cmd中输入:
<pre class="prism-highlight prism-language-bash">ssh MSI-GL62
返回:
<pre class="prism-highlight prism-language-bash">Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.15.0-58-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 82 更新可以立即应用。 要查看这些附加更新,请运行:apt list --upgradable New release '22.04.1 LTS' available. Run 'do-release-upgrade' to upgrade to it. 2 updates could not be installed automatically. For more details, see /var/log/unattended-upgrades/unattended-upgrades.log Your Hardware Enablement Stack (HWE) is supported until April 2025. Last login: Sun Feb 5 16:59:22 2023 from 192.168.0.105 ygzone@YGZone-GL62M-7RD:~$
SSH免密设置成功。
版权声明
本文仅代表作者观点。
本文系作者发表,未经许可,不得转载。
上一篇:ubuntu配置ftp服务器 下一篇:Ubuntu-ARM配置软件源
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。