0

I switched to a different user with the su command, and now I want to create a file/folder but "permission denied" error appears.

safi_test4@safi-VirtualBox:/home/safi$ su safi_test4
Password:
safi_test4@safi-VirtualBox:/home/safi$ whoami
safi_test4
safi_test4@safi-VirtualBox:/home/safi$ mkdir try
mkdir: cannot create directory `try': Permission denied
safi_test4@safi-VirtualBox:/home/safi$

screenshot of the terminal showing the problem

  • I think you ran some commands before those shown--either a previous su command, or a previous cd command--but I am not sure which. I recommend that you edit your question to clarify whether or not that is the case and, if so, what commands you ran. If we close this first, your edit may lead to it being reopened. Thanks! – Eliah Kagan Oct 20 at 22:24
  • You swiched to safi_test but you still are on safi home folder, where you don't have permission to write (and therefore create dirs): Check with ls -ld . safi is others, r-x. Learn about Linux file permissions – Pablo A Oct 22 at 4:26
5

You switched to a new user but you are still under the user safi home directory. safi_test4 does not have permission to create files in the safi home directory.

Run the following command to switch to the safi_test4 home directory:

cd
3

You are running su safi_test4 but you should be running su safi.

Your prompt quite strongly suggests that you started out as the same user you became with su:

safi_test4@safi-VirtualBox:/home/safi$

Specifically:

  • Although it can be set up otherwise, usually when a shell prompt starts with text followed by a @ character, the text it shows before that character is the name of the current user.
  • When a prompt ends in a :, followed by some text, followed by a $, that text between the : and the $ usually represents the current directory. Furthermore, a directory named /home/safi is very likely the home directory of a user named safi.

This is to say that you appear to have used the cd command to enter the home directory of safi, but then you used a su command that did not change your identity to safi.

Assuming the usual permissions are set, safi_test4 will not be able to create files in the home directory of safi. Furthermore, it's unlikely you intended to use su to take on the identity that you already had.

To use su to take on the identity of another user, you should pass the name of the user you want to become as the argument to su, not the name of the user you currently are:

su safi

Though you might consider using this command instead, which will run a login shell as safi:

su - safi

When you do that, you get an environment like what you would get if you had logged in as safi by other means (like on a virtual console or via SSH). This has the additional minor benefit that you don't have to separately cd to safi's home directory.

Note that when you use su you must enter the target user's password. That is, for su safi to succeed you must enter safi's password, not safi_test4's password (unless they happen to be the same).

  • @mchid The OP probably ran something like cd ~safi before running su and just didn't show that. I agree the OP intended to switch to a different user. They may have thought that happened. I would be quite surprised if it did. Barring prompts that are deliberately made to be misleading, the screenshot in the question unambiguously shows an su command run by the same user specified as the user to switch to. Note that each prompt begins with the text safi_test4@. The prompt written before the su command was run begins that way, as does the one written after the su command succeeded. – Eliah Kagan Oct 20 at 22:01
  • @mchid At first I thought your answer must have been wrong, on the grounds that the prompts shown before and after su show the user did not change and both showed safi_test4@. But now I am not sure. I agree it will help to get clarification from the OP (I've commented on the question requesting it). Am I correct to think your interpretation is that the OP ran a previous su command that isn't shown in the question and that did actually switch users from safi to safi_test4, but never changed to safi_test4's home directory. – Eliah Kagan Oct 20 at 22:21
  • Also, +1 for: su - safi – mchid Oct 20 at 22:38
  • @mchid Yes, you are right that "probably" was overly strong. I had not at all considered that that the command that was run but not shown may have been a separate su command rather than a cd command. Hopefully safi jinji will edit the question (or comment) to clarify. It's been less than a day since the question was first posted, so I think it's fairly likely that we will get clarification. (Whatever it turns out to have been, I suspect that both our answers will remain relevant and valuable, though one or both of them might then benefit from edits to accommodate or address the new info.) – Eliah Kagan Oct 20 at 22:39
  • Yeah, that's what I like about stackexchange sites. I see a lot of questions with all kinds of answers and it really helps put things into context to see various solutions to the same problem. – mchid Oct 20 at 23:21
0

As mchid answer states you can use:

cd

to change to the home directory of the current user safi_test4. However on my system it doesn't work due to a function that gets in the way:

$ cd

cd: missing operand

Usage:

    cd ~            Change to home directory. Equivelent to 'cd /home/$USER'

    cd -            Change to previous directory before last 'cd' command

    cd ..           Move up one directory level

    cd ../..        Move up two directory levels

    cd ../sibling   Move up one directory level and change to sibling directory

    cd /path/to/    Change to specific directory '/path/to/' eg '/var/log'

    cd $OLDPWD      Change back to previous directory (similar to "cd -").

In this case on my system I must always type:

cd ~

Note that the regular cd command also supports the usages described above.

cd help screen

Here is how I get the cd help screen but be warned some people have criticized it. Edit your ~/.bashrc and put in these lines:

cd() {
    if [[ $# -eq 0 ]] ; then
        cat << 'EOF'

cd: missing operand

Usage:

    cd ~            Change to home directory. Equivelent to 'cd /home/$USER'

    cd -            Change to previous directory before last 'cd' command

    cd ..           Move up one directory level

    cd ../..        Move up two directory levels

    cd ../sibling   Move up one directory level and change to sibling directory

    cd /path/to/    Change to specific directory '/path/to/' eg '/var/log'

    cd $OLDPWD      Change back to previous directory (similar to "cd -").

EOF
        return
    fi

    builtin cd "$@"
} # cd ()
  • How did you do this? – mchid Oct 20 at 19:46
  • @mchid I updated answer with the function. I'd appreciate any recommended changes or additions to help text. – WinEunuuchs2Unix Oct 20 at 21:33
  • Awesome. Thanks! – mchid Oct 20 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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