I'm trying to figure out how CentOS 7 identifies what is considered a valid character in a naming convention when a user account is created.

I was playing around with useradd to confirm my understanding of what CentOS considered valid characters in a user name.... and to my surprise the useradd command accepted '_' and '.'... Which got me wondering where valid/invalid characters are established in CentOS

The useradd man page in CentOS doesn't mention valid characters... And google searches said this is done with NAME_REGEX (For ubuntu at least)... and had me looking for it in a adduser.conf file... which does not exist in CentOS 7... I haven't been able to locate a CentOS/Red Hat specific source on google about this... But I've also looked in /etc/login.defs and /etc/default/useradd with no luck

Can anyone direct me to the mechanism in CentOS does this?


Note that useradd may be more restrictive than what is actually valid as a username. The rules as to what a valid username is may also change via PAM and nsswitch configuration settings.

The POSIX standard says that usernames must be from the "portable filename characterset" but the first character must not be a hyphen.

The portable filename character set are the characters A-Z a-z 0-9 . - _

Note this means that something like "1337" is a valid username according to this standard, which may cause issues elsewhere.

So, for safety, I would use a pattern such as [A-Za-z][A-Za-z0-9._-]+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.