0

The script below is what I have tried already but it still asked me for the user's password and other information when it ran.

#!/bin/bash

PASSWORD="somepassword"
USERNAME="default"

if id -u "$USERNAME" >/dev/null 2>&1; then
    userdel -r -f $USERNAME
    adduser --disabled-password --gecos "" $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd

else
    adduser $USERNAME
    adduser --disabled-password --gecos "" $USERNAME 
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd
fi
  • What is the first adduser $USERNAME (in the else block - without the non-interactive options) doing there? – steeldriver Feb 9 '17 at 23:11
  • Even when I removed it, the issue persisted, but I fixed the problem with useradd! see my answer. – answerSeeker Feb 9 '17 at 23:26
0

Turns out adduser sucks if you try to use it in an autonomous script. I ended up using good old useradd instead which completely fixed my script below.

#!/bin/bash

PASSWORD="somepassword"
USERNAME="default"

if id -u "$USERNAME" >/dev/null 2>&1; then
    userdel -r -f $USERNAME
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd

else
    useradd -m -p $PASSWORD -s /bin/bash $USERNAME
    usermod -a -G sudo $USERNAME
    echo $USERNAME:$PASSWORD | chpasswd
fi
0

Warning: It is a very bad idea to leave an UN-encrypted password inside an script. If you still choose to do it, at least change the permissions of the file to 600 by root. That will protect it from simple attacks.

Then:

The first problem I see is that you are calling adduser twice, in the else part.

That will trigger a first execution that asks all the questions.
The second call works fine.

Also, there are many common options between both then and else parts, the script could be written as this:

#!/bin/bash

$PassWord="somepassword"
UserName="default"

if id -u "$UserName" >/dev/null 2>&1; then
    userdel -r -f "$UserName"
fi

adduser --disabled-password --gecos "" "$UserName"
userdir=/home/"$UserName"
[[ -d $userdir ]] || mkdir "$userdir"   # only needed for system users.
                                        # which usually do not have a password.
usermod -a -G sudo "$UserName"
echo "$UserName:$PassWord" | chpasswd
  • The password in the bash file is a default password. What if I want everyone to know what the default password is? Do I still need to encrypt it? The people getting their new devices will change their passwords or create new users anyways. – answerSeeker Feb 11 '17 at 22:46
  • That imply that everyone will be able to create a system account. That is a very lax secure system, to state it politely. If that is your selected level of security: good luck. – sorontar Feb 11 '17 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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