Find the answer to your Linux question:
Results 1 to 8 of 8
I'm trying to create a new user with a password through the command line but for some reason I can't log into the account. Here's my stripped down command: adduser ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33

    adding users and passwords with adduser

    I'm trying to create a new user with a password through the command line but for some reason I can't log into the account.

    Here's my stripped down command:

    adduser -p password user1

    The man pages states that the password is what's returned by crypt(). Does this mean I have to call the crypt function and store the results into a variable and then pass that with the above command?

  2. #2
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,096
    If you aren't sure about your command, you can just enter (as root):

    Code:
    adduser
    ...and the adduserscript will ask for the info it needs to add a new user properly. It won't allow for setting a password until all the other information is collected and you have actually created the account.

    When all this is finished, you should be able to login as the new user.
    oz

    new members/users: read this first | new member faq
    no private messages requesting computer support - post them on the forums!
    please use the "report post" button to alert our forum admins to problematic posts rather than responding to them yourself.

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33
    Thanks, but I need to run this from a script file without any user input.

  4. #4
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    Here is what you can do. First read the manpage of adduser. I am pretty sure that all of these user inputs can be supplied as cmd line args (sitting on a windows machine so cant check that). Now in a plain text file put all the new user's information (one user per line, one information per column). Use awk to read this input and pass it on to adduser. You might also try perl.
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  5. #5
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33
    So I already have the commands down but, I just need to determine how to crypt the password. I guess my question should have really been:

    How can I use the crypt function inside of my script file? The output of the crypt function is what the "-p password" switch is looking for. Since the crypt function is in a library called unistd.h. How do I get my script file to understand the crypt function?

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    How good are you at writing Perl? That's the only way I found to encrypt a password using crypt. My knowledge of Perl is extremely limited and haven't work with it in a long time.

    Perl script called a.pl to encrypt a password:

    Code:
    #!/usr/bin/perl
    
    my $password = 'thisisatest';
    my $salt = 132;
    my $CryptPass = crypt($password,$salt);
    print $CryptPass
    Bash script called a.bash to call a.pl and then use the encrypted password to add the account:

    Code:
    #!/bin/bash -vx
    
    crypt="`./a.pl`"
    echo "$crypt"
    useradd -p "$crypt" testit
    I don't remember how to pass parameters to Perl so you'll have to look into that.

    Hope this helps.

  7. #7
    Just Joined!
    Join Date
    Aug 2007
    Posts
    33
    Thanks for the input. This should do the trick...but I was wondering if there was a way were I would only have

    I found that there is a function called passwd that takes the username as an arguement but, after this function call it asks the user to input the new password and then repeat the new password.

    So is there any way that I can provide the passwords without having the user input them?

  8. #8
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Forgot about passwd. You'd have to write the password twice to a file like tmp.tmp and then execute the command:

    Code:
    echo "$pswd" > tmp.tmp
    echo "$pswd" >> tmp.tmp
    passwd --stdin [username] < tmp.tmp
    rm -f tmp.tmp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...