Sara Sara - 1 year ago 54
Linux Question

permission denied in linux when creating a new user

I am new in linux and I was trying to do some commands in terminal. My problem is that when I do su to another user, and I want to create a directory there, it shows me that permission is denied. Please, help me to understand why this happens and what can I do to solve it.

user@user_VB:~$ sudo su
root@user_VB:~$ useradd user1
root@user_VB:~$ groupadd students
root@user_VB:~$ usermod -g people user1
root@user_VB:~$ su user1
user1@user_VB:~$ mkdir exams

Now it shows:

permission denied.

But if I type this:

user1@user_VB:~$ sudo mkdir exams

It shows:

user1 is not in suboers file.

Answer Source

So you have two issues here.

The first is that the new user you created cannot create a new dir. This is because, even though you're changing users (user -> root -> user1), you're still in the home directory of user, which user1 can't write to. What you need to do is either specify a user directory when you create the user, (useradd -d /home/user1 user1), as @fedorqui mentioned, or, if you've already created user1, use the usermod command to add a home directory to user1, like this:

usermod -d /home/user1 user1

If /home/user1 does not exist, you'll need to, as root, do the following:

mkdir /home/user1
chown -R user1 /home/user1

What this is doing is first creating the directory, and then assigning ownership of it to your newly created user. So now, when you do su - user1, you'll end up in /home/user1. You should now be able to create directories and files.

Your second issue, "user1 is not in suboers file.", is a result of how sudo manages which users are allowed to use it. The rules that control this are in /etc/sudoers, which you should edit with visudo. If you wanted to allow user1 to run whatever they want with sudo, you could add a line like this:

user1 ALL=(ALL) ALL

Which says that user user1 may run any command on the system as root. If you now try to run a command with sudo as user1, it will prompt you for user1's password and then run the command.