Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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'd like to use the adduser command to add a user (with disabled password) via a shell script.

By default, adduser prompts you for various values (e.g., Full Name). Is there any way to submit these values via command line? Or will I need to useradd instead?

share|improve this question
    
have a look at the man page for adduser. It will help you out. – Zoke Jan 9 '12 at 14:01
2  
@Zoke I couldn't find the info I was looking for on the man page. – Lorin Hochstein Jan 9 '12 at 14:06
up vote 107 down vote accepted

Use the --gecos option to skip the chfn interactive part.

adduser --disabled-password --gecos "" username

It's all in the man page. Not the most obvious formulation tho.

--gecos GECOS
          Set  the  gecos field for the new entry generated.  adduser will
          not ask for finger information if this option is given.

The GECOS field is a comma separated list as such: Full name,Room number,Work phone,Home phone

Hope this helps you.

share|improve this answer
12  
Yeah, I can see how I missed that. "gecos" is not an obvious description – Lorin Hochstein Jan 9 '12 at 14:26

useradd can also add users and does not appear to have any form of prompting built in.

useradd -m -p <encryptedPassword> -s /bin/bash <user>
  • -m, --create-home: Create user home director
  • -p, --password: Specify user password
  • -s, --shell: Default shell for logon user.

    Blank will use default login shell specified by the SHELL variable in /etc/default/useradd

  • Substitute <user> with the login name
  • Substitute <encryptedPassword> with the encrypted password

To generate your encrypted password you can use perl like this:

password="1YelloDog@" # your chosen password
perl -e 'printf("%s\n", crypt($ARGV[0], "password"))' "$password"
share|improve this answer
    
The options you mention don’t exist for adduser on my (recent) version of Ubuntu. – ᴠɪɴᴄᴇɴᴛ Sep 22 '15 at 20:30
    
@ᴠɪɴᴄᴇɴᴛ adduser is distinct from useradd, confusing I know. – ThorSummoner Sep 22 '15 at 21:01
1  
Oops, indeed missed that you use its almost namesake … isn’t there a BDFL protecting the command line namespace? ;p – ᴠɪɴᴄᴇɴᴛ Sep 23 '15 at 9:52
    
It is not working, I cannot login SSH. – Kovacs Akos Dec 3 '15 at 8:58
    
@mum007 This is only general advice, try adding -v or -vv or -vvv to your ssh commands to see whats wrong and search your error messages here on SO or Google. – ThorSummoner Dec 3 '15 at 17:30

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.