Can't access a folder with sudo












-1














I have been using Linux for two years, but today I encountered a very strange thing. I have nginx deployed on one server, and this is its log folder:




♪ log l -d nginx
drwx------ 2 nginx nginx 4.0K Dec 7 03:15 nginx



(I'm using zsh and ♪ log means I am in a folder named 'log'. As you can see, I ended up saying that I did not go into the folder named 'nginx'.)



When I try to access it:




♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Even after I changed its permissions to 766, this folder still can not be accessed:




♪ log sudo chmod -R 766 nginx
♪ log l -d nginx
drwxrw-rw- 2 nginx nginx 4.0K Dec 7 03:15 nginx
♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Can someone explain this to me?










share|improve this question
























  • Can you switch to root by sudo -i and then try to do cd?
    – kirill-a
    Dec 7 '17 at 7:42


















-1














I have been using Linux for two years, but today I encountered a very strange thing. I have nginx deployed on one server, and this is its log folder:




♪ log l -d nginx
drwx------ 2 nginx nginx 4.0K Dec 7 03:15 nginx



(I'm using zsh and ♪ log means I am in a folder named 'log'. As you can see, I ended up saying that I did not go into the folder named 'nginx'.)



When I try to access it:




♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Even after I changed its permissions to 766, this folder still can not be accessed:




♪ log sudo chmod -R 766 nginx
♪ log l -d nginx
drwxrw-rw- 2 nginx nginx 4.0K Dec 7 03:15 nginx
♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Can someone explain this to me?










share|improve this question
























  • Can you switch to root by sudo -i and then try to do cd?
    – kirill-a
    Dec 7 '17 at 7:42
















-1












-1








-1







I have been using Linux for two years, but today I encountered a very strange thing. I have nginx deployed on one server, and this is its log folder:




♪ log l -d nginx
drwx------ 2 nginx nginx 4.0K Dec 7 03:15 nginx



(I'm using zsh and ♪ log means I am in a folder named 'log'. As you can see, I ended up saying that I did not go into the folder named 'nginx'.)



When I try to access it:




♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Even after I changed its permissions to 766, this folder still can not be accessed:




♪ log sudo chmod -R 766 nginx
♪ log l -d nginx
drwxrw-rw- 2 nginx nginx 4.0K Dec 7 03:15 nginx
♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Can someone explain this to me?










share|improve this question















I have been using Linux for two years, but today I encountered a very strange thing. I have nginx deployed on one server, and this is its log folder:




♪ log l -d nginx
drwx------ 2 nginx nginx 4.0K Dec 7 03:15 nginx



(I'm using zsh and ♪ log means I am in a folder named 'log'. As you can see, I ended up saying that I did not go into the folder named 'nginx'.)



When I try to access it:




♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Even after I changed its permissions to 766, this folder still can not be accessed:




♪ log sudo chmod -R 766 nginx
♪ log l -d nginx
drwxrw-rw- 2 nginx nginx 4.0K Dec 7 03:15 nginx
♪ log cd nginx
cd: permission denied: nginx
♪ log sudo cd nginx
♪ log



Can someone explain this to me?







filesystems cd-command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 16 at 11:47









Rui F Ribeiro

38.9k1479129




38.9k1479129










asked Dec 7 '17 at 7:17









Xiaochen Cui

112




112












  • Can you switch to root by sudo -i and then try to do cd?
    – kirill-a
    Dec 7 '17 at 7:42




















  • Can you switch to root by sudo -i and then try to do cd?
    – kirill-a
    Dec 7 '17 at 7:42


















Can you switch to root by sudo -i and then try to do cd?
– kirill-a
Dec 7 '17 at 7:42






Can you switch to root by sudo -i and then try to do cd?
– kirill-a
Dec 7 '17 at 7:42












2 Answers
2






active

oldest

votes


















3














The main reason is that sudo cd nginx entered the nginx folder and exited. So you are back to the previous state before launching the sudo command.



It's the same than doing sudo ls nginx/* for exemple which will list the content of the nginx folder and exit.



sudo spawns a new process with the requested privileges and running the requested command. Once the command finish, sudo exits and you fall back to previous situation.



To dig more, you have to understand that your location inside a file system, is dynamically managed by the shell program you are using. When a process is forked the shell context is replicated in the new process. Every manipulation of the copied context is specific to it, and is not affecting the parent context. So in our case, cd nginx is modifying the $PWD of the new process and not the parent's one. Once it finish, the related context is destroyed and then, you fallback in the parent's context which wasnt modified.



As the other dude explained to you, to be able to move into a folder you need to also have the execution permission (+x).






share|improve this answer























  • Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
    – Xiaochen Cui
    Dec 7 '17 at 7:38










  • If you read the last paragraph of the answer, you have an explanation of what happens and why so.
    – netmonk
    Dec 7 '17 at 7:45



















2














A user has to have execute permissions on a directory to be able to change to it.



See e.g. the question "To cd into a directory".



To change to the directory as your ordinary user (not nginx or root), you will first have to sudo chmod o+x nginx. You may also start a root shell with sudo -s and access it from that shell session.





sudo cd nginx doesn't make much sense. The cd will succeed, but as soon as the sudo process exits, you will be left in the directory from where you executed sudo. The current working directory is part of the process' environment, and a child process (sudo) can't change the environment of its parent (your interactive shell).






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409401%2fcant-access-a-folder-with-sudo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The main reason is that sudo cd nginx entered the nginx folder and exited. So you are back to the previous state before launching the sudo command.



    It's the same than doing sudo ls nginx/* for exemple which will list the content of the nginx folder and exit.



    sudo spawns a new process with the requested privileges and running the requested command. Once the command finish, sudo exits and you fall back to previous situation.



    To dig more, you have to understand that your location inside a file system, is dynamically managed by the shell program you are using. When a process is forked the shell context is replicated in the new process. Every manipulation of the copied context is specific to it, and is not affecting the parent context. So in our case, cd nginx is modifying the $PWD of the new process and not the parent's one. Once it finish, the related context is destroyed and then, you fallback in the parent's context which wasnt modified.



    As the other dude explained to you, to be able to move into a folder you need to also have the execution permission (+x).






    share|improve this answer























    • Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
      – Xiaochen Cui
      Dec 7 '17 at 7:38










    • If you read the last paragraph of the answer, you have an explanation of what happens and why so.
      – netmonk
      Dec 7 '17 at 7:45
















    3














    The main reason is that sudo cd nginx entered the nginx folder and exited. So you are back to the previous state before launching the sudo command.



    It's the same than doing sudo ls nginx/* for exemple which will list the content of the nginx folder and exit.



    sudo spawns a new process with the requested privileges and running the requested command. Once the command finish, sudo exits and you fall back to previous situation.



    To dig more, you have to understand that your location inside a file system, is dynamically managed by the shell program you are using. When a process is forked the shell context is replicated in the new process. Every manipulation of the copied context is specific to it, and is not affecting the parent context. So in our case, cd nginx is modifying the $PWD of the new process and not the parent's one. Once it finish, the related context is destroyed and then, you fallback in the parent's context which wasnt modified.



    As the other dude explained to you, to be able to move into a folder you need to also have the execution permission (+x).






    share|improve this answer























    • Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
      – Xiaochen Cui
      Dec 7 '17 at 7:38










    • If you read the last paragraph of the answer, you have an explanation of what happens and why so.
      – netmonk
      Dec 7 '17 at 7:45














    3












    3








    3






    The main reason is that sudo cd nginx entered the nginx folder and exited. So you are back to the previous state before launching the sudo command.



    It's the same than doing sudo ls nginx/* for exemple which will list the content of the nginx folder and exit.



    sudo spawns a new process with the requested privileges and running the requested command. Once the command finish, sudo exits and you fall back to previous situation.



    To dig more, you have to understand that your location inside a file system, is dynamically managed by the shell program you are using. When a process is forked the shell context is replicated in the new process. Every manipulation of the copied context is specific to it, and is not affecting the parent context. So in our case, cd nginx is modifying the $PWD of the new process and not the parent's one. Once it finish, the related context is destroyed and then, you fallback in the parent's context which wasnt modified.



    As the other dude explained to you, to be able to move into a folder you need to also have the execution permission (+x).






    share|improve this answer














    The main reason is that sudo cd nginx entered the nginx folder and exited. So you are back to the previous state before launching the sudo command.



    It's the same than doing sudo ls nginx/* for exemple which will list the content of the nginx folder and exit.



    sudo spawns a new process with the requested privileges and running the requested command. Once the command finish, sudo exits and you fall back to previous situation.



    To dig more, you have to understand that your location inside a file system, is dynamically managed by the shell program you are using. When a process is forked the shell context is replicated in the new process. Every manipulation of the copied context is specific to it, and is not affecting the parent context. So in our case, cd nginx is modifying the $PWD of the new process and not the parent's one. Once it finish, the related context is destroyed and then, you fallback in the parent's context which wasnt modified.



    As the other dude explained to you, to be able to move into a folder you need to also have the execution permission (+x).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 7 '17 at 7:50

























    answered Dec 7 '17 at 7:36









    netmonk

    1,1741516




    1,1741516












    • Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
      – Xiaochen Cui
      Dec 7 '17 at 7:38










    • If you read the last paragraph of the answer, you have an explanation of what happens and why so.
      – netmonk
      Dec 7 '17 at 7:45


















    • Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
      – Xiaochen Cui
      Dec 7 '17 at 7:38










    • If you read the last paragraph of the answer, you have an explanation of what happens and why so.
      – netmonk
      Dec 7 '17 at 7:45
















    Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
    – Xiaochen Cui
    Dec 7 '17 at 7:38




    Thanks for your help but can you explain why sudo cd nginx will execute exit operate?
    – Xiaochen Cui
    Dec 7 '17 at 7:38












    If you read the last paragraph of the answer, you have an explanation of what happens and why so.
    – netmonk
    Dec 7 '17 at 7:45




    If you read the last paragraph of the answer, you have an explanation of what happens and why so.
    – netmonk
    Dec 7 '17 at 7:45













    2














    A user has to have execute permissions on a directory to be able to change to it.



    See e.g. the question "To cd into a directory".



    To change to the directory as your ordinary user (not nginx or root), you will first have to sudo chmod o+x nginx. You may also start a root shell with sudo -s and access it from that shell session.





    sudo cd nginx doesn't make much sense. The cd will succeed, but as soon as the sudo process exits, you will be left in the directory from where you executed sudo. The current working directory is part of the process' environment, and a child process (sudo) can't change the environment of its parent (your interactive shell).






    share|improve this answer




























      2














      A user has to have execute permissions on a directory to be able to change to it.



      See e.g. the question "To cd into a directory".



      To change to the directory as your ordinary user (not nginx or root), you will first have to sudo chmod o+x nginx. You may also start a root shell with sudo -s and access it from that shell session.





      sudo cd nginx doesn't make much sense. The cd will succeed, but as soon as the sudo process exits, you will be left in the directory from where you executed sudo. The current working directory is part of the process' environment, and a child process (sudo) can't change the environment of its parent (your interactive shell).






      share|improve this answer


























        2












        2








        2






        A user has to have execute permissions on a directory to be able to change to it.



        See e.g. the question "To cd into a directory".



        To change to the directory as your ordinary user (not nginx or root), you will first have to sudo chmod o+x nginx. You may also start a root shell with sudo -s and access it from that shell session.





        sudo cd nginx doesn't make much sense. The cd will succeed, but as soon as the sudo process exits, you will be left in the directory from where you executed sudo. The current working directory is part of the process' environment, and a child process (sudo) can't change the environment of its parent (your interactive shell).






        share|improve this answer














        A user has to have execute permissions on a directory to be able to change to it.



        See e.g. the question "To cd into a directory".



        To change to the directory as your ordinary user (not nginx or root), you will first have to sudo chmod o+x nginx. You may also start a root shell with sudo -s and access it from that shell session.





        sudo cd nginx doesn't make much sense. The cd will succeed, but as soon as the sudo process exits, you will be left in the directory from where you executed sudo. The current working directory is part of the process' environment, and a child process (sudo) can't change the environment of its parent (your interactive shell).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 7 '17 at 9:58

























        answered Dec 7 '17 at 7:36









        Kusalananda

        121k16229372




        121k16229372






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409401%2fcant-access-a-folder-with-sudo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre