Меню Рубрики

Linux set home directory user

Command to change the default home directory of a user

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

6 Answers 6

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 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:

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

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:

Verify user information:

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:

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.

Set DHOME variable

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:

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.

Источник

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 :

But this gives me an error:

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 :

  • have joe log off.
  • use usermod -d /new/home joe to change home dir for subsequent session.

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 .

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.

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.

Источник

Change Home Directory in Linux

Change the home directory of a Linux user with a simple usermod command. While creating a user if you didn’t specify any –home parameter Linux assumes the home directory of the user to be /home/username even if you did specify you can later change it to something else according to your needs. Apart from changing the home directory using the usermod command you’ll have to assign proper ownership and permissions to the new folder. You can also change the home directory by editing the /etc/passwd file. I’ll outline both the steps here.

Change the home directory using usermod

This method is for command line warriors. Before you use the usermod command the new home directory should be created, ownership should be assigned to the new user and the folder should be chmoded correctly so that no one else can access it. Run the following commands to do it.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory
usermod —home /home/new_home_directory username

Change the home directory by editing /etc/passwd

Alternatively you can also edit the /etc/passwd to change the home directory. But you should be careful not to edit anything else. Before editing this file it is always better to create the new home directory and assign proper permissions and ownership to it. Execute the following commands.
mkdir /home/new_home_directory
chown username:username /home/new_home_directory
chmod 700 /home/new_home_directory

Open the /etc/passwd file using a text editor and locate the line containing the required username it should look something like this
username:x:500:500::/home/username:/bin/bash

change it to
username:x:500:500::/home/new_home_directory:/bin/bash

Save the file.

Finally copy all the old content to the new home directory
cp -f /home/username/* /home/new_home_dir/

Источник

Create default home directory for existing user in terminal

How do I do this? I created a user without a home directory and now I want to create one. Not just a folder called new_user, I want the default home directory with all it’s folders and hidden files, etc.

5 Answers 5

Use the following (as root, or with sudo if not root):

For this to work, folder /home/username must not exist.

For X-related folders (Desktop, Downloads, etc), you will need to login in a graphics environment; they will be automatically generated the first time you login.

The subdirectories (Documents, Downloads, etc. ) are automatically created when the user first logs in through GNOME, provided that the home directory is created with the correct permissions. Here’s a demonstration:

You can check that the user’s home directory is correctly set by checking the entry in /etc/passwd . You should, by default, see the home directory set to /home/testinguser :

If you don’t see the home directory /home/testinguser there, you’ll need to execute the command sudo usermod -d /home/testinguser testinguser to update it, although you should not need to use this command because it should be set by default (according to useradd ‘s manpages).

I then logged out of my account, and logged back in with testinguser , and here are the subdirectories automatically created:

I didn’t need to copy the contents of /etc/skel .

If possible, can you please try following these steps, creating another new user? Once you’re done, you can remove this new user by sudo deluser testinguser && sudo rm -r /home/testinguser .

If all of this did not work with you, then I’m guessing it’s a bug.

Источник

Linux Change Default User Home Directory While Adding A New User

B y default base directory for the system user is set to /home directory. I’d like to add user to /iscsi/home/$ directory instead of the default /home. How do I force useradd command to add user to /iscsi/home under CentOS / RHEL / Fedora Linux server?

Default values for account creation defined in /etc/default/useradd file under CentOS / RHEL / Fedora / Debian / Ubuntu and other Linux distros. Simply open this file using a text editor, type:
# vi /etc/default/useradd
The default home directory defined by HOME variable, find line that read as follows:
HOME=/home
Replace with:
HOME=/iscsi/user
Save and close the file. Now you can add user using regular useradd command:
# useradd vivek
# passwd vivek

Verify user information:
# finger vivek
Output:

How Do I Change Existing User’s Home Directory?

You need to use the usermod command to set the user’s new login directory. The syntax is as follows:

usermod -m -d /path/to/new/home/dir userNameHere

  1. -d dirnanme : Path to new login (home) directory.
  2. -m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

In this example set the user’s new login directory to /users/v/vivek from /home/vivek, enter:
# usermod -m -d /users/v/vivek vivek

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Soft for mac os x
  • Social kit mac os
  • Sniper elite для mac os
  • Snapseed для mac os
  • Snagit для mac os