How to change my home directory?

14,030

Well, you could just add this line to your ~/.profile1:

HOME=/user/home/

However, that really isn't a good idea. Problems it would cause include (but are probably not limited to):

  1. That will only work if /home/user is owned by your user. If it isn't, you won't even be able to log in.

  2. This will work for your user only. For everyone else, your home directory will be whatever is stored in /etc/passwd. This means that, for example, cd ~user will fail. In other words, if I log in as bob and bob has the line HOME=/home/bob/foo in ~/.profile, then bob thinks that his home directory is /home/bob/foo but nobody else knows that:

     $ whoami
     bob
     $ echo $HOME
     /home/bob/foo
     $ cd ## this moves to your $HOME
     $ pwd
     /home/bob/foo
    

So far so good. But:

    $ whoami
    terdon
    $ cd ~bob
    $ pwd
    /home/bob
  1. This will drive your sysadmin insane. You do not want to anger your sysadmin for you are crunchy and taste good with ketchup.

In any case, it is rarely a good idea to mess with variables like $HOME, as it can often have unintended consequences. Instead, a much cleaner solution would be to make sure every new shell session starts in the target directory. Just add this line to your ~/.bashrc:

cd /user/home/

Now, each time you log2 in or open a terminal, you will find yourself in /user/home.


1 Or ~/.bash_profile if it exists.

2 Log in to Debian-based systems like Ubuntu, anyway. For other distributions/OSs, you might need to add it to ~/.profile as well.See here.

Share:
14,030

Related videos on Youtube

Арсен Мирзаян
Author by

Арсен Мирзаян

Updated on September 18, 2022

Comments

  • Арсен Мирзаян
    Арсен Мирзаян over 1 year

    I have a current /home/user/ directory for ~ but I want to change it to be at /user/home/

    /user/home already exists.

    The option of using usermod is not going to work because I don't have access to the system as root or as another user.

    I am asking for a solution along the lines of modifying some .bashrc file and changing some environment variable or smth similar. I log in via ssh.

    I'm running Ubuntu 14.04.

    Thank you in advance

    Solutions like the ones below unfortunately aren't applicable to my case:

    How to change my own home directory?

    How to change my default home directory

    https://stackoverflow.com/questions/20797819/command-to-change-the-default-home-directory-of-a-user

    EDIT

    I thought I'd give some more info here rather than respond to the comments.

    Currently the folder structure is a lot stranger than my example above, but the jist of it is the same. Ie currently when I do:

    user@local:~$ ssh user@host
    

    I end up in:

    user@host:~$ 
    user@host:~$ pwd
    /path/of/current/home/
    

    so when I use things like pip with the --user tag it will install things locally.

    Because there are some memory limitations as well as ssh issues with writing to that location (after some time I can no longer write) I would like to have the following behaviour:

    user@local:~$ ssh user@host
    user@host:~$ 
    user@host:~$ pwd
    /path/of/new/home/
    

    /path/of/new/home/ already exists and doesn't have the limitations set above.

    • Арсен Мирзаян
      Арсен Мирзаян over 9 years
      I added some extra info to the question to hopefully address your concerns. Let me know if you still have questions
    • user68186
      user68186 over 9 years
      Thanks for editing the original question rather than putting all that in comments. Ubuntu (and Linux in general) follows strict guidelines on where the user data (and user specific software) can be. It they have to be inside /home/[userID]/... folders. If the problem is disk space, you should consider moving /home/ or some sub-folders to a new partition. Hint, partitions can be mounted as any (sub-)folder. See Move home folder to new partition
    • Арсен Мирзаян
      Арсен Мирзаян over 9 years
      what would happen if I simply changed the $HOME environment variable do you know?
    • user68186
      user68186 over 9 years
      I don't know if that will work.
    • muru
      muru over 9 years
      Seems to me the follow up is more suited to Unix & Linux or Super User than Stack Overflow.
    • Арсен Мирзаян
      Арсен Мирзаян over 9 years
      do you know how I can move the question other than the obvious copy paste?
    • Арсен Мирзаян
      Арсен Мирзаян over 9 years
    • raj
      raj over 3 years
      Definitely the easiest way would be to ask the person who has root access to your system to run the usermod command for you. Everything else are workarounds that may or may not work.