Determining home dir of a user

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I need to know how home directory for a linux user is determined. Everyone's answer would be "hey, look at /etc/passwd and find it out". But this seems not to be always correct. Look at this:



$ whoami
test
$ cat /etc/passwd
test:x:2000:2000::/home/test:/bin/bash (relevant line)
$ echo ~
/root
$ cd
bash: cd: /root: No such file or directory


I really have no idea what is going on. It is a subsystem which I entered as root by chroot --userspec=test path/to/subsystem/ /bin/bash. Does anyone know what this is and how to fix it so that ~ expands to /home/test and cd works as expected?







share|improve this question




















  • Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
    – igal
    Oct 19 '17 at 23:56














up vote
3
down vote

favorite












I need to know how home directory for a linux user is determined. Everyone's answer would be "hey, look at /etc/passwd and find it out". But this seems not to be always correct. Look at this:



$ whoami
test
$ cat /etc/passwd
test:x:2000:2000::/home/test:/bin/bash (relevant line)
$ echo ~
/root
$ cd
bash: cd: /root: No such file or directory


I really have no idea what is going on. It is a subsystem which I entered as root by chroot --userspec=test path/to/subsystem/ /bin/bash. Does anyone know what this is and how to fix it so that ~ expands to /home/test and cd works as expected?







share|improve this question




















  • Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
    – igal
    Oct 19 '17 at 23:56












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I need to know how home directory for a linux user is determined. Everyone's answer would be "hey, look at /etc/passwd and find it out". But this seems not to be always correct. Look at this:



$ whoami
test
$ cat /etc/passwd
test:x:2000:2000::/home/test:/bin/bash (relevant line)
$ echo ~
/root
$ cd
bash: cd: /root: No such file or directory


I really have no idea what is going on. It is a subsystem which I entered as root by chroot --userspec=test path/to/subsystem/ /bin/bash. Does anyone know what this is and how to fix it so that ~ expands to /home/test and cd works as expected?







share|improve this question












I need to know how home directory for a linux user is determined. Everyone's answer would be "hey, look at /etc/passwd and find it out". But this seems not to be always correct. Look at this:



$ whoami
test
$ cat /etc/passwd
test:x:2000:2000::/home/test:/bin/bash (relevant line)
$ echo ~
/root
$ cd
bash: cd: /root: No such file or directory


I really have no idea what is going on. It is a subsystem which I entered as root by chroot --userspec=test path/to/subsystem/ /bin/bash. Does anyone know what this is and how to fix it so that ~ expands to /home/test and cd works as expected?









share|improve this question











share|improve this question




share|improve this question










asked Oct 19 '17 at 23:14









karlosss

20416




20416











  • Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
    – igal
    Oct 19 '17 at 23:56
















  • Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
    – igal
    Oct 19 '17 at 23:56















Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
– igal
Oct 19 '17 at 23:56




Related post: Chrooted user does not start in his home directory and does not load his bash_profiles
– igal
Oct 19 '17 at 23:56










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










Looking up in /etc/passwd is correct for local user accounts (unless the system administrator has gone out of their way to make things difficult). The correct general answer is to look up in the user database that contains the user account, e.g. LDAP. Most modern systems use NSS to list user databases, so check /etc/nsswitch.conf on your system if you think there might be non-local accounts.



From an application's perspective, the correct answer is that the home directory is whatever the HOME environment variable says.



Login programs (login, sshd, X display managers, etc.) normally set the HOME environment variable to the home directory of the user that's logging in. “High-level” user change programs such as su and sudo change HOME to (with sudo, it depends on the configuration and the command line options). So normally, the definition for “home directory of the current user” matches with the combination of “user's home directory” and “current user”.



But chroot doesn't change HOME, so your session in the chroot inherits from the HOME environment variable in the parent process, which is unsurprisingly /root. In bash, like any other shell, ~ expands to the value of HOME, defaulting to the user database lookup if HOME is not set.



Solution: unset HOME.



env -u HOME chroot --userspec=test path/to/subsystem/ /bin/bash





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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f399237%2fdetermining-home-dir-of-a-user%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    Looking up in /etc/passwd is correct for local user accounts (unless the system administrator has gone out of their way to make things difficult). The correct general answer is to look up in the user database that contains the user account, e.g. LDAP. Most modern systems use NSS to list user databases, so check /etc/nsswitch.conf on your system if you think there might be non-local accounts.



    From an application's perspective, the correct answer is that the home directory is whatever the HOME environment variable says.



    Login programs (login, sshd, X display managers, etc.) normally set the HOME environment variable to the home directory of the user that's logging in. “High-level” user change programs such as su and sudo change HOME to (with sudo, it depends on the configuration and the command line options). So normally, the definition for “home directory of the current user” matches with the combination of “user's home directory” and “current user”.



    But chroot doesn't change HOME, so your session in the chroot inherits from the HOME environment variable in the parent process, which is unsurprisingly /root. In bash, like any other shell, ~ expands to the value of HOME, defaulting to the user database lookup if HOME is not set.



    Solution: unset HOME.



    env -u HOME chroot --userspec=test path/to/subsystem/ /bin/bash





    share|improve this answer
























      up vote
      5
      down vote



      accepted










      Looking up in /etc/passwd is correct for local user accounts (unless the system administrator has gone out of their way to make things difficult). The correct general answer is to look up in the user database that contains the user account, e.g. LDAP. Most modern systems use NSS to list user databases, so check /etc/nsswitch.conf on your system if you think there might be non-local accounts.



      From an application's perspective, the correct answer is that the home directory is whatever the HOME environment variable says.



      Login programs (login, sshd, X display managers, etc.) normally set the HOME environment variable to the home directory of the user that's logging in. “High-level” user change programs such as su and sudo change HOME to (with sudo, it depends on the configuration and the command line options). So normally, the definition for “home directory of the current user” matches with the combination of “user's home directory” and “current user”.



      But chroot doesn't change HOME, so your session in the chroot inherits from the HOME environment variable in the parent process, which is unsurprisingly /root. In bash, like any other shell, ~ expands to the value of HOME, defaulting to the user database lookup if HOME is not set.



      Solution: unset HOME.



      env -u HOME chroot --userspec=test path/to/subsystem/ /bin/bash





      share|improve this answer






















        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        Looking up in /etc/passwd is correct for local user accounts (unless the system administrator has gone out of their way to make things difficult). The correct general answer is to look up in the user database that contains the user account, e.g. LDAP. Most modern systems use NSS to list user databases, so check /etc/nsswitch.conf on your system if you think there might be non-local accounts.



        From an application's perspective, the correct answer is that the home directory is whatever the HOME environment variable says.



        Login programs (login, sshd, X display managers, etc.) normally set the HOME environment variable to the home directory of the user that's logging in. “High-level” user change programs such as su and sudo change HOME to (with sudo, it depends on the configuration and the command line options). So normally, the definition for “home directory of the current user” matches with the combination of “user's home directory” and “current user”.



        But chroot doesn't change HOME, so your session in the chroot inherits from the HOME environment variable in the parent process, which is unsurprisingly /root. In bash, like any other shell, ~ expands to the value of HOME, defaulting to the user database lookup if HOME is not set.



        Solution: unset HOME.



        env -u HOME chroot --userspec=test path/to/subsystem/ /bin/bash





        share|improve this answer












        Looking up in /etc/passwd is correct for local user accounts (unless the system administrator has gone out of their way to make things difficult). The correct general answer is to look up in the user database that contains the user account, e.g. LDAP. Most modern systems use NSS to list user databases, so check /etc/nsswitch.conf on your system if you think there might be non-local accounts.



        From an application's perspective, the correct answer is that the home directory is whatever the HOME environment variable says.



        Login programs (login, sshd, X display managers, etc.) normally set the HOME environment variable to the home directory of the user that's logging in. “High-level” user change programs such as su and sudo change HOME to (with sudo, it depends on the configuration and the command line options). So normally, the definition for “home directory of the current user” matches with the combination of “user's home directory” and “current user”.



        But chroot doesn't change HOME, so your session in the chroot inherits from the HOME environment variable in the parent process, which is unsurprisingly /root. In bash, like any other shell, ~ expands to the value of HOME, defaulting to the user database lookup if HOME is not set.



        Solution: unset HOME.



        env -u HOME chroot --userspec=test path/to/subsystem/ /bin/bash






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 19 '17 at 23:51









        Gilles

        508k12010031533




        508k12010031533



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399237%2fdetermining-home-dir-of-a-user%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay