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 (advanced; affects file ownership
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:
