Cyberithub

Solved "Could not chdir to home directory /home/xxxx" in Linux

Advertisements

In this article, we will see how to solve "Could not chdir to home directory /home/xxxx: No such file or directory" error if you are also getting this. In Linux, every user is linked to its home directory so whenever you login to your Server, it will search the user's home directory under /home location. If Server does not able to find the home directory then it will throw "Could not chdir to home directory /home/xxxx: No such file or directory" error.

But it does not mean you won't be able to login to your System. You will still be able to login but it will change your login location to / path. This can be easily checked by running pwd command immediately after you login.

It is ok if you get the error in some Lab or test server where you can easily delete the user account and create it again with home directory assigned but the problem usually occurs when you have lots of files and directories created using that user account and by the time you realized now you cannot delete the user and recreate it again. Sometimes this becomes a very complicated situation. We will see how to deal with this problem with the help of an example in below section.

Solved "Could not chdir to home directory /home/xxxx" in Linux

Solved "Could not chdir to home directory /home/xxxx" in Linux

Also Read: How to Install AnyDesk on Ubuntu 20.04 LTS (Focal Fossa)

When I tried to login to my Server with testuser account then I noticed "Could not chdir to home directory /home/testuser: No such file or directory" error as you can also see below.

NOTE:

Please note that in my case I am using Ubuntu 20.04 LTS Server. You might be using some different Linux distribution. It might not be same for you.
login as: testuser
testuser@192.168.0.108's password:
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.15.0-52-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

21 updates can be applied immediately.
13 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2025.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

Could not chdir to home directory /home/testuser: No such file or directory
$

There are two different ways to fix "Could not chdir to home directory /home/testuser: No such file or directory" error depending on which server you are working on and how critical is the user's account. You can choose either of the below method as per your requirements.

Method 1: Create User with home directory

If you are getting the error in some lab or test server where you can easily delete and recreate the user then the quickest way to solve this error is to delete the user by using userdel testuser command as shown below.

cyberithub@ubuntu:~$ sudo userdel testuser

And then recreate the user with sudo useradd testuser -m command as shown below. This will solve your problem in no time. Check more about useradd command.

cyberithub@ubuntu:~$ sudo useradd testuser -m

Now when you login to the server with this user, you won't get "Could not chdir to home directory /home/xxxx" error again. This can be confirmed by using pwd command immediately after login as shown below.

login as: testuser
testuser@192.168.0.108's password:
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.15.0-52-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

9 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2025.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

$ pwd
/home/testuser

 

Method 2: Create home directory and link with user

Second scenario could be that you cannot delete the created user now but you still have to get rid of this error. In those cases, it is always advised to create user home directory manually and then link that directory to user account. So Let's create the user's home directory first by using mkdir /home/testuser command.

cyberithub@ubuntu:~$ sudo mkdir /home/testuser

Then provide a login shell using --shell switch and link home directory using --home switch.

cyberithub@ubuntu:~$ sudo usermod --shell /bin/bash --home /home/testuser testuser

Change the ownership of directory to user testuser and group testuser using sudo chown -R testuser:testuser /home/testuser command as shown below.

cyberithub@ubuntu:~$ sudo chown -R testuser:testuser /home/testuser

Finally transfer all the user's file from source location to user's home directory using below cp command.

cp -rvf <source_location> /home/testuser/*

To test the user's home directory, you can login with the user credentials as shown below.

login as: testuser
testuser@192.168.0.108's password:
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.15.0-52-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

9 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable

New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2025.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

testuser@ubuntu:~$ pwd
/home/testuser
testuser@ubuntu:~$

Hope above solution would be enough to solve your "Could not chdir to home directory /home/xxxx: No such file or directory". Please let me know your feedback in the comment box !!

3 thoughts on “Solved "Could not chdir to home directory /home/xxxx" in Linux”

  1. If you are using sssd, make sure that with-mkhomedir is set:
    authselect current
    authselect select --force
    authselect enable-feature with-mkhomedir
    authselect apply-changes

    Reply

Leave a Comment