1

This question already has an answer here:

I'm trying to make a script where it makes a test user with a home dir and the rights he needs but everytime I run the script I get the following error:

/home/thomas/Scripts/CreateUser.sh: line 2: useradd: command not found
passwd: user 'password' does not exist
/home/thomas/Scripts/CreateUser.sh: line 4: mkhomedir_helper: command not found
chmod: cannot access ‘/home/Test/’: No such file or directory

Script:

#!/bin/bash
useradd Test 
passwd password
mkhomedir_helper Test
chmod 700 /home/Test/

I'm new to linux so I don't know why this happens, any solutions?

marked as duplicate by Jeff Schaller, don_crissti, Eric Renouf, Kusalananda, GAD3R Feb 3 '17 at 16:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • what distro are you running and what's the output of find / -iname useradd? – 13dimitar Feb 3 '17 at 11:50
  • I don't know what distro is but the output of find / -iname useradd is imgur.com/a/tyqyr – Thomasttw Feb 3 '17 at 11:55
  • 1
    okay, give me the output of more /etc/*-release and echo $PATH – 13dimitar Feb 3 '17 at 11:57
  • the first command didn't work imgur.com/a/LvElq – Thomasttw Feb 3 '17 at 12:00
  • try again with more /etc/*-release – 13dimitar Feb 3 '17 at 12:01
2

Your script should look like:

#!/bin/bash
/usr/sbin/useradd -m -d /home/Test/ -s /bin/bash Test 
echo -e "password\npassword" | passwd Test
chmod 700 /home/Test/

The reason for your error is that /usr/sbin is most likely not in the $PATH variable of the account you're running the script with.

  • this is my result: imgur.com/a/9jU1X – Thomasttw Feb 3 '17 at 12:11
  • Execute the script with sudo, as you need elevated permissions to use useradd. – 13dimitar Feb 3 '17 at 12:12
  • How can I add the sudo to my script because I don't run the scripts with cmd but by right clicking the file and click run in Konsole – Thomasttw Feb 3 '17 at 12:25
  • If you click with the right mouse button on the script, depending on your desktop environment, you should have an option to run the script with different user, or elevated run. Use root and you will be fine. – 13dimitar Feb 3 '17 at 12:27
  • Is there no way to add a line to the script so it does that for me because in the end of my project I need this to work on its own – Thomasttw Feb 3 '17 at 12:30

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