base64
format via user_data
in the payload request.
Let’s say you want to add the User Data field as below:
base64
format. You can use the base64
command line tool to do so.
base64
result should look similar to this:
user_data
field provided in the payload to create a new VM:
base64
format first, then provide the encoded value to the user_data
field in the Terraform definition file as below:
cloud-init
, an industry-standard multi-distribution method for cross-platform cloud VM initialization.
Cloud-init
is an industry-standard software for automating the initialization of cloud Virtual Machines. You can use cloud-init
to perform a number of initial tasks when your VM is booted for the first time, such as installing packages, running a shell script, or configuring the network interfaces.
Cloud-init
supports all major Linux distributions:
ssh_pwauth: True
in the User Data field. Below is an example configuration of how to set your Virtual Machine to be accessible using a default username and password.
password
field sets the password value for your Virtual Machine’s default username. Depending on the VM image, the username may vary. For example, if you create a Virtual Machine using the Ubuntu distro, your default username will be ubuntu
chpasswd: { expire: False }
configuration, you do not need to change the password of your default user after you access the VM
ssh_pwauth: True
allows your Virtual Machine to be accessible using a username and password
power_state
block option tells cloud-init
to wait 30 seconds for the cloud-init
process to finish before shutting down the system
guest
with the same password as the first user. Here’s the example of a cloud-init
script for creating this guest
user:
sudo
: Gives superuser rights to the guest user by setting the value ALL=(ALL) NOPASSWD:ALL
.
passwd
: Provides an encrypted value of your password according to the SHA-512 encryption method. To generate the SHA-512 encrypted value, you can use the mkpasswd
command line tool:
groups
: Lists all groups the guest
user will belong to. In this configuration, the user will be automatically added to the users
and admin
groups. If you want to add the user to other groups, first make sure that these groups exist.
lock_passwd
: When set to false
, this value allows you to access the Virtual Machine with a username and password.
shell
: When set to bin/bash
, this value defines the default shell as bash
for the guest
user.
guest
user to access the VM:
1. Run the following command: ssh guest@your_instance_ip
.
2. Type guest
and press Enter.
You should now be able to log in to your guest user account.
guest
user belongs to, run the command groups
. You should see the users
and admin
groups listed, indicating that the guest
user belongs to the users
and admin
groups.regular-users
by adding the below line to the User Data field:
regular-users
group should be displayed at the bottom of the result.
authorized_keys
by running the below command:
nmap
and traceroute
to debug your application network.
Adding the below configuration into your User Data field helps you install the docker
, nmap
, and traceroute
tools when the VM boots up.
nmap
and traceroute
, you only need to include them in the packages
block. However, with packages that are not available in the default instance repository— like docker-ce
and docker-ce-cli
—you need to add the Docker repository first using apt sources
.
After installing, restart the Virtual Machine so that these packages are added to the system path. Including the power_state
block at the end of the User Data field is helpful because it allows you to use these tools as soon as the Virtual Machine is ready.
useful-docker-script.yaml
in the /root
directory.
useful-docker-script.yaml
has been created:
useful-docker-script.yaml
.
cloud-init
will generate a new file named 50-cloud-init.yaml
in the /etc/netplan
directory and create a new network interface based on that file. To add a static IP address to the network interface, you need to create another file in the /etc/netplan
other than 50-cloud-init.yaml
. When cloud-init
creates the network interface, it will try to merge the content of these two configuration files. As a result, the static IP address will be added to the network interface. Below is an example content of the 50-cloud-init.yaml
file.
enp3s0
network interface, you can add the below content to the User Data field.
Cloud-init
will execute two steps based on this configuration.
cloud-init
will create a new file named 00-add-static-ip.yaml
in /etc/netplan
with instructions to add static IP addresses for both IPv4 (192.170.1.25) and IPv6 (2020:1::1.) The permission of the file is 0644, which means it is readable by any user and writable by cloud-init
.
power_state
block configuration), cloud-init
will try to merge the network configuration for both 00-add-static-ip.yaml
and 50-cloud-init.yaml
files so that you can add static IP addresses to the enp3s0
network interface.
192.170.1.25
and for IPv6 it is 2020:1::1
.
cloud-init
, you can check the logs to identify the problem. There are two main log files on your instances.
cloud-init
log is located at /var/log/cloud-init.log
and contains detailed information about the cloud-init
process, including any errors that occur.
To view the cloud-init
log, run the following command:
cloud-init-output
log is located at /var/log/cloud-init-output.log
and contains the output of the scripts the cloud-init
process runs.
To view the cloud-init-output
log, run the following command: