What are the real rules for linux usernames on CentOS 6 and RHEL 6?
A basic gnu/linux username is a 32 character string (useradd(8)
). This is a legacy format from the BSD 4.3 standard. passwd(5)
adds some additional restrictions like, do not use capital letters, do not use dots, do not end it in dash, it must not include colons.
To be on the safe side of things, follow the same rules of a C identifier:
([a-z_][a-z0-9_]{0,30})
That's half the problem. Modern GNU/Linux distributions use PAM for user authentication. With it you can choose any rule you want and also any data source.
Since you are writing a program it's better to define your own format, and then use something like pam_ldap
, pam_mysql
, etc. to access it.
Linux: Can't get home directory of user which contains whitespace
A basic gnu/linux username is a 32 character string (useradd(8))
. This is a legacy format from the BSD 4.3 standard. passwd(5)
adds some additional restrictions like, do not use capital letters, do not use dots, do not end it in dash, it must not include colons.
check more details in this answer What are the real rules for linux usernames on CentOS 6 and RHEL 6?
Generate valid Unix username and password in Perl
For generating password You can use:
$password = `date | md5sum | fold -w 10 | head -n 1`;
$password = crypt($password,'some_string');
It's actually more Bash than Perl but it does the job.
How can we get list of non-system users on linux?
You need to get all users whose gid
is greater than or equals 1000. Use this command for that:
awk -F: '($3>=1000)&&($1!="nobody"){print $1}' /etc/passwd
If you want system users (gid<1000) it will be:
awk -F: '($3<1000){print $1}' /etc/passwd
Given a linux username and a password how can I test if it is a valid account?
You can validate that a given password is correct for a given username using the shadow file.
On most modern distributions, the hashed passwords are stored in the shadow file /etc/shadow (which is only readable by root). As root, pull the line from the shadow file for the given user like so:
cat /etc/shadow | grep username
You will see something like this:
username:$1$TrOIigLp$PUHL00kS5UY3CMVaiC0/g0:15020:0:99999:7:::
After the username there is $1. This indicates that it is an MD5 hash. After that there is another $, then (in this case) TrOIigLp followed by another $. TrOIigLp is the salt. After that is the hashed password, which was hashed using the salt - in this case PUHL00kS5UY3CMVaiC0/g0.
Now, you can use openssl to hash the given password using the same salt, like so:
openssl passwd -1 -salt TrOIigLp
Enter the given password when prompted, the openssl command should compute the MD5 hash using the salt provided, and it should be exactly the same as the above from the shadow file. The -1 in the above command is for MD5 hashing.
Related Topics
Docker-Compose Up and User Inputs on Stdin
Prevent File Descriptors Inheritance During Linux Fork
What Is the Default Register State When Program Launches (Asm, Linux)
Linux Terminal Input: Reading User Input from Terminal Truncating Lines at 4095 Character Limit
Init Function Invocation of Drivers Compiled into Kernel
Why Does Sed Fail with International Characters and How to Fix
What's the Point of Eval/Bash -C as Opposed to Just Evaluating a Variable
Bash Command Substitution on Remote Host
Getting Pids from Ps -Ef |Grep Keyword
What Linux Shell Command Returns a Part of a String
Finding Docker Container Processes? (From Host Point of View)
How to Delete All Lines in a File Starting from After a Matching Line