Set default directory in linux

How to Change Default User Home Directory in Linux

Before we can jump into changing the default user home directory on a Linux operating system environment, we should brief through some theoretical and practical information related to the Linux home directory.

By definition, Linux is a multi-user operating system, which creates the need for a universal directory called the Home directory where different OS users can store and create personalized/user-centered files and directories.

These files and directories are only accessible to the homeowner (currently logged-in user). Therefore, each time a new user is created on a Linux environment, the user is associated with a unique home directory accessible only to that user.

On the other hand, the existence of these universal directories also permits a universal user (other than the homeowner) to have access to them. This user is known as root and can access all homeowner directories within a Linux system.

The syntax associated with a user’s Home directory is as follows:

For instance, if a Linux system has a user called dnyce, then the absolute path to this user’s Home directory will be:

We can even list the permission settings associated with the user in the following manner:

List User Home Directory

This article will walk us through changing the default user home directory on a Linux operating system environment.

Benefits of Having a Linux Home Directory

We can summarize the benefits of having a Linux Home directory in the following manner.

  • User Access Restriction – It is only the homeowner and root user that can access this directory making it impossible for other users to tamper with existing customization settings and stored configuration files. Therefore, privacy and security are guaranteed.
  • Controlled Environment – Since multiple homeowners can exist within a single Linux system, their restriction on their Home directories is very advantageous. For instance, if a homeowner’s Home directory contains malicious code in stored executable files, the execution of this code will only affect that user environment and not the entire system.
  • Controlled Flexibility – The user’s Home directory is automatically created during the Linux installation. During this installation process, a user can decide to assign this directory an exclusive partition. This step makes it easier to execute backup and restoration tasks.

Changing Default User Home Directory in Linux

We will need to create a sample Linux user account for this tutorial to be more practical. Since creating a new user in Linux is associated with a Home directory, let us create one.

# useradd -m homeowner # passwd homeowner

The -m flag makes sure that a default user Home directory is created and set a password to the user.

Create New User in Linux

We can now switch to the newly created user account and confirm the default user home directory.

Check User Home Directory

Switch back to the root user account.

and create a new directory replacement for the default user Home directory.

We will assign user homeowner directory ownership for the newowner.

# chown -R homeowner:homeowner /home/newowner

Now, to change the user’s default Home directory from /home/homeowner to the new directory /home/newowner, we will make use of the usermod -d command in the following manner.

# usermod -d /home/newowner homeowner

The -d flag sets the directory.

Читайте также:  Open source linux project

Change Linux User Home Directory

We can switch back to the homeowner account and confirm the changes.

Confirm User Home Directory

Alternatively, we could set the preferred user Home directory during user creation in the following manner.

# useradd -m -d /home/newowner homeowner

The -d flag sets the directory.

While changing the default user Home directory, we might also need to migrate/move already existing user content to the new location. The usermod command to implement is:

# usermod -m -d /home/newowner homeowner

The -m flag moves the old directory content to the new one.

We can now easily change the default Home directory in Linux. I hope this article guide was helpful. Feel free to leave a comment or feedback.

Источник

How to Change the Default Home Directory of a User

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In this tutorial, we’re going to see how to change the default home directory of a user on Linux. By default, it’s /home/.

We’ll show how to change it for a new user, as well as move the existing content to a new location.

2. Creating a User

Let’s begin by creating a user called baeldung, in the default way, using the useradd command:

We used -m so that useradd would create the home directory at the default location if it doesn’t already exist.

Note that we needed sudo since we’ll require root permissions to create and modify the user accounts configuration.

We can also create a user and set the location for the home directory at the same time by adding the argument -d:

$ sudo useradd -m -d /home/baeldung baeldung

3. Changing the Default Home Directory

So, we’ve created the baeldung user, and its home directory is /home/baeldung.

Now we’re going to change the user’s home directory to /usr/baeldung with usermod -d:

$ sudo usermod -d /usr/baeldung baeldung

The usermod command modifies a user account information. We specify the desired home directory with the -d parameter.

Let’s check if we correctly changed the home directory:

Actually, there’s a problem with the usermod command that we’ve just run.

We could have some files already created in our previous home folder. So, we’ll waste space in disk if we forget to delete or move those files in the future.

Let’s modify our command then to also move the existing content to the new location with -m:

sudo usermod -m -d /usr/baeldung baeldung

We can now see that our files for the user baeldung have been moved to /usr/baeldung and the old directory deleted.

4. Conclusion

In this tutorial, we’ve introduced two commands, useradd and usermod, which we can use to add and then modify the user and their home directory. We saw how to move the user’s old home directory content in the process.

Читайте также:  Red hat enterprise linux kernel

Источник

