Due to a specific design requirements on a project I've been working on recently I needed to create a consistent system account on all the Linux servers across all the environments. This account supposed to have the following characteristics:
- Same GID and UID
- Both password and public SSH key are set
- Only the SSH key-based authentication is allowed
- Privileged operations are executed without having to provide password (optional)
As you can tell this is an admin type account that is usually created just after the installation of an operating system.
Servers are up and running. Setting it up by hand could be an option. However, this would be a very old-fashioned approach though. I already have an automation tool in place. My favorite one is Ansible due to its simple architecture. Whether or not our configuration management framework is really advanced and facilitates tweaking of every aspect of an OS or a VM in my opinion the best language to script it is Bash. The change will be expressed in a most readable way that every sysadmin or platform engineer as they are called these days will understand. I'm going to use Ansible only as an orchestration tool to ship the script across and execute it without any user interaction.
Here is our sample input:
But before we create such a user let's check if the ID that we intent to use is available. If it is already taken we need to find the current user, terminate all his processes, change the UID and GID, then fix file ownership accordingly.
Now, we are ready to create a new system user.
SSH key-based authentication provides cryptographic strength that even extremely long passwords cannot offer. So, this will be our preferred method of granting access to a system and ...
... we make sure the above is the only method of authentication by changing the OpenSSL daemon configuration.
The last thing we may want to do is to allow privileged operations without password. This may come handy while the same system account is used to perform other automated tasks that normally would prompt for a user password.
There we have it. A simple way to test it could be by loading the private key locally using the
ssh-add command and trying to
ssh into the server.
In my next blog post I will show how to create a minimal Ansible orchestration script.