1

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

3

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.

13dimitar
  • 1,529
  • this is my result: http://imgur.com/a/9jU1X – Thomasttw Feb 03 '17 at 12:11
  • 2
    Execute the script with sudo, as you need elevated permissions to use useradd. – 13dimitar Feb 03 '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 03 '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 03 '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 03 '17 at 12:30
  • You can add sudo inside the script, but you have to add it on every single line. I do not find this practical at all and I think that it is a bad solution. It will work, but it is wrong. You can add sudo at the beginning of every line. If the user you run the script with doesn't get prompted for a password when using sudo, you will be fine. – 13dimitar Feb 03 '17 at 12:33
  • Adding sudo to every line gives me a prompt and when I enter the password I get some errors – Thomasttw Feb 03 '17 at 12:39
  • It gives you a prompt because your sudoers file doesn't contain a line to permit nopasswd sudo for the user you're running the script with. That's why it's not a good idea to add sudo to every single line. – 13dimitar Feb 03 '17 at 12:40
  • I still get a lot of errors when I run it even with sudo and It also makes the dir in /home/thomas/Scripts/ folder http://imgur.com/a/KtOZd – Thomasttw Feb 03 '17 at 12:51
  • All seems to work but the password still isn't changing to what I want (same error as the last screenshot) – Thomasttw Feb 03 '17 at 12:59
  • Just to note that the passwd command might take an optional password on the command line in Linux, but it won't do that on all Unices. (Does it even in Linux?) – Kusalananda Feb 03 '17 at 12:59
  • you can try now again – 13dimitar Feb 03 '17 at 13:06
  • Huge thanks, it works even when it says that the password isn't good because it's in the dictionary, can you tell me what I need to edit in the password line to change the password maybe? – Thomasttw Feb 03 '17 at 13:09
  • replace password with the actual password you want to set (e.g. echo -e "mynewpassword\nmynewpassword" | passwd Test – 13dimitar Feb 03 '17 at 13:10
  • Huge thanks for your help and time! Everything works like it's supposted to work! – Thomasttw Feb 03 '17 at 13:15