Command to change the default home directory of a user [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

I would like to know whether there is any simple shell command to change the user home directory in Linux/Unix (one similar to chsh which changes the default login shell of an existing valid user) without touching the /etc/passwd file. Thanks

stackoverflow.com/questions/20071714/…, says that usermod -d /home/userxx /home/useryy to change linux default login directory to /home/useryy

Why do you ask about the default home directory? Do you want to change the home directory of one particular existing user, or of all users added in the future? Notice that chsh don’t change the default login shell, but just the login shell of a given user. To change the HOME of a particular user, just edit /etc/passwd

I’m voting to close this question because it has nothing to do with programming and would have been best suited for Unix & Linux or Super User.

7 Answers 7

Ibrahim’s comment on the other answer is the correct way to alter an existing user’s home directory.

Change the user’s home directory:

usermod -d /newhome/username username 

usermod is the command to edit an existing user.
-d (abbreviation for —home ) will change the user’s home directory.

Change the user’s home directory + Move the contents of the user’s current directory:

usermod -m -d /newhome/username username 

-m (abbreviation for —move-home ) will move the content from the user’s current directory to the new directory.

Not only move the content, I see it more like move whole folder = original folder ( /home/username typically) is removed.

@STW Is this a reversible task? Meaning if I just run it once and then flip the parameters, will my home directory be exactly the way it was before?

The order of options for the second command is wrong. usermod -m -d /newhome/username username works.

@KolonUK because that’s the only correct way really. When you are logged in as the user, you have applications opened which rely on that home directory. You shouldn’t be changing the location while everything is running. So yeah, creating a new user account and deleting is not that bad at all. Or you could set a password for root and login as root in a new TTY.

Simply open this file using a text editor, type:

The default home directory defined by HOME variable, find line that read as follows:

Save and close the file. Now you can add user using regular useradd command:

# useradd vivek # passwd vivek 

usermod -m -d /path/to/new/login/home/dir user changes existing user home directory to a new login directory which is created if it does not already exist, option -m moves the contents of the current home directory to the new home dir

The accepted answer is faulty, since the contents from the initial user folder are not moved using it. I am going to add another answer to correct it:

sudo usermod -d /newhome/username -m username 

You don’t need to create the folder with username and this will also move your files from the initial user folder to /newhome/username folder.

Читайте также:  От linux админа до devops

In case other readers look for information on the adduser command.

Edit the user home directory and then move the required files and directories to it:

cp/mv -r /home/$user/.bash* /home/newdir .bash_profile .ssh/ 

Set the correct permission

chmod -R $user:$user /home/newdir/.bash* 

Found out that this breaks some applications, the better way to do it is

In addition to symlink, on more recent distros and filesystems, as root you can also use bind-mount:

mkdir /home/username mount --bind --verbose /extra-home/username /home/username 

This is useful for allowing access «through» the /home directory to subdirs via daemons that are otherwise configured to avoid pathing through symlinks (apache, ftpd, etc.).

You have to remember (or init script) to bind upon restarts, of course.

An example init script in /etc/fstab is

/extra-home/username /home/username none defaults,bind 0 0 

Источник

Setting default path when opening a terminal session

I am new to Ubuntu. Whenever I open a terminal my current working directory /home/Varun (as found by typing pwd ). Is there any way I can make the terminal’s path to be set as /home/Varun/Desktop/Java Files when it opens?

8 Answers 8

Just run the following command in your terminal:

echo "cd ~/Desktop/Java\ Files" >> ~/.bashrc 

The above command will add a new line in your ~/.bashrc file that contain cd ~/Desktop/Java\ Files and that will change your default working directory to /home/Varun/Desktop/Java Files when you will open the terminal.

Reopen the terminal and you will see the difference.

But whenever you need open through a file manager, it’ll still open the path you gave in ~/.bashrc file. So, this is not the best option.

You can add the following line to the end of your ~/.bashrc

Although changing the $HOME variable and calling cd command (i’ll use cmd for short) in .bashrc file is right answer to your question,

i find it more comfortable to create alias (for example cdh ) which takes me directly to directory i want.

The reason is that all files which configures other programs (Just like .bashrc for example) stay in default $HOME directory and i can work in my » cdh directory» without interuption from theese files.

If i needed to go back to $HOME directory i can allways use cd cmd.

In some linux distros the .bashrc file is shipped with command or commands which runs or run one or multiple other files intended for that specific use (for example .bash_aliases )

so decide for yourself if you want to use them or not,

in case you want to use them, just use it the same as you use .bashrc but with commands inteded for the specified file.

so in .bashrc (or in .bash_aliases or whichever file you’ve chosen)

alias cdh='cd /home/Varun/Desktop/Java Files' 

if you don’t like cdh alias don’t be afraid to use different name but make sure there isn’t any other cmd or alias named like this, couse you could make that cmd more or less unusable.

You can check if the name is taken by triyng to call it but i would sugest a type cmd with argument of name of another cmd.

The type cmd should tell you if the given cmd is alias, binary file, or bash script, or . whatever. And therefore will tell you when cmd doesn’t exist. (Which is what you want in this case)

Источник

Оцените статью
Adblock
detector