Frequently Asked Questions about cloud-init
Request a new translation or translation update
A translation for your language preference does not exist. Click below to request a translation for your selected language.
Red Hat Enterprise Linux Atomic Host uses cloud-init to configure the system during installation and first-boot. Cloud-init was initially developed to provide early initialization of cloud instances. In Red Hat Enterprise Linux Atomic Host it can also be used for virtual machine installations. Upstream documentation is available online.
The files used by cloud-init are YAML formatted files. For additional information please see YAML in a Nutshell.
NOTE: cloud-init is run only the first time that the machine is booted. If cloud-init fails because of syntax errors in the file or doesn't contain all of the needed directives, such as user credentials, a new instance must be created and launched. Restarting the failed instance with a new cloud-init file will not work.
Below are examples of how to do common tasks with cloud-init.
-
How do I create users with cloud-init?
To create users with cloud-init, you must create two files: meta-data and user-data, and then package them into an ISO image.
-
Make a directory and move into it:
$ mkdir clouinitiso $ cd cloudinitiso -
Create a file called meta-data. Add the following to the file called meta-data:
instance-id: Atomic0 local-hostname: atomic-00 -
Create a file called user-data. Add the following to the file called user-data:
#cloud-config password: atomic chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvZ user1@domain.com
Note: The final line of the user-data file above is an ssh public key. SSH public keys are found in ~/.ssh/id_rsa.pub.
-
Create an ISO image that includes meta-data and user-data:
# genisoimage -output atomic0cidata.iso -volid cidata -joliet -rock user-data meta-data -
A file named atomic0cidata.iso is generated. Attach this file to the machine on which you plan to install Red Hat Enterprise Linux Atomic Host, and your username will be "cloud-user" and your password will be "atomic".
-
-
How do I expire the cloud-user's password so that the user must change it during their first login?
To force cloud-user to change their password at first login, change the line
chpasswd: {expire: False}tochpasswd: {expire: True}in the user-data file.#cloud-config password: atomic chpasswd: {expire: True} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvz user1@yourdomain.com - ssh-rsa AAB...QTuo user2@yourdomain.comThis works because the password and chpasswd operate on the default user unless otherwise indicated.
Note: This is a global setting. If you set this to True all users who are created (see below) will have to change their password.
-
How do I change the default username?
To change the default username from cloud-user to something else, add the line
user: usernameto the user-data file:#cloud-config user: username password: atomic chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvz user1@yourdomain.com - ssh-rsa AAB...QTuo user2@yourdomain.com -
How do I set the root password?
To set the root password you must create a user list in the
chpasswdsection of the user-data file. The format of the list is shown below. Whitespace is significant, so do not include any on either side of the colon (:) as it will set a password with a space in it. If you use this method to set the user passwords, all passwords must be set in this section. This means that thepassword:line must be moved from the top and into this section.#cloud-config ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvz user1@yourdomain.com - ssh-rsa AAB...QTuo user2@yourdomain.com chpasswd: list: | root:password cloud-user:atomic expire: False -
How do I add more users during initial system configuration? How do I set additional user options?
Users are created and described in the users section of the user-data file. Adding this section requires that options for the default user be set here as well.
If the first entry in the users section is
default, the default user, cloud-user will be created along with the other users. If the default line is omitted, then cloud-user is not created.#cloud-config users: - default - name: foobar gecos: User N. Ame selinux-user: staff_u groups: users,wheel ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA..vz user@domain.com chpasswd: list: | root:password cloud-user:atomic foobar:foobar expire: FalseNote: By default users will be labeled as unconfined_u if there is not an se-linux-user value.
Note: This example places the user foobar into two groups:
usersandwheel. As of cloud-init 0.7.5, no whitespace is supported in the group list. Bug #1126365. -
How do I run first boot commands?
The
runcmdandbootcmdsections of the user-data file can be used to execute arbitrary commands during startup and initialization. Thebootcmdsection is run early in the initialization process. Theruncmdsection is executed near the end of the process by init. These commands are not saved for future boots and will only be executed during the first initialization-boot.#cloud-config users: - default - name: foobar gecos: User N. Ame groups: users chpasswd: list: | root:password fedora:atomic foobar:foobar expire: False bootcmd: - echo New MOTD >> /etc/motd runcmd: - echo New MOTD2 >> /etc/motd -
How do I add additional sudoers?
A user can be configured as a sudoer by adding a
sudoandgroupsentry to theuserssection of the user-data file, as shown below.#cloud-config users: - default - name: foobar gecos: User D. Two sudo: ["ALL=(ALL) NOPASSWD:ALL"] groups: wheel,adm,systemd-journal ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA...vz user@domain.com chpasswd: list: | root:password cloud-user:atomic foobar:foobar expire: False -
How do I set up a static networking configuration?
Add a
network-interfacessection to the meta-data file. This section contains the usual set of networking configuration options.Because of a current bug in cloud-init, static networking configurations are not automatically started. Instead the default DHCP configuration remains active. A suggested work around is to manually stop and restart the network interface via the bootcmd directive.
network-interfaces: | iface eth0 inet static address 192.168.1.10 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.254 bootcmd: - ifdown eth0 - ifup eth0 -
How do I delete cloud-user and just have root and no other users?
To have only a root user created, create an entry for root in the
userssection of the user-data file. This section can be as simple as just anameoption:users: - name: root chpasswd: list: | root:password expire: FalseOptionally, you can set up SSH keys for the root user as follows:
users: - name: root ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA..vz user@domain.com -
How do I re-run cloud-init on an instance?
In most situations it is not possible to re-run cloud-init to change the configuration of a virtual machine that has already been created.
When cloud-init is used in an environment where the Instance ID can be changed (for instance, from Atomic0 to Atomic1), it is possible to re-configure an existing virtual machine by changing the Instance ID and rebooting to re-run cloud-init. This is not recommended practice for production environments because cloud-init is supposed to be set up to create on first boot systems that are fully and properly configured.
In most IAAS implementations it is not possible to change the Instance ID. If cloud-init must be re-run, the instance should be cloned in order to obtain a new Instance ID.
-
Can I put shell scripts in bootcmd and runcmd?
Yes. If you use a list value for bootcmd or runcmd, each list item is run in turn using execve. If you use a string value, then the entire string is run as a shell script. Alternatively, if you want simply to use cloud-init to run a shell script, you can provide a shell script (complete with shebang (#!) ) instead of providing cloud-init with a .yaml file. See this website for examples of how to put shell scripts in bootcmd and runcmd.
This solution is part of Red Hat's fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.