Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Here's the script code I'm using now:

getent group $MYGROUP
if [ $? -ne 0 ] ; then
    sudo su -c "groupadd $MYGROUP"
fi
sudo su -c "useradd mynewuser -p mypassword -m -g $PRIMARYGRP -G $MYGROUP"

This approach works well on openSuse. But there are several problems with the user account it creates on Ubuntu so I am seeking help here.

  • the terminal prompt isn't set (echo $PS1 returns nothing)
  • the arrow keys and tab key do not work correctly in the terminal
  • the password doesn't seem to work (although I'm still unclear exactly what this issue is)
  • the /etc/sudoers rights set for this new user are not honored

If instead I manually create the user with adduser (instead of useradd) I don't have these problems on Ubuntu. But I can't use adduser on openSuse (afaik). Therefore, I need a non-Debian-exclusive script or method of adding user accounts via my bash script that works on Ubuntu (and doesn't stop working on other distros).

Finally, I would like to understand the differences between adduser and useradd. For example, I want to know which skeleton directory is used by adduser as that might be the reason useradd isn't working as expected (because I just accepted the default).

Thanks

share|improve this question
1  
As for the difference: from the useradd man page (man useradd): useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead. –  guntbert Jul 13 '13 at 20:24
    
Also, sudo su -c "cmd arg1 arg2" is equivalent to sudo cmd arg1 arg2. –  enzotib Jul 13 '13 at 21:09
    
@enzotib thanks. Good to know those are equivalent on Ubuntu because they are not on openSuse. Therefore, I will keep using sudo su -c "cmd arg1 arg2" so that it works everywhere I need it to work. –  MountainX Jul 13 '13 at 21:13
    
adduser is a perl script more than 1000 lines long, so for the differences you can take a look at the script. –  enzotib Jul 13 '13 at 21:13
    
@enzotib Yes, I've been reading adduser but I don't know perl. So that's not a good approach for me. –  MountainX Jul 13 '13 at 21:14

2 Answers 2

up vote 5 down vote accepted

My solution was provided here: http://unix.stackexchange.com/questions/82923/proper-way-to-add-a-user-account-via-bash-script by Ulrich Schwarz and Joseph R.. The main thing I had to do was add -s /bin/bash to my existing useradd command and remove -p password which expects an encrypted password.

sudo su -c "useradd mynewuser -s /bin/bash -m -g $PRIMARYGRP -G $MYGROUP"

Then do this:

sudo chpasswd << 'END'
mynewuser:password
END
share|improve this answer

I suppose you can simply use a condition in your script, something like

if grep -q 'Ubuntu\|Debian' /etc/issue; then
    adduser .....
else
    useradd .....
fi

(cannot verify if the file /etc/issue is present on OpenSUSE, otherwise you can put the condition on the existence of such a file).

share|improve this answer

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.