0

When running as user1 in the same directory (/home/user1/WWW)

[user1@server1 WWW (master)]# touch c

Creates the file c inside the directory.

[root@server1 WWW (master) ACCEPTATIE SERVER]# su - user1 -c "touch c"

gives the error

touch: cannot touch `c': Permission denied

Why can this be?

6
  • @Kusalananda yes the directory is owned by user1:apache. I would suspect the first case would also fail when that was not true.
    – Thomas
    Commented Aug 15, 2018 at 13:16
  • 3
    If your remove the - from the command and do su user1 -c ...? With - you do a full login, and may end up in another directory than the directory you're currently in.
    – Kusalananda
    Commented Aug 15, 2018 at 13:21
  • @steeldriver That's what I thought. But why would user1 not be able to touch c in their home directory. Is there already a file owned by another user there?
    – Kusalananda
    Commented Aug 15, 2018 at 13:22
  • @steeldriver that is the correct identification of my problem and is solved now. So conclusion for everyone coming here via google: su - user1 -c changed the current working directory!
    – Thomas
    Commented Aug 15, 2018 at 13:25
  • ... and that file in user1's home directory is owned by another user?
    – Kusalananda
    Commented Aug 15, 2018 at 13:26

1 Answer 1

4

When you do su - username you perform a full login as that user, meaning that you are transferred to the user's home directory. When executing touch c in this way, you therefore try to run that command in the user's home directory.

Instead, drop the - from the command line:

su user1 -c "touch c"

This would execute touch c as user1 in the current directory.


Speculation:

The original su - command failed because there is already a file called c in user1's home directory owned by another user.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .