Linux System Administration

0 of 85 lessons complete (0%)

Managing User Accounts

Homes Directory in Linux

You don’t have access to this lesson

Please register or sign in to access the course content.

Home Directory in Linux

The home directory is the default directory assigned to a user in a Linux system where personal files, configurations, and settings are stored. When a user logs in, they are placed in their home directory, typically located under the /home directory with a subdirectory named after the user.

For example:

  • User john would have a home directory at /home/john.

The home directory provides a secure and private space for each user to work, ensuring that their files and settings are isolated from other users on the system.

Features of the Home Directory

  • User-specific Environment: Each user’s home directory contains personal files, such as documents, media files, and configuration files (like .bashrc, .profile).
  • Default Permissions: By default, a user’s home directory is only accessible by that user and the root user. Other users typically don’t have access to this directory unless permissions are changed.
  • Customization: Users can customize their working environment within their home directory without affecting the rest of the system.

Changing the Home Directory

There might be scenarios where you want to change the location of a user’s home directory, either because you need to move it to another partition, or because you’re restructuring the file system.

1. Using the usermod Command

The usermod command allows you to change a user’s home directory.

Steps to Change the Home Directory:

Create the New Directory:First, create the new home directory if it doesn’t already exist.

sudo mkdir /new_home_directory

Move the User’s Files:Copy the contents of the old home directory to the new location:

sudo cp -r /home/username/* /new_home_directory/ 

The -r option copies files recursively, preserving the directory structure.

Change the Home Directory Path:Use the usermod command to update the user’s home directory path.

sudo usermod -d /new_home_directory username 

This command updates the user’s home directory to /new_home_directory.

Change Ownership of the New Directory:Ensure that the new directory is owned by the user:

sudo chown -R username:username /new_home_directory 

The -R option ensures that ownership is applied recursively to all files and subdirectories.

Update Login Session:To make sure the changes take effect, you can either log out and log back in, or reboot the system.

sudo reboot

(Optional) Delete the Old Home Directory:After verifying that everything works correctly, you can remove the old home directory if no longer needed.

sudo rm -r /home/username

2. Editing /etc/passwd File

Alternatively, you can manually change the home directory by editing the /etc/passwd file, which contains user account information, including the home directory path.

Steps to Change the Home Directory via /etc/passwd:

  1. Edit the /etc/passwd File:Open the file with a text editor: sudo nano /etc/passwd
  2. Locate the User’s Entry:Find the line corresponding to the user whose home directory you want to change. It will look something like this: username:x:1001:1001:FullName,,,:/home/username:/bin/bash
  3. Modify the Home Directory Path:Change the home directory path (the second-to-last field) to the new location: username:x:1001:1001:Full Name,,,:/new_home_directory:/bin/bash
  4. Save the Changes:After editing, save the file and exit.
  5. Move Files and Update Ownership:As described in the usermod method, move the user’s files to the new directory and update ownership using the cp and chown commands.

Verifying the Change

Once you’ve changed the home directory, you can verify it by checking the user’s information with the getent command:

getent passwd username

The output should reflect the new home directory path.


Examples and Practical Uses of Common Configuration Files in the Home Directory

Here are examples and explanations of commonly used configuration files found in a user’s home directory, such as .profile, .bashrc, and .ssh/config. These examples will help demonstrate their practical purposes.

1. .profile

Purpose: The .profile file is a shell script that runs during user login, typically for setting environment variables and configuring the user environment.

Example:

# ~/.profile

# Set PATH environment variable
export PATH="$PATH:$HOME/bin"

# Set environment variables
export EDITOR=nano
export LANG=en_US.UTF-8

# Source other configuration files
if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
fi

Explanation:

  • export PATH="$PATH:$HOME/bin": Adds the bin directory in the user’s home directory to the system’s PATH variable, allowing executable files in that directory to be run from the command line without specifying their full path.
  • export EDITOR=nano: Sets the default text editor to nano.
  • export LANG=en_US.UTF-8: Sets the system language and locale.
  • if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc"; fi: Sources the .bashrc file if it exists, which is commonly used for interactive shell configurations.

2. .bashrc

Purpose: The .bashrc file is used for setting up user-specific shell settings for interactive shells. It’s executed each time a new terminal session is started.

Example:

# ~/.bashrc

# Define custom prompt
PS1='[\u@\h \W]\$ '

# Create aliases
alias ll='ls -la'
alias gs='git status'

# Set custom PATH
export PATH="$HOME/scripts:$PATH"

# Enable color support for ls
alias ls='ls --color=auto'

Explanation:

  • PS1='[\u@\h \W]\$ ': Customizes the shell prompt to show the username, hostname, and current directory.
  • alias ll='ls -la': Creates an alias ll for ls -la to list files with detailed information.
  • export PATH="$HOME/scripts:$PATH": Adds a scripts directory in the user’s home directory to the PATH.
  • alias ls='ls --color=auto': Enables color output for the ls command.

3. .ssh/config

Purpose: The .ssh/config file is used to configure SSH client options and manage different SSH connections.

Example:

# ~/.ssh/config

# Default SSH settings
Host *
    ForwardAgent yes
    ForwardX11 yes
    ServerAliveInterval 60

# Configuration for a specific host
Host example
    HostName example.com
    User myusername
    Port 22
    IdentityFile ~/.ssh/id_rsa

Explanation:

  • Host *: Applies settings to all SSH connections.
  • ForwardAgent yes: Allows SSH agent forwarding.
  • ForwardX11 yes: Enables X11 forwarding for graphical applications.
  • ServerAliveInterval 60: Sends a keep-alive message every 60 seconds to prevent disconnection.

Try it on your own

Practical Demonstrations

1. Configuring .profile:

  • Edit .profile to add a custom PATH and environment variables.
  • Log out and log back in or source the .profile file to apply changes: source ~/.profile
  • Use commands like echo $PATH and echo $EDITOR to verify the changes.

2. Customizing .bashrc:

  • Add aliases and modify the prompt in .bashrc.
  • Open a new terminal session to see the changes take effect.
  • Use the ll alias and observe the color output in ls command results.

3. Setting Up .ssh/config:

  • Create a new configuration entry for a specific host in .ssh/config.
  • Use the ssh example command to connect, and observe that it uses the specified settings and key file.