Permission denied creating directory after switching user
Solution 1
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
Solution 2
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 namedsafi
.
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).
Solution 3
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 ()
Related videos on Youtube
![safi jinji](https://lh5.googleusercontent.com/-hcyesa5hyKI/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rdKa-COBHgkn32eMRwz23mSnDsCGw/photo.jpg?sz=256)
safi jinji
Updated on September 18, 2022Comments
-
safi jinji almost 2 years
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$
-
Eliah Kagan over 4 yearsI think you ran some commands before those shown--either a previous
su
command, or a previouscd
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! -
Pablo Bianchi over 4 yearsYou swiched to
safi_test
but you still are onsafi
home folder, where you don't have permission to write (and therefore create dirs): Check withls -ld
. safi is others,r-x
. Learn about Linux file permissions
-
-
mchid over 4 yearsHow did you do this?
-
WinEunuuchs2Unix over 4 years@mchid I updated answer with the function. I'd appreciate any recommended changes or additions to help text.
-
Eliah Kagan over 4 years@mchid The OP probably ran something like
cd ~safi
before runningsu
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 ansu
command run by the same user specified as the user to switch to. Note that each prompt begins with the textsafi_test4@
. The prompt written before thesu
command was run begins that way, as does the one written after thesu
command succeeded. -
Eliah Kagan over 4 years@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 showedsafi_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 previoussu
command that isn't shown in the question and that did actually switch users fromsafi
tosafi_test4
, but never changed tosafi_test4
's home directory. -
mchid over 4 yearsAlso, +1 for:
su - safi
-
Eliah Kagan over 4 years@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 acd
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.) -
mchid over 4 yearsYeah, 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 over 4 yearsAwesome. Thanks!