Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Ubuntu Login via a ssh key

Recommended Posts

本文记录通过 ssh key 登录 Ubuntu 的设置。

安装 openssh server

sudo apt-get install openssh-server

查看运行状态

sudo service ssh status

或者

sudo systemctl status ssh.service

执行

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntus/.ssh/id_rsa): /home/ubuntus/d          
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/ubuntus/.ssh/id_rsa.
Your public key has been saved in /home/ubuntus/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:J+l/vCIK1E40M9lkqd/9ycuhOF1fQ4CjFimSZYkFAC8 ubuntus@ubuntuS
The key's randomart image is:
+---[RSA 2048]----+
| .....*++.. .    |
|  .  +.B.o o .   |
| E .  B.o o . .  |
|  .  o.+ +     . |
|    . o.S...  .  |
|   . o ..o. . ...|
|    . . .  o +.oo|
|     .  ..o.+o+..|
|      .. .o+o.o. |
+----[SHA256]-----+

然后

cd ~/.ssh

cat id_rsa.pub >> authorized_keys

chmod 600 authorized_keys

chmod 700 ~/.ssh

然后修改下 SSH 的配置

先备份

sudo cp /etc/ssh/sshd_config /etc//ssh/sshd_config.default

 

sudo gedit /etc/ssh/sshd_config

或者

sudo nano /etc/ssh/sshd_config

这个图是针对 Ubuntu 16.04 的,下面有针对 Ubuntu 18.04 的。

2017-12-06 04-06-07屏幕截图.png

主要是下面2个

PermitRootLogin yes

AuthorizedKeysFIle 前面的注释去掉。

针对 Ubuntu 18.04 LTS ,需要修改以下项:

PermitRootLogin yes
StrictModes yes
PubkeyAuthentication yes
PasswordAuthentication no

 

检查下 

sudo sshd -t

然后重启 SSH 服务

sudo service ssh restart

或者

sudo systemctl restart ssh.service

如果提示没找到 ssh.service,那就装下

sudo apt install ssh
sudo service ssh restart

然后把私钥下载 id_rsa 复制到可见的目录下,比如说用户名是 ubuntus, 那就在自己的主目录下新建个 backup 目录,将 id_rsa 复制过来,并更改下拥有者

cd ~
mkdir backup
cp ~/.ssh/id_rsa ~/backup
cd ~/backup
sudo chown uuu id_rsa

然后把它下载到自己的电脑上。用 PUTTY 的转换工具 PuttyGen ,点击 COversions,载入id_rsa,输入生成时的“密码”, 生成 ppk 格式的私钥, 点击 Save private key ,保存密钥,就可以通过 WinSCP 或者 PuTTY 使用了:

2910.png

在 WinSCP 中新建站点,输入IP(可在 linux 上执行 ifconfig 查看它在局域网内的IP),点“高级”,SSH->验证中选中 ppk :

1.png

确定,保存,输入密钥的用户名和在 Ubuntu 上生成时的“密码”,即可连接:

2909.png

隐藏内容
回复可见

Share this post


Link to post
uuu@uuu:~/下载$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/uuu/.ssh/id_rsa): 
Created directory '/home/uuu/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/uuu/.ssh/id_rsa.
Your public key has been saved in /home/uuu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jMZ/0xR1cpfqoUQs+zH/BLu7puuUqhTbzeYY+qvF360 uuu@uuu
The key's randomart image is:
+---[RSA 2048]----+
|          .   o =|
|         . o . =.|
|          + . .  |
|     . o . + =   |
|      +.S o B +  |
|     . .= o=.+ . |
|       o.=o*. +  |
|      . o.O..o.. |
|       ++=o*+E+. |
+----[SHA256]-----+

 

Share this post


Link to post
Guest
This topic is now closed to further replies.
Sign in to follow this  

×