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.

I am on Ubuntu 11.04. I am creating another user and placing an existing user in the group of other user, hoping to write in the home directory of other user.

# uname -a
Linux vini 2.6.38-11-generic #50-Ubuntu SMP Mon Sep 12 21:18:14 UTC
2011 i686 athlon i386 GNU/Linux
# whoami
sachin
# su root
# useradd -m -U foo               // create user foo
# usermod -a -G foo sachin      // add user `sachin' to group `foo'
# chmod 770 /home/foo/
# exit
# whoami
sachin
# cd /home/foo/
bash: cd: /home/foo/: Permission denied
# groups sachin
sachin : sachin foo

This is totally weird. Though user sachin is in group foo, and group bits for /home/foo/ is set to rwx, sachin can't chdir to /home/foo/. I am not able to understand this.

But, if at the exit step, I switch to sachin user from root, this is what happens:

# uname -a
Linux vini 2.6.38-11-generic #50-Ubuntu SMP Mon Sep 12 21:18:14 UTC
2011 i686 athlon i386 GNU/Linux
# whoami
sachin
# su root
# useradd -m -U foo               // create user foo
# usermod -a -G foo sachin      // add user `sachin' to group `foo'
# chmod 770 /home/foo/
# su sachin
# whoami
sachin
# cd /home/foo/
# ls
examples.desktop

Now, whatever is happening here is totally incomprehensible. Does su sachin inherits some permissions from the root user at this step?

Any explanations would be much appreciated.

share|improve this question
    
It might help if you checked, in each step, the contents of /etc/group and did ls -ld /home/foo and so on. –  belacqua Jun 17 '12 at 20:10

2 Answers 2

Logout and then login back, it will work or you can use a new terminal (not gnome-terminal, the tty-devices).

It seems to be a bug as the requirement of this logout and login is completely unnecessary.

share|improve this answer
    
I know it was totally unexpected that there would be a requirement for logging in and then logging out –  Sachin Jun 17 '12 at 10:20

What are the permission of /home? You need +x permissions there as well.

Also, you need to use su - sachin to get to full environment of the new user.

Note that the man page for useradd now says:

   useradd is a low level utility for adding users. On Debian,
   administrators should usually use adduser(8) instead.

If you're using a desktop/GUI version, things usually work out more easily with the users-admin tool.

The user with the active shell won't inherit the new group in this session (assuming you're doing it as in the example, or by script) -- you need to have a new shell for that user to test the group membership. (Unless you are root.) This is a longstanding Unix behavior -- so you would typically start a new shell process for the user, e.g., by opening a new terminal (which spawns a new shell, reading your environment by default), or by spawning a new shell for the user in the same terminal.

share|improve this answer
    
/home does have +x, The thing is I can't use su - sachin because I am creating the new users programmatically, and after that I need to access these directories via apache server which is running under sachin The only workout is that I access directories via c code where the setuid bit is on. But that is an overkill, keeping in mind that sachin is in the group of the new user and should logically have access to the new user's directory. The problem is not of useradd I tried adduser as well and the problem persists –  Sachin Jun 17 '12 at 10:24
    
It would help if you gave more of an idea of how you are doing this programmtically, since in the example in the question, you are not doing so. –  belacqua Jun 17 '12 at 20:06
    
Yes in the above example I am not doing so but what I intended to say is that ultimately I have to carry this task from a c program having the effective uid set to root. After I have created the user, I need to copy some files from a python program to the newly created home directory. I can't run the python program via root, I was expecting if the user under which the python program is running is also in the new users group I would have been able to copy files after having set apt permissions. But unfortunately this requires a reboot which I don't want –  Sachin Jun 18 '12 at 8:32

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.