Server Fault is a question and answer site for system and network administrators. 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

I'm useing linux command useradd and I have problem. I crypted password and add user but not put all pass like:

Password: 7856

Encrypted password: $1$ky6XlX5g$mu6Wva/vTJgZF9.p8tyAq/

Example: useradd -m -p '$1$ky6XlX5g$mu6Wva/vTJgZF9.p8tyAq/' username

I look to shadow file and not all password saved, just a slice of encrypted password.

Saved peace: /vTJgZF9.p8tyAq/

I need exactly command: useradd -m -p

share|improve this question
You mention you are using PHP. Are you sure that your system isn't trying to evaluate $ky6XlX5g as if it was a variable? – Zoredache Aug 20 '11 at 18:51
up vote 2 down vote accepted

This may solve your particular problem...

# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
    read -p "Enter username : " username
    read -s -p "Enter password : " password
    egrep "^$username" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo "$username exists!"
        exit 1
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
        useradd -m -p $pass $username
        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
    echo "Only root may add a user to the system"
    exit 2
share|improve this answer
I need to know my error reason, it good script but I need like my example. Meabe it impossible? – altdovydas Aug 20 '11 at 15:09
There are many reasons your method is failing, namely the way crypt() works. – tagram Aug 20 '11 at 15:19
your method is failing most likely due to useradd decrypting the encrypted password and re-encrypting it silently. this is expected behavior with useradd, so to accomplish your goal, you will need a script like the one I posted. It's a simple bash script which you can put in a plain text file and call it from any other application or script if you mark it executable and place it somewhere in your $PATH – tagram Aug 20 '11 at 15:21
how modify this scritp that I can execute like ./ username password? sorry I just scripting in php... – altdovydas Aug 20 '11 at 15:31
create a new empty file, copy the script EXACTLY as it appears and save the file. This script will then be run ./scriptname it will then prompt you for a username and password. In php you can include some expect logic, or change the bash script to accept command line parameters. – tagram Aug 20 '11 at 15:35

You mention you are using PHP and SSH. Are you sure that your system isn't trying to evaluate $ky6XlX5g as if it was a variable? Both PHP and Bash use this syntax, perhaps you haven't properly escaped the command you are sending via SSH.

I ask this because I just ran your command on my local system and it seems to work exactly like it should. I suspect something else is happening, and the command is not actually being passed correctly.

root@hostname:# useradd -m -p '$1$ky6XlX5g$mu6Wva/vTJgZF9.p8tyAq/' username
root@hostname:# grep username /etc/shadow
share|improve this answer
And I tryed with putty program but it same error like ssh2... tagram script is good solution in my way. Maybe you don't get because your local system... – altdovydas Aug 22 '11 at 9:01

I think you have to decrypt it before adding. Maybe u can use a python script to do that. You can substitute the compression technique below with your encryption technique :

import zlib
import os

def useradd(pass_string,username):
    os.system('useradd -m -p'+password+' '+username)


You can arrive at that parse string by just typing python on you command line, and in the interpreter you can type

import zlib

This will output


Hope it is clear. You can just substitute the zlib compression with your encryption technique.

share|improve this answer
I need to know why not all pass saved and how do it, because I'm using ssh2 with php. – altdovydas Aug 20 '11 at 15:06
Yeah u can also do it that way !! – sethu Aug 20 '11 at 17:58

Your Answer


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.