How to change user in linux
In Linux, the phrase βchange userβ can describe several different actionsβsome temporary and session-based, others permanent and system-wide. Because Linux is a multi-user operating system by design, it provides multiple ways to switch identities and permissions depending on what youβre trying to achieve: administering a server, running an application with limited rights, fixing file access problems, or restructuring user accounts.
Changing βuserβ in Linux can mean a few different things depending on context:
- Switching to another account in the shell (e.g., from john to root)
This is used when you need an interactive terminal session as another userβoften for system administration or to test how something behaves under a different account environment. - Running a single command as another user
Ideal when you only need elevated privileges or a different identity for one task (like restarting a service or running a database command) without fully switching your session.
- Switching to another account in the shell (e.g., from john to root)
- Changing the default login user for a service/process
Services (web servers, databases, apps) should usually run under dedicated, non-root users for security. Changing the service user affects how the process runs and what it can access. - Changing who owns files and directories
File ownership controls access. If permissions are wrongβcommon after migrations, restores, or deploymentsβyou βchange userβ by reassigning ownership so the correct account can read/write files. - Changing user identity attributes (username, UID, groups)
This is account management: renaming a user, changing their UID, or adjusting group membership (like granting sudo access). These changes can impact logins, permissions, and service access.
This guide covers all of these scenarios, showing when to use each approach, how to do it safely, and which mistakes to avoidβso you can change users confidently without breaking permissions, services, or access.
Switch to another user (interactive shell)
su (switch user)
– (or -l) loads the target userβs full login environment: home directory, PATH, shell profile.
Without -, you keep much of your current environment (can be confusing).
Switch to root:
Security note: On many distros, su requires the target userβs password (e.g., root password), which is often disabled.
sudo -i (preferred for root/admin shells)
Gives you a root login shell (similar to su -), using your sudo rights instead of the root password.
Switch to another user with a login shell:
Run a single command as another user (non-interactive)
sudo -u
Run with a clean login-like environment:
Run a command as root
Change the βeffective userβ of a running process (advanced reality)
Linux does not let you βchange the userβ of an already-running process in-place in most practical scenarios. Instead, you typically:
restart the process under the correct user
or use service managers (systemd) to define the user
To inspect which user runs a process:
Or:
Change which user a service runs as (systemd)
Most production Linux uses systemd. Services should run as dedicated, unprivileged users.
Check service configuration:
Look for:
User=Group=
Example override (safe method):
Add:
Apply:
Verify:
Change file ownership (changing βuserβ for files)
chown (owner change)
Change owner:
Change owner and group:
Recursive (be careful):
Preserve symlinks (avoid changing link targets):
Advanced tip: For big trees, preview first:
Change your current shell identity vs. changing the account itself
Confirm who you are
Confirm who is logged in
See which user ran the current shell via sudo
Change username, UID, groups (account modification)
Rename a user (username)
Also move/rename home directory:
Update group name too (optional):
Change UID
After changing UID, fix file ownership:
Add user to a group (e.g., sudo)
Verify:
Quick βcheat sheetβ
Switch user:
Run command as another user:
Run root shell:
Change file owner:
Change account details:
