1. Overview
Linux users can create and manage their own accounts. These accounts are identified by usernames. However, it’s usually advisable not to start Linux usernames with numbers.
In this tutorial, we’ll understand the naming convention in Linux. Next, we’ll see why it’s better to avoid using numbers as a prefix to usernames.
2. User Naming Convention in Linux
A username is a string of characters identifying a user on a Linux system. Yet, there are some restrictions on forming and validating a username.
2.1. adduser Rules
The adduser command checks if a username is valid by using a regular expression.
In particular, the configuration file at /etc/adduser.conf has two variables that control how usernames are created:
- NAME_REGEX: for normal users
- NAME_REGEX_SYSTEM: for system users
Both variables use regular expressions to specify the rules for valid user names.
By default, NAME_REGEX and NAME_REGEX_SYSTEM variables are set to ^[a-z][-a-z0-9_]*\$? which specifies that the username:
- starts with a lowercase letter
- contains only lowercase letters, digits, underscores, hyphens, and periods
- optionally ends with a dollar sign
This way, adduser restricts usernames starting with a digit.
2.2. Creating Users
For instance, we can create a user with the adduser command and sudo:
$ sudo adduser baeldung
This command creates a user named baeldung. In the process, the system prompts us to enter a password for the new user. In addition, we may enter some extra information like the user’s full name and phone number.
After creating user baeldung, let’s also create a user named 55:
$ sudo adduser 55
adduser: Please enter a username matching the regular expression configured
via the NAME_REGEX[_SYSTEM] configuration variable. Use the `--force-badname'
option to relax this check or reconfigure NAME_REGEX.
Since the username 55 isn’t valid according to the default rules, adduser shows an error message.
2.3. System Users
Furthermore, system usernames are only for users that run system services or processes:
- root
- bin
- daemon
Also, system users are different from normal users:
- they have UIDs with smaller numbers
- they have more privilege and access
Of course, we can change the naming rules for both regular and system users.
2.4. Relaxing adduser Rules
So, let’s modify the username restrictions to relax them.
On the one hand, we can change the NAME_REGEX or NAME_REGEX_SYSTEM variable in the /etc/adduser.conf file.
Alternatively, we can use the –force-badname option with the adduser command. The –force-badname option lets adduser accept usernames that don’t match the rules. Finally, we can avoid any restrictions by adding a user manually.
However, it’s advisable to follow the default naming convention for usernames in Linux. This way, we avoid problems that can happen with non-recommended naming conventions.
3. Why Usernames With Number Prefixes Are Problematic
Usernames that start with a digit or ones that contain only numbers can cause problems on our Linux systems. Let’s understand why.
3.1. Ambiguity With UID
In Linux, a user identifier (UID) is a special number given to each user. In particular, the system uses the UID to handle the user’s files, processes, and permissions. Thus, users can log in, access files, and run commands using their UID.
However, there’s a potential ambiguity when using usernames or UIDs as arguments to some commands:
In essence, some commands can accept usernames or UIDs as arguments.
POSIX (Portable Operating System Interface) rules describe how such commands should work:
- first, look for a username that matches the argument
- if no username matches the argument, look for a UID that matches the argument
This means that i****f there are two users with the same number, one as a username and one as a UID, then these commands will always use the username, not the UID.
Consequently, this can cause problems when we want to specify a UID explicitly.
For instance, let’s say we have a file called file.txt and we want to change the owner of the file via chown to the user with UID 55:
$ chown 55 file.txt
If we have a username 55, this code snippet will change the owner of file.txt to that instead.
Still, there’s a way around this. To avoid this ambiguity and force the interpretation of a string as a UID, we can prefix the string with a +:
$ chown +55 file.txt
This skips the username lookup process and uses the string as a UID directly. In short, a string containing + is not considered a valid username. However, this syntax isn’t supported by all Unix systems. Thus, it’s advisable to avoid using numeric usernames.
3.2. Readability
Usernames beginning with numbers are usually harder to read than those starting with letters. Numbers used as prefixes for usernames may not convey much meaning.
Further, number prefixes can lead to mistakes. For example, if we, as an administrator, need to perform an action on a specific user account but mistakenly read a username like 3lizabeth as elizabeth, this can have unintended effects.
Moreover, these kinds of usernames can hinder collaboration. Multiple users on a system may find it difficult to remember or understand usernames that have numbers in front of them. This can make communication difficult or less efficient.
3.3. Conventions
As we saw, Linux systems often use usernames that begin with letters of the alphabet. So, starting a username with a number goes against the usual conventions. Consequently, it can cause compatibility issues and confusion with applications that rely on standard usernames:
- some applications may assume that usernames begin with letters and validate them accordingly
- some applications may use usernames to generate other identifiers, such as email addresses or URLs
For instance, if an application uses the username to create an email address, we might get something like [email protected]. However, this email address may not be valid or acceptable by some email servers or clients.
Thus, it’s usually better to follow the conventions and use alphabetical usernames.
4. Conclusion
In this article, we discussed the naming convention in Linux and why we might not want to start a username with numbers.
Usernames are an essential part of the Linux system and should be chosen carefully. In short, usernames should be unique and follow the naming convention of Linux.