Change linux user home directory

How to change the Home directory of the currently logged In user

I am currently logged in into a CentOS server and I would like to change my home directory from /home/myuserName/ to /var/www/html/ I tried the below command :

> sudo usermod -d /var/www/html myuserName 
usermod: user myUserName is currently logged in 

That helps, But i would like to change my home directory permanently, not for just the current session.

5 Answers 5

short answer : you can’t.

long answer:

HOME dir is set in /etc/passwd , 6th field. It is read upon login; your shell is started with this home dir.

The proper way to change home dir for joe is :

Once session is run, you must do two things:

  • edit $HOME to change home dir for session (to be repeated on all active session).
  • use sudo vipw to edit home dir for next session

Also, be aware you might have an issue with permissions/ownership on /var/www/html .

editing /etc/passwd for an account that is logged in, and then start a new session with that same account, and it obeys the new home dir. For the already logged in account, that session still has the old home location in the Environment.

You need to edit the /etc/passwd file to change home directory of users that are currently logged in.

Edit the /etc/passwd with sudo vipw and change home directory of the user.

vipw highly recommended other than vim or other editors since vipw will set lock to prevent any data corruption.

The usermod command won’t work if you’re logged in with the user you are trying to make changes on.

From the manual page on usermod it says:

CAVEATS usermod will not allow you to change the name of a user who is logged in. You must make certain that the named user is not executing any processes when this command is being executed if the user’s numerical user ID is being changed. You must change the owner of any crontab files manually. You must change the owner of any at jobs manually. You must make any changes involving NIS on the NIS server.

Try logging in with a different user and running the command again.

Читайте также:  Get disk information linux

If that isn’t possible then you can manually edit the /etc/passwd file (which is actually what the usermod command is doing). If you do that make sure you back the file up in case you inadvertently do something silly.

Источник

How to rename Linux users and their home directory

In this tutorial, you will learn how to a rename a user in CentOS, Debian, Ubuntu and most other Linux distributions. More than that, you will also learn how to rename thier home directory, primary group, and change their UID.

Renaming Users in Linux

Linux provides a tool named usermod specifically for making modifications to user accounts. In this case we are using it to rename a user account, which is done using the -l flag.

For example, to rename a user named student1 to johndoe, you would run the usermod command as follows.

sudo usermod -l johndoe student1

The -l flag will only change the user’s name. All other things attached to the user will remain unaffected, such as home directory. and UID.

Changing a User’s Home Directory in Linux

After renaming a user it may make sense to change their home directory, too. Otherwise, it would cause a lot of confusion trying to explain why johndoe ‘s home directory is /home/student .

To change the user’s home directory we once again use the usermod command. However, this time we need to perform to actions: change the home directory to a new path, and move the contents from the old path into the new path.

We set the new home directory path using the -d or —home flag with the path to the new directory. We must also use the -m flag to copy the contents of the old home directory into the new one.

sudo usermod -d /home/joedoe -m johndoe 

Every user on a Linux system is created with a group of the same name. When we change the name of a user their group name is left untouched. It’s a good idea to also change the user’s primary group name as well.

To change a user’s primary group name we use the groupmod command with the -n flag. We must supply the old name and a new name.

Читайте также:  Сжатие оперативной памяти linux

For example, to rename the newly renamed user johndoe ‘s primary group to johndoe from student1 , we would run the following command.

sudo groupmod -n johndoe student1

Changing a User’s UID

A little more rare than renaming a user or changing their home directory is changing their UID. A User’s UID is their unique ID on a Linux system. When we assign permissions to file and directories, we use their UID. Processes started by a user are also executed using a user’s UID.

To change a user’s ID we use the usermod command with the -u flag, followed by a new, unique integer.

For example, to set johndoes UID to 5001, we would run the following usermod command.

sudo usermod -u 5001 johndoe

Источник

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.

Читайте также:  Minecraft linux как установить

@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.

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 

Источник

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