如何使用cloud-init和用户数据将配置ssh公钥登录?

计算操作系统技术服务知识库
问题描述

我要在VeLinux上配置一个用户的公钥登录,如何使用cloud-init 和用户数据完成?

问题分析

要完成配置,执行以下操作: 1.创建新用户。 2.将 SSH 公钥附加到 authorized_keys 文件。

解决方案

1、使用SSH连接到实例。 2、运行以下命令,查看cloud-init是否安装,如下:

root@i-2udbbtytcx3gv6bvmhzb:~# apt list | grep cloud-init

cloud-init/oldstable 20.2-2~deb10u2 all
cloud-initramfs-dyn-netconf/oldstable 0.18.debian7 all
cloud-initramfs-growroot/oldstable 0.18.debian7 all
cloud-initramfs-rescuevol/oldstable 0.18.debian7 all

3、创建新用户,如下:

root@i-2udbbtytcx3gv6bvmhzb:~# adduser test
Adding user `test' ...
Adding new group `test' (1001) ...
Adding new user `test' (1001) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password: 

4、生成ssh所需公钥、私钥,如下:

root@iv-ybmpz656q12udbnkjbmr:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
...

5、进入ECS控制台,找到要配置的实例,依次选择右上角...-实例设置-更改自定义数据,然后将如下内容粘贴:

#cloud-config
cloud_final_modules:
- [users-groups,always]
users:
  - name: test
    groups: [ wheel ]
    sudo: [ "ALL=(ALL) NOPASSWD:ALL" ]
    shell: /bin/bash
    ssh-authorized-keys: 
    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAA

将name后修改为您新建的用户,将ssh—rsa后修改为您生成的公钥内容,默认为:/root/.ssh/id_rsa.pub。 6、进行测试,如下:

root@iv-ybmpz656q12udbnkjbmr:~/.ssh# ssh -i id_rsa test@192.168.xx.xx
....
test@i-2udbbtytcx3gv6bvmhzb:~$ 

如果您有其他问题,欢迎您联系火山引擎技术支持服务

316
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论