第 15 章 Managing users, groups, SSH keys, and secrets in image mode for RHEL
Learn more about users, groups, SSH keys, and secrets management in image mode for RHEL.
15.1. Users and groups configuration 复制链接链接已复制到粘贴板!
Image mode for RHEL is an update and configuration mechanism for generic operating system. For best practices in selected use cases, see the following list:
- Users and groups configuration for generic base images
- Usually, the distribution base images do not have any configuration. Do not encrypt passwords and SSH keys with publicly-available private keys in generic images because of security risks.
- Injecting SSH keys through
systemdcredentials -
You can use
systemdto inject a root password or SSHauthorized_keysfile in some environments. For example, use System Management BIOS (SMBIOS) to inject SSH keys system firmware. You can configure this in local virtualization environments, such asqemu. - Injecting users and SSH keys by using
cloud-init -
Many Infrastructure as a service (IaaS) and virtualization systems use metadata servers that are commonly processed by software such as
cloud-initorignition. See AWS instance metadata. The base image you are using might includecloud-initor Ignition, or you can install it in your own derived images. In this model, the SSH configuration is managed outside of the bootc image. - Adding users and credentials by using container or unit custom logic
-
Systems such as
cloud-initare not privileged. You can inject any logic you want to manage credentials in the way you want to launch a container image, for example, by using asystemdunit. To manage the credentials, you can use a custom network-hosted source, for example, FreeIPA. - Adding users and credentials statically in the container build
In package-oriented systems, you can use the derived build to inject users and credentials by using the following command:
RUN useradd someuserYou can find issues in the default
shadow-utilsimplementation ofuseradd: Users and groups IDs are allocated dynamically, and this can cause drift.- User and group home directories and
/vardirectory For systems configured with persistent
/home, any changes to/var/home /varmade in the container image after initial installation will not be applied on subsequent updates.For example, if you inject
/var/home/someuser/.ssh/authorized_keysinto a container build, existing systems do not get the updatedauthorized_keysfile.- Using DynamicUser=yes for
systemdunits Use the
systemdDynamicUser=yesoption where possible for system users.This is significantly better than the pattern of allocating users or groups at package install time, because it avoids potential UID or GID drift.
- Using
nss-altfiles The
nss-altfilessplits system users into/usr/lib/passwdand/usr/lib/group, aligning with the way the OSTree project handles the 3 way merge for/etcas it relates to/etc/passwd. Currently, if the/etc/passwdfile is modified in any way on the local system, then subsequent changes to/etc/passwdin the container image are not applied.Base images built by
rpm-ostreehavenss-altfilesenabled by default.Also, base images have a system users pre-allocated and managed by the NSS file to avoid UID or GID drift.
In a derived container build, you can also append users to
/usr/lib/passwd, for example. Usesysusers.dorDynamicUser=yes.- Using
systemd-sysusers Use
systemd-sysusers, for example, in your derived build. For more information, see thesystemd-sysusers documentation.COPY mycustom-user.conf /usr/lib/sysusers.dThe
sysuserstool makes changes to the traditional/etc/passwdfile as necessary during boot time. If/etcis persistent, this can avoidUIDorGIDdrift. It means that theUIDorGIDallocation depends on how a specific machine was upgraded over time.- Machine-local state for users
The filesystem layout depends on the base image.
By default, the user data is stored in both
/etc,/etc/passwd,/etc/shadowandgroups, and/home, depending on the base image. However, the generic base images have to both be machine-local persistent state. In this model/homeis a symlink to/var/home/user.- Injecting users and SSH keys at system provisioning time
For base images where
/etcand/varare configured to persist by default, you can inject users by using installers such as Anaconda or Kickstart.Typically, generic installers are designed for one time bootstrap. Then, the configuration becomes a mutable machine-local state that you can change in Day 2 operations, by using some other mechanism.
You can use the Anaconda installer to set the initial password. However, changing this initial password requires a different in-system tool, such as
passwd.These flows work equivalently in a
bootc-compatiblesystem, to support users directly installing generic base images, without requiring changes to the different in-system tool.- Transient home directories
Many operating system deployments minimize persistent, mutable, and executable state. This can damage user home directories.
The
/homedirectory can be set astmpfs, to ensure that user data is cleared across reboots. This approach works especially well when combined with a transient/etcdirectory.To set up the user’s home directory to, for example, inject SSH
authorized_keysor other files, use thesystemdtmpfiles.dsnippets:f~ /home/user/.ssh/authorized_keys 600 user user - <base64 encoded data>SSH is embedded in the image as:
/usr/lib/tmpfiles.d/<username-keys.conf. Another example is a service embedded in the image that can fetch keys from the network and write them. This is the pattern used bycloud-init.- UID and GID drift
-
The
/etc/passwdand similar files are a mapping between names and numeric identifiers. When the mapping is dynamic and mixed with "stateless" container image builds, it can cause issues. Each container image build might result in the UID changing due to RPM installation ordering or other reasons. This can be a problem if that user maintains a persistent state. To handle such cases, convert it to usesysusers.dor useDynamicUser=yes.