How to Change the Home Directory of a New or Existing User on Ubuntu 24.04LTS

I do this so I will land in the docroot of the virtual host when using SSH, FileZilla, and/or Visual Studio Code. This configuration aids me when I want to edit PHP remotrely using Visual Studio Code.

The command sudo usermod -d is used to change the home directory of a user on a Unix-like operating system. In this case I use this command on Ubuntu 24.04LTS.

Here’s a Breakdown of the Command

sudo: This command runs the following command with superuser (root) privileges, which are often necessary for modifying user accounts.

usermod: This is the command used to modify user account settings.

-d <directory-path>: This option specifies the new home directory for the user.

username: This is the placeholder for the actual username of the account whose home directory you want to change.

Apache configures a default virtual host as part of the initial installation. This virtual host is incomplete as a virtual host. I completed the build of the default Apache virtual host.

As part of the build out I added a user named default and changed it’s home directory to /var/www/html by the following command:

– sudo usermod -d /var/www/html default

Key Points

Changing Home Directory: This command changes where the user’s files and settings are stored. By default, a user’s home directory is often located in /home/username.

Permissions: Ensure that the new directory (<directory-path>) has the appropriate permissions for the user to access and manage files there.

Impact: Changing the home directory can affect scripts, applications, and settings that rely on the old home directory path.

Make sure to back up any important data before making such changes.