Configure system settings by using cloud-init
You can use cloud-init to configure various system settings during the initial boot process of your Red Hat Enterprise Linux (RHEL) virtual machine or cloud instance.
It includes setting up authentication methods, managing Red Hat subscriptions, configuring static networking, setting up container storage, running custom shell scripts, and changing system locale settings. With these configuration options, you can automate the setup of your RHEL systems in cloud environments, ensuring consistent and repeatable deployments.
Overview of authentication configuration in cloud-init Copy linkLink copied!
To manage users, configure access rights and sudo privileges, set or reset the root password, and require password changes at first login for virtual machine (VM), you can use the cloud-init utility
Procedure Copy linkLink copied!
Red Hat subscription management with cloud-init Copy linkLink copied!
To register your system with Red Hat Subscription Management, you can use the rh_subscription directive in cloud-init. Configure the directive with username and password, activation key and organization, or a custom server hostname.
- Using the
defaultoption -
Under
rh_subscription, add yourusernameandpassword.rh_subscription: username: <example@redhat.com> password: <example_password> - Using the
activation-keyandorgoptions -
Under
rh_subscription, add youractivation keyandorgnumber.rh_subscription: activation-key: <example_key> org: <example_id> - Using the
server-hostnameoption -
You can set a server hostname in the
/etc/rhsm/rhsm.conffile. Underrh_subscription, add yourusername,password, andserver-hostname.rh_subscription: username: <example@redhat.com> password: <example_password> server-hostname: <test.example.com>
Configure a static networking configuration by using cloud-init Copy linkLink copied!
To configure network configuration with cloud-init, add a network-interfaces section to the metadata. With this setting, you can configure static IP addresses, gateways, and other network settings.
Before you begin Copy linkLink copied!
- You have the default networking service provided by
NetworkManager, which manages network devices and connections dynamically on Red Hat Enterprise Linux (RHEL). - You have confirmed whether your datasource provides a network configuration. For more information, you have consulted the relevant
cloud-initdocumentation section. - You have not specified a custom network configuration for
cloud-initor disabled network configuration, allowingcloud-initto detect attached devices and auto-generate a Dynamic Host Configuration Protocol (DHCP)-based configuration if an active connection exists. - Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:Note All user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.
Procedure Copy linkLink copied!
Results Copy linkLink copied!
- Verify that the static IP address is configured on the interface:
# ip addr show eth0inet 192.0.2.1/24 brd 192.0.2.255 scope global eth0If the output displays the static IP address
192.0.2.1/24, the network configuration applied successfully. - Optional: Verify that the gateway is reachable by pinging it:
# ping -c 1 192.0.2.254If the ping succeeds, the gateway configuration is working correctly.
Configure container storage by using cloud-init Copy linkLink copied!
You can configure container storage by referencing the container-storage-setup utility within the write_files module of cloud-init.
Before you begin Copy linkLink copied!
- Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:Note All user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.
Procedure Copy linkLink copied!
write_files module to include the path to the container-storage-setup utility:
write_files:
- path: /etc/sysconfig/docker-storage-setup
permissions: 0644
owner: root
content: |
ROOT_SIZE=6G
This example sets the size of the root logical volume to 6 GB rather than the default 3 GB.
Results Copy linkLink copied!
- Verify that the container storage is configured correctly:
# docker info | grep "Docker Root Dir"Docker Root Dir: /var/lib/dockerIf the output displays the container storage is configured correctly.
Shell scripts with cloud-init Copy linkLink copied!
You can add list values or string values to bootcmd or runcmd in cloud-init. You can also provide a shell script within userdata to run custom commands during VM initialization.
- If you use a string value, the entire string runs as a shell script.
- If you use a list value for
bootcmdorruncmd, each list item runs in turn usingexecve. - If you want to use
cloud-initto run a shell script, provide a shell script (complete with shebang#!). Use this instead of providingcloud-initwith a.yamlfile.
Change the system locale with cloud-init Copy linkLink copied!
You can configure the system locale with the locale module in cloud-init by adding the locale directive to your configuration.
Before you begin Copy linkLink copied!
- Depending on the requirements of your datasource, edit the
meta-datafile. You can also add the following directive to thecloud.cfgfile or thecloud.cfg.ddirectory.
Procedure Copy linkLink copied!
locale directive, specifying the location. The following sample sets the locale to ja_JP (Japan) with Unicode character encoding (UTF-8):
# vi /etc/cloud/cloud.cfg
#cloud-config
locale: ja_JP.UTF-8
Results Copy linkLink copied!
- Verify that the
localeis set correctly:# localeThe output should show the
localeis set toja_JP.UTF-8.