Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry, I´m novice in Linux and I am looking for information, how to create new user account in Debian with pre-defined encrypted password.

I have a function in bash, that if new user´s id´s are not presented in my /etc/passwd file than I run the bash command:

adduser -m -p <encrypted-password> <username>

so far so good, but all my data including username and password are stored in JSON output and user´s password is already hashed by SHA-512 (for security reasons). User´s password is encrypted by PHP script and sent to JSON:

$hashed_password = crypt('Test007', '$6$rounds=5000$StJ.1Wji$');
echo looks like this: $6$rounds=5000$StJ.1Wji$cm6ZVl.XoIiQXaJAVHkqiteUGAqZoJ1Ee3dpHP2a6x6rG/kHg4k7ucMLrzHCvQA1TpQYP4eKnoFITVGcviqjU0

After adduser function when I look in my /etc/shadow file, I see that the hashed password is different from original one generated by PHP and of course I cannot login the new user with that password in plain text (even if I know it in plain text).

Is there an easy way, how to create user account with pre-defined encrypted password? How to generated hashed password to be accepted by Linux useradd function? I cannot find any solutions on the internet.

share|improve this question

That $6$rounds=5000 is probably part of the problem, because except for the leading $6, the entire value passed to adduser has to be a valid hash. Also (not apparent in your script fragment), the value should be quoted to avoid parameter expansion, e.g.,

adduser -m -p '$6$gehr8sgkkX$lMZ5bmb7c4HY76pnn0uUXA5wH51YE0Byp4rIfcA94gwrVvfEnVQ

SMwoW1erVuxzFScxRvaHOLFMqVSYCjVLTV/' oneshot

I used mkpasswd to get a value as suggested in How to create an SHA-512 hashed password for shadow?

In a quick check here, the value stored in /etc/shadow matches the value I used when running adduser.

share|improve this answer
    
I ´ve tried generate sha-512 hash by php $pass = crypt("myPassword" , "$6$FixedSalt")** but when I do again **useradd -p "$pass" <username>** - what i see in **/etc/shadow** looks still different hash (there is no $6$ at the beginning and it is short). I thinked maybe my PHP hash not corresponds the valid Unix format? What finally worked for me, was hashing inside my bash **openssl passwd -crypt Test007 -> output me: "jc44wHaeucnHI" than I can login with password "Test007" to my new user. Can I find similiar function as openssl in PHP? – scree Nov 12 '15 at 20:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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