I added a user (user2) in a WSL2 environment and then wanna change that user2's home directory to a different directory. But something must be wrong.

$ usermod -d /var/lib/app1 user2
usermod: no changes
$ vim /etc/passwd
$ sudo su user2

It wasn't /var/lib/app1 but /home/abc.

What am I missing here?

  • @Kulfy Sorry. I am not familliar about linux(ubuntu), so I cannot understand 'the owner of /var/lib/app1'. Could you tell me about it easily? default user and subtitle user are both me. I wrote that subtitle user is 'user2', but exactly 'airflow' For airflow application, I add subtitle user 'airflow' and 'airflow initdb' in /var/lib/airflow. – vinsh_77 Aug 24 at 8:56
  • Do you mean when you run sudo su user2 you're still in user1's home, i.e., prompt is like user2@host:/home/user1? – Kulfy Aug 24 at 9:17
  • @Kulfy Yes! sudo su user2; pwd then /home/user1. – vinsh_77 Aug 24 at 9:44
  • Did my answer answered your question? Also, I see that you changed "substitute user" to "subtitle user". What do you mean by that? – Kulfy Aug 26 at 11:07
  • @Kulfy Yes. With your efforts, I was able to solve the problem I was having. Thank you. Because I noticed that 'su' is 'subtitle user' late, So I changed "substitute user" to "subtitle user" in title. – vinsh_77 Aug 30 at 7:14

That's the intended behavior. su by default doesn't change working directory. It only switches user. According to su manpage:

For backward compatibility, su defaults to not change the current directory and to only set the environment variables HOME and SHELL (plus USER and LOGNAME if the target user is not root).

su does change user and $HOME but it doesn't cd to that user's $HOME.

For example, I have 2 users, kulfy and kulfy1. BASH is the default shell of the earlier one and sh (which is a symlink to DASH) of the later one.

kulfy@leo:~$ echo $HOME
kulfy@leo:~$ sudo su kulfy1
$ echo $USER  
kulfy1  # User changed
$ pwd      
/home/kulfy # but directory didn't change 
$ echo $HOME
/home/kulfy1 # and HOME variable now points to the HOME of user2, i.e., kulfy1

To change the directory as well, you can use --login or simply - with su. From the manpage:

-, -l, --login
        Start the shell as a login shell with an environment similar to a real login:

           o    clears all the environment variables except TERM and variables specified by --whitelist-environment

           o    initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH

           o    changes to the target user's home directory

           o    sets argv[0] of the shell to '-' in order to make the shell a login shell

kulfy@leo:~$ sudo su --login kulfy1 
$ echo $USER
$ pwd
/home/kulfy1  # directory changed automatically
$ echo $HOME
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.