Use Cloud-Init when creating/ordering new Cloud-Servers

  • Sunday, 27th October, 2019
  • 09:01am

In some situations, you will need to launch a script when you create an instance. For example, you may want to do this if you need to configure multiple SSH keys for your instance, or configure your SSH service automatically.

There are several different scripts that are useful for you to launch when you create an instance. For example, you can use shell scripts.

With this script, you can create a user named “tux“. Then we give this user sudo access, and add their SSH key:

#!/bin/bash

adduser tux -gecos "" --disabled-password
echo "ovh ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

mkdir /home/tux/.ssh
echo "YOUR_PUBLIC_SSH_KEY" > /home/tux/.ssh/authorized_keys

It is also possible to run cloud-config script to achive this:

#cloud-config

users:
  - name: tux
    groups: sudo
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh-authorized-keys:
      - SSH_KEY
      

For more examples see https://help.ubuntu.com/community/CloudInit and http://cloudinit.readthedocs.io/en/latest/topics/examples.html

 

« Back