Chapter 7. Configuring authentication by using cloud-init
To manage users, access rights, and passwords in a virtual machine (VM), you can use the cloud-init utility. You can create users, configure sudo access, set root passwords, and force password changes at first login.
-
Create and describe users in a
userssection. If you add theuserssection, you must also set the default user options in that section. You can modify the section to add more users to the initial system configuration, and also set additional user options. -
Configure a user as a
sudoerby adding asudoandgroupsentry to theuserssection. -
Configure the user data so that only you have a
rootuser access. -
Force
cloud-userto change thecloud-userpassword at the first login to reset the password. -
Set the
rootpassword by creating a user list.
Prerequisites
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll 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
To add users and user options:
By default, users are labeled as
unconfined_uif there is not anselinux-uservalue.NoteThis example places the user
user2into two groups:usersandwheel.Add or modify the
userssection to add users. For example:#cloud-config users: - default - name: user2 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:mypassword user2:mypassword2 expire: False-
If you want
cloud-userto be the default user created along with the other users you specify, ensure that you adddefaultas the first entry in the section. If it is not the first entry,cloud-useris not created.
To add a
sudouser to the users list:-
Add a
sudoentry and specify the user access. For example,sudo: ALL=(ALL) NOPASSWD:ALLallows a user unrestricted user access. Add a
groupsentry and specify the groups that include the user:#cloud-config users: - default - name: user2 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:mypassword user2:mypassword2 expire: False
-
Add a
To configure an exclusive
rootaccess for a user:Create an entry for the user
rootin theuserssection by modifying thenameoption:users: - name: root chpasswd: list: | root:password expire: FalseOptional: Set up SSH keys for the
rootuser:users: - name: root ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA..vz user@domain.com
To change the default
cloud-inituser name, follow:Add the line
user: <username>, replacing <username> with the new default user name:#cloud-config user: username password: mypassword chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvz user1@yourdomain.com - ssh-rsa AAB...QTuo user2@yourdomain.com
To reset a password for a new user:
Change the line
chpasswd: {expire: False}tochpasswd: {expire: True}:#cloud-config password: mypassword chpasswd: {expire: True} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...SDvz user1@yourdomain.com - ssh-rsa AAB...QTuo user2@yourdomain.comNote-
This works to expire the password because
passwordandchpasswdoperate on the default user unless you indicate otherwise. -
This is a global setting. When you set
chpasswdtoTrue, all users you create need to change their passwords when they log in.
-
This works to expire the password because
To set a root password:
Create a user list in the
chpasswdsection:NoteWhite space is significant. Do not include white space before or after the colon in your user list. If you include white space, the password is set with a space in it.
#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:myrootpassword cloud-user:mypassword expire: FalseNoteIf you use this method to set the user password, you must set all passwords in this section.