- Change Home Directory in Linux
- Change the home directory using usermod
- Change the home directory by editing /etc/passwd
- How To – Linux Set Environment Variables Command
- Two types of shell variables
- Display current environment variables on Linux
- env command
- How to set and list environment variables in Linux using set command
- Where is the $HOME environment variable set?
- 5 Answers 5
- Command to change the default home directory of a user [closed]
- 7 Answers 7
- Change the user’s home directory:
- Change the user’s home directory + Move the contents of the user’s current directory:
- Not the answer you’re looking for? Browse other questions tagged linux shell unix or ask your own question.
- Linked
- Related
- Hot Network Questions
- How to change my home directory?
- 1 Answer 1
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/
Источник
How To – Linux Set Environment Variables Command
- Configure look and feel of shell.
- Setup terminal settings depending on which terminal you’re using.
- Set the search path such as JAVA_HOME, and ORACLE_HOME.
- Create environment variables as needed by programs.
- Run commands that you want to run whenever you log in or log out.
- Set up aliases and/or shell function to automate tasks to save typing and time.
- Changing bash prompt.
- Setting shell options.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux |
Est. reading time | 11 minutes |
Two types of shell variables
- Environment variables (GLOBAL): Typically, end-users shouldn’t mess with Environment variables as they are available system-wide, and subshells and child processes can access them. In certain scenarios, we can modify them as per our needs. For example, we set up a system-wide path for the JAVA app or PATH for searching binaries. In almost all cases, we use the export command to define or modify environment variables.
- Shell and user-defined variables (LOCAL) : As the name suggests, these are defined by users and currently apply to the current shell session.
You can use the following commands to view and configure the environment.
Display current environment variables on Linux
The printenv command shows all or the given environment variables. Open the terminal prompt and then type:
printenv
printenv VAR_NAME
printenv PS1
printenv ORACLE_HOME
printenv JAVA_HOME
# use the grep command/egrep command to filter out variables #
printenv | grep APP_HOME
printenv | egrep ‘APP_HOME|DOCKER_HOME|NIX_BACKUP_HOST’
env command
The env command runs a Linux command with a modified environment. The syntax is:
Please note that If no command name is specified following the environment specifications, the resulting environment is displayed on screen. This is like specifying the printenv command as discussed earlier. For example:
How to set and list environment variables in Linux using set command
The env command/printenv command displays only the Linux shell environment variables. What if you need to see a list of all variables, including shell, environment, user-defined shell functions? Try the following set command for printing environment variables:
set
set | grep BASH
Here is what we see:
The $PATH defined the search path for commands. It is a colon-separated list of directories in which the shell looks for commands. The $PS1 defines your prompt settings. See the list of all commonly used shell variables for more information. You can display the value of a variable using the printf command or echo command:
Outputs from the last command displaying my home directory location set by the $HOME environment variable on Linux:
/home/vivek
Источник
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
If you are attempting to modify your HOME, you can do
either in your shell, or in your
/.profile file and/or
/.bashrc (or appropriate login shell).
(The above code will work for bash and similar shells, which are default in Debian; you would otherwise do `setenv HOME $HOME:/extra/path I think on csh-like shells in other distros.)
edit — However this is probably not the way to do it. See other answers. Do not use this answer.
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.
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.
I did some digging, and the answer to this is a bit surprising. Take the following test script and chmod +x it:
We can run it with ./test.sh and see:
Let’s take a peek under the hood with strace.
$ strace ./test.sh |& grep ‘^open[a-z]*’
openat(AT_FDCWD, «/etc/ld.so.cache», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libtinfo.so.5», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libdl.so.2», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libc.so.6», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/dev/tty», O_RDWR|O_NONBLOCK) = 3
openat(AT_FDCWD, «/usr/lib/locale/locale-archive», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache», O_RDONLY) = 3
openat(AT_FDCWD, «./test.sh», O_RDONLY) = 3
I do not see any mention of HOME, rc files, or passwd. Let’s try it with a clean env:
Nothing, as expected. Let’s run the script in the blank env.
Interesting, the script is able to get home. Now let’s trace.
openat(AT_FDCWD, «/etc/ld.so.cache», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libtinfo.so.5», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libdl.so.2», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libc.so.6», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/dev/tty», O_RDWR|O_NONBLOCK) = 3
openat(AT_FDCWD, «/etc/nsswitch.conf», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/etc/ld.so.cache», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libnss_compat.so.2», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/etc/ld.so.cache», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libnss_nis.so.2», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libnsl.so.1», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/lib/x86_64-linux-gnu/libnss_files.so.2», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «/etc/passwd», O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, «./test.sh», O_RDONLY) = 3
I’ve bolded the interesting lines. As we can see, it would appear that when $HOME is not defined, the shell will try to fill it in, even when not in login or interactive mode.
Источник
Command to change the default home directory of a user [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 9 months ago .
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
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 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.
An example init script in /etc/fstab is
You can do it with:
Edit the user home directory and then move the required files and directories to it:
Set the correct permission
usermod -m -d /newhome username
Not the answer you’re looking for? Browse other questions tagged linux shell unix or ask your own question.
Linked
Related
Hot Network Questions
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
How to change my home directory?
I have a current /home/user/ directory for
but I want to change it to be at /user/home/
/user/home already exists.
The option of using usermod is not going to work because I don’t have access to the system as root or as another user.
I am asking for a solution along the lines of modifying some .bashrc file and changing some environment variable or smth similar. I log in via ssh.
I’m running Ubuntu 14.04.
Thank you in advance
Solutions like the ones below unfortunately aren’t applicable to my case:
EDIT
I thought I’d give some more info here rather than respond to the comments.
Currently the folder structure is a lot stranger than my example above, but the jist of it is the same. Ie currently when I do:
so when I use things like pip with the —user tag it will install things locally.
Because there are some memory limitations as well as ssh issues with writing to that location (after some time I can no longer write) I would like to have the following behaviour:
/path/of/new/home/ already exists and doesn’t have the limitations set above.
1 Answer 1
Well, you could just add this line to your
However, that really isn’t a good idea. Problems it would cause include (but are probably not limited to):
That will only work if /home/user is owned by your user. If it isn’t, you won’t even be able to log in.
This will work for your user only. For everyone else, your home directory will be whatever is stored in /etc/passwd . This means that, for example, cd
user will fail. In other words, if I log in as bob and bob has the line HOME=/home/bob/foo in
/.profile , then bob thinks that his home directory is /home/bob/foo but nobody else knows that:
So far so good. But:
- This will drive your sysadmin insane. You do not want to anger your sysadmin for you are crunchy and taste good with ketchup.
In any case, it is rarely a good idea to mess with variables like $HOME , as it can often have unintended consequences. Instead, a much cleaner solution would be to make sure every new shell session starts in the target directory. Just add this line to your
Now, each time you log 2 in or open a terminal, you will find yourself in /user/home .
/.bash_profile if it exists.
2 Log in to Debian-based systems like Ubuntu, anyway. For other distributions/OSs, you might need to add it to
Источник