- Linux Home Directory | Explained
- What is the Purpose of a Home Directory, and Where is it Located?
- How to Access the Home Directory?
- Example 1: Access Home Directory Using cd /home Command
- Example 2: Using cd $HOME
- Example 3: Using cd “~(tilde)” Slash
- How to Check the Home Directory Permissions?
- Conclusion
- Where is the $HOME environment variable set?
- 5 Answers 5
- Linux Home Directory
- How Linux Home Directory Command Works?
- Examples to Implement Linux Home Directory
- 1. Home Directory
- 2. Home Directory with cd Command
- 3. Home Directory with Tilde Option
- 4. Home Directory with HOME Variable
- Conclusion
- Recommended Articles
Linux Home Directory | Explained
In Linux, the Home directory (also called the “Login Directory”) is the default directory of a user. It contains the specific user’s files, data, and programs. Generally, it is the first workplace when the user logs in to the Linux system. Keeping this in view, this post provides a deep insight into the “home” directory.
The outline of this post is as typed below:
What is the Purpose of a Home Directory, and Where is it Located?
The main purpose of the home directory is also referred to as the personal workspace of a Linux user. Suppose the system has three users “itslinuxfoss”, “anna”, and “milton”. These users have the home directories like “/home/itslinuxfoss”, “/home/anna”, and “/home/milton”.
It is also used to store application configuration files, allowing users to customize their own environment and settings.
The “home” directory is generally present in the “root” directory. The “root” directory is the top-level directory that contains all the subdirectories, directories, and files present in the system. The “root” directory is denoted by the “(forward) slash (/)”.
The “home” directory is followed by the “~tilde” slash as shown below:
It denotes the “home” directory of logged-in user “itslinuxfoss”.
How to Access the Home Directory?
The user can easily access the home directory in the following ways:
Example 1: Access Home Directory Using cd /home Command
Suppose the logged-in “itslinuxfoss” user is in the “test ” directory as shown in the screenshot:
Execute the “cd(change directory)” command followed by the “/home” to move into the “home” directory:
The output shows that the “test” directory has been changed to “home” directory.
Example 2: Using cd $HOME
The user can also switch to the “home” directory by using the “$HOME” shell environment variable with the “cd” command in the following way:
The “$HOME” variable contains the full path of the particular user directory. In this case, it changed the “Downloads” directory to the “home” directory, which is verified by the “pwd” command.
Example 3: Using cd “~(tilde)” Slash
The “~(tilde)” slash is another way to access the “home” directory followed by the “cd” command. Now the “itslinuxfoss” user is in the “New2” present working directory:
Type the following “cd” command to access the “home” directory:
How to Check the Home Directory Permissions?
Each file and directory available in the Linux system has three types of permissions “r(read)”, “w(write)”, and “x(execute)”. These permissions are usually assigned by default and can also be modified per requirements.
To check the “home” directory permissions, use the following “ls” command with the combination of argument “l(list)” and “d(directory)”:
The output shows that the “itslniuxfoss” owner of the “home” directory has all the permissions (rwx). Whereas its associated group “itslinuxfoss” only has the “x(execute)” permission.
These default permissions are better for security as only the “itslinuxfoss” user can read, write and execute files in the “home” directory.
Conclusion
Linux “home” directory is the subdirectory/next-level of the “root” directory denoted by the “~(tilde)” slash. It also refers to the “Login Directory”, which contains the specific user files, directories, and subdirectories. Its default permissions are “750”, meaning only its owner has full rights. This post has briefly described all about the Linux “home” directory.
Where is the $HOME environment variable set?
I’m looking for the place where $HOME environment variable set. It is after login, to my mind. I’m using Linux debian 2.6.32-5-686.
5 Answers 5
(it is defined in /etc/passwd ; edit it with usermod -d /some/new/home/dir myusername , though modifying the file may work)
(the string is passed in as an argument to the login program, which will later set the $HOME env var)
sidenote: When the $HOME variable is not defined, the bash shell will fetch it from its official definition in /etc/passwd and define it in that script. This may indicate that your scripting or programming language may have special treatment for the $HOME variable since it is slightly ‘important’ (though perhaps not as much as $PATH).
You are likely confusing $HOME with $PATH . It does not make sense to have multiple paths in $HOME (the entire value will be treated as a single pathname) or, in most cases, to modify $HOME at all.
On Linux, the HOME environment variable is set by the login program:
- by login on console, telnet and rlogin sessions
- by sshd for SSH connections
- by gdm , kdm or xdm for graphical sessions.
The login program arranges it before calling exec on your shell (by including it in the arguments to exec), based on the value in /etc/passwd.
Interesting this did not get more votes. It’s the only answer that actually specifies where the environment variable is set, which was the OP’s actual question.
Edit this by running: usermod -d /home/whatever_dir whatever_user .
Please note that this will (obviously) be the new home directory. Bash will cd to it on login, so make sure it exists and the permissions are correct. In addition, don’t forget about .bashrc , .profile , .xinitrc , etc; if they’re not in the home directory, they will not be read.
Usage: usermod [options] LOGIN Options: -c, --comment COMMENT new value of the GECOS field -d, --home HOME_DIR new home directory for the user account -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -f, --inactive INACTIVE set password inactive after expiration to INACTIVE -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append the user to the supplemental GROUPS mentioned by the -G option without removing him/her from other groups -h, --help display this help message and exit -l, --login NEW_LOGIN new value of the login name -L, --lock lock the user account -m, --move-home move contents of the home directory to the new location (use only with -d) -o, --non-unique allow using duplicate (non-unique) UID -p, --password PASSWORD use encrypted password for the new password -R, --root CHROOT_DIR directory to chroot into -s, --shell SHELL new login shell for the user account -u, --uid UID new UID for the user account -U, --unlock unlock the user account
Linux Home Directory
In the Linux ecosystem, the home directory is also called as the home directory. It is the primary entry point of the user when they are login into the Linux environment. It is responsible to store files, folders, data, and software on /home directory with the respective individual user profile.
Web development, programming languages, Software testing & others
Syntax of Home Directory
- /home: We can use the /home as the default path for Linux Home Directory. It is the primary or starting path for entering the individual user profile.
- USER NAME: We need to pass the user name in the “/home” path. It will help to enter in the individual user profile path (in terms of the file system).
How Linux Home Directory Command Works?
When we are creating any user in the Linux system. While creating the user 5 different steps will happen. The user directory creation it comes under the same 5 different steps. When any user will be added in the Linux operating system, by default, the user directory will create in the “/home” path with the same user name.
The user directory is also known as the user home directory. It will provide the basic environment of shell and bash. It will help to execute the shell or some application-level jobs.
By default, the list of files will create in the user “/home” directory.
- bash_logout: The file is responsible to perform any action when the user or the terminal will logout.
- bash_profile: The file is responsible to perform any action when the user profile will load the in the Linux environment.
- Bashrc: The file is responsible for the necessary kinds of stuff in it. It will help to the bash or shell or other applications. We can put functions, alias, shell or bash options etc.
As we have seen, the default home directory path will present in the “/home” path. But is not mandatory that, it will always be present over there. As per the requirement, we can change the home directory for the individual user. We can change the default home directory with the help of “usermod” command (as per user level).
Examples to Implement Linux Home Directory
Following are the examples are given below:
1. Home Directory
When we are creating any user on the Linux level, the user home directory will automatically create in the “/home” path with the same user name.
Explanation: As per the above command, we are able to list out the number of user home directories. Before adding any user, there is no home directory available in the environment (refer to screenshot 1 (a)). When we are adding the user in the Linux environment. The home directory will create in “/home” path. The home directory name is similar to the user name only (refer to screenshot 1 (a)). The user can use the same directory to store the number of files, folders, data, etc. It is having the necessary permissions on the files system to store or retrieve the data from the home directory.
As discussed earlier, when any user or home directory will create in the “/home” path. It will create default 3 files in the same working directory i.e. bash_logout, bash_profile and bashrc.
2. Home Directory with cd Command
In the Linux environment, we are having the functionality to come directly in the home directory. For that, we just need to run the simple “cd” command in the shell prompt.
Command:
Explanation: As per the above command, we are simply using the “cd” command. It is a simple command but usable command in the Linux environment. When we are working in any current directory and we need to move from the current working directory to the home directory. We just simply use the “cd” command. As per the below screenshot 2 (a), we are login with the “user1” user and the current working directory is “/home/user1/data”. Now I need to move or go back to the home directory. I have just used the “cd” command in the shell window (refer to screenshot 2 (b)).
3. Home Directory with Tilde Option
We can use the “cd” command to come in the home directory from any location. Similarly, we can achieve the same functionality via the “tilde (~)” option.
Explanation: As have seen the “cd” command concept in the home directory. Similarly, we can see the concept with a tilde sign or option. As per the below screenshot 3 (a), the current working directory is “/home/user1/data”. No matter, in which location we are. If we would need to move from the current working directory to the home directory. We can use the tilde sign with the cd command. As per the above command, we are moving from the current working directory to the home directory (refer to screenshot 3 (b)).
4. Home Directory with HOME Variable
In the Linux environment, we are having the functionality to use the environment variables. With the help of the “HOME” variable, we can directly able to go the home directory.
Note: It will help to call the home directly in different shell jobs or other applications.
Explanation: In Linux, there are lots of global variables. The “#HOME” variable is one of the global variables present in the Linux operating system. As per the above command, we are able to move from any working directory to the home directory. As per the below screenshot 4 (a), we are in “/home/user1/data” directory. As per the current working directory, we are able to move from “/home/user1/data” directory to the home directory (refer screenshot 4 (b)).
Conclusion
We have seen the uncut concept of “Linux Home Directory Command” with the proper example, explanation and command with different outputs. The home directory will define the proper skeleton of the structure. It is the primary authorized storage for the Linux user. When any user will add in the Linux operating system. By default, the home directory will allocate to that user.
Recommended Articles
We hope that this EDUCBA information on “Linux Home Directory” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5