Understanding the NAME_REGEX in Linux (used for checking usernames)

  • Thread starter Wrichik Basu
  • Start date
  • Tags
    Linux
In summary, the conversation discusses the use of a regex for setting up a username on Linux. The regex specifies that the username must begin with a lower case letter, followed by zero or more characters including hyphens, lower case letters, digits, and underscores. A dollar sign is also required at the end, but may not actually appear in the regex due to backslash interpretation. The conversation concludes with the clarification that the backslash may not be needed in certain contexts.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
I was helping one of my classmates to set up the Ubuntu app on Windows 10 so that he can do Bash programming. While setting the username, he was getting an error due to bad regex, and that prompted me to look up the regex for username on Linux. This regex is stored in /etc/adduser.conf file. The regex is as follows: ^[a-z][-a-z0-9_]*\$

As far as I understand, this pattern wants the username to begin with a lower case letter. Then there can be zero or more characters, chosen from hyphen, lower case letters, digits, and underscore. At the end, the dollar sign is escaped, so there has to be a literal $ sign at the end.

regex101.com says that my interpretation is correct. In fact, user-name does not match the regex, but user-name$ matches.

However, my username is wrichik-basu, i.e., no $-sign at the end. While setting up the Ubuntu app for my classmate, no dollar sign was required either.

Am I wrong somewhere in understanding the regex?
 
Technology news on Phys.org
  • #2
Wrichik Basu said:
At the end, the dollar sign is escaped, so there has to be a literal $ sign at the end.
The backslash might not actually end up in the regex, depending on how backslashes are interpreted in string literals.
Code:
$ echo "^[a-z][-a-z0-9_]*$"
^[a-z][-a-z0-9_]*$
$ echo "^[a-z][-a-z0-9_]*\$"
^[a-z][-a-z0-9_]*$
$ echo "^[a-z][-a-z0-9_]*\\$"
^[a-z][-a-z0-9_]*\$
$ echo '^[a-z][-a-z0-9_]*\$'
^[a-z][-a-z0-9_]*\$
 
  • Like
  • Love
Likes Wrichik Basu and Ibix
  • #3
wle said:
The backslash might not actually end up in the regex, depending on how backslashes are interpreted in string literals.
Yes, you are right. Thanks, that explains it. (In fact, I am learning bash programming right now, and should have found that out myself. :doh: )
 

What is the purpose of the NAME_REGEX in Linux?

The NAME_REGEX in Linux is used for checking and validating usernames. It ensures that usernames follow a specific format and do not contain any illegal characters.

What is the format of the NAME_REGEX in Linux?

The NAME_REGEX in Linux follows the regular expression syntax, which allows for a combination of letters, numbers, and special characters to create a pattern for validating usernames.

What characters are allowed in a username according to the NAME_REGEX in Linux?

The NAME_REGEX in Linux allows for usernames to contain letters (both uppercase and lowercase), numbers, underscores, and dashes. Spaces and special characters are not allowed.

Can the NAME_REGEX in Linux be customized?

Yes, the NAME_REGEX in Linux can be customized to fit the specific requirements of a system. This can be done by modifying the regular expression pattern or by using additional options and flags.

What happens if a username does not match the NAME_REGEX in Linux?

If a username does not match the NAME_REGEX in Linux, it will be considered invalid and the system will not allow it to be used. This is to ensure that all usernames on the system follow a standard format for consistency and security purposes.

Similar threads

  • Computing and Technology
Replies
5
Views
288
Replies
16
Views
2K
  • Computing and Technology
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
152
Views
5K
  • STEM Academic Advising
2
Replies
54
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
279
Back
Top