15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
02.01.2026

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.
  • 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)

su - username
  • – (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:

su -

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)

sudo -i

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:

sudo -iu username

Run a single command as another user (non-interactive)

sudo -u

sudo -u username whoami
sudo -u postgres psql

Run with a clean login-like environment:

sudo -iu username

Run a command as root

sudo systemctl restart nginx

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:

ps -eo user,pid,cmd | grep nginx

Or:

ps -p <PID> -o user,group,cmd

Change which user a service runs as (systemd)

Most production Linux uses systemd. Services should run as dedicated, unprivileged users.

Check service configuration:

systemctl cat myservice.service

Look for:

  • User=

  • Group=

Example override (safe method):

sudo systemctl edit myservice.service

Add:

[Service]
User=myuser
Group=mygroup

Apply:

sudo systemctl daemon-reload
sudo systemctl restart myservice.service

Verify:

systemctl status myservice.service
ps -eo user,pid,cmd | grep myservice

Change file ownership (changing “user” for files)

chown (owner change)

Change owner:

sudo chown username file.txt

Change owner and group:

sudo chown username:groupname file.txt

Recursive (be careful):

sudo chown -R username:groupname /var/www/site

Preserve symlinks (avoid changing link targets):

sudo chown -h username:group symlink

Advanced tip: For big trees, preview first:

find /path -maxdepth 2 -printf '%u:%g %p\n' | head

Change your current shell identity vs. changing the account itself

Confirm who you are

whoami
id

Confirm who is logged in

who
w

See which user ran the current shell via sudo

echo $USER
echo $SUDO_USER

Change username, UID, groups (account modification)

Rename a user (username)

sudo usermod -l newname oldname

Also move/rename home directory:

sudo usermod -d /home/newname -m newname

Update group name too (optional):

sudo groupmod -n newname oldname

Change UID (advanced; affects file ownership

sudo usermod -u 2001 username

After changing UID, fix file ownership:

sudo find / -user oldUID -exec chown -h username {} \;

Add user to a group (e.g., sudo)

sudo usermod -aG sudo username # Debian/Ubuntu
sudo usermod -aG wheel username # RHEL/Alma/Rocky

Verify:

id username

Quick “cheat sheet”

Switch user:

su - user
sudo -iu user

Run command as another user:

sudo -u user command

Run root shell:

sudo -i

Change file owner:

sudo chown -R user:group /path

Change account details:

sudo usermod -l new old
sudo usermod -d /home/new -m new
sudo usermod -aG
sudo user
15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started