The phrase linux change user actually covers several different jobs. You might need to switch into another account in a terminal, run one command under a different identity, reassign file ownership after a deployment, or rename an existing account. Each task uses its own command. This guide shows the exact syntax for every scenario so you can pick the right tool without trial and error.
What Linux Change User Actually Means
Linux is a multi-user system, so “changing user” rarely has a single meaning. The table below maps each scenario to the situation where you’d reach for it.
| Scenario | When to use it |
|---|---|
| Switch accounts interactively | Admin work or testing another user’s environment |
| Run one command as another user | Quick task without opening a new session |
| Change which user a service runs as | Security hardening for web servers, databases, daemons |
| Reassign file or directory ownership | Fixing access after migrations or restores |
| Modify an account | Renaming, changing UID, adjusting group membership |
Switch Linux User Interactively With su
The su command opens a new shell as a different user. The hyphen flag loads the target account’s login environment, including its home directory, PATH, and shell profile.
su - username
Skip the hyphen and your current environment carries over, which usually causes odd behavior later. To jump straight to root:
su -
That requires the root password, which most modern distros disable by default. Use sudo instead to get a root shell without needing it:
sudo -i
Need a login shell as a specific named user? Combine both flags:
sudo -iu username
Run a Single Command as Another User
Switching sessions is overkill when you just need one command under a different identity. The -u flag on sudo handles this cleanly.
sudo -u username whoami
sudo -u postgres psql
For administrative work, the usual pattern is running the command as root:
sudo systemctl restart nginx
This is where most admin actions happen in practice. If you need to read or write to someone else’s files during these one-off commands, the Linux credentials reference explains how effective user IDs drive every permission check behind the scenes.
Change Which User a Service Runs Under
Production services should never run as root. Run them as dedicated unprivileged accounts so a compromised service cannot touch the rest of the box. Most distributions use systemd for this.
First, check the current configuration:
systemctl cat myservice.service
Look for the User= and Group= directives. To override them safely without editing the shipped unit file:
sudo systemctl edit myservice.service
Add these lines to the override:
[Service]
User=myuser
Group=mygroup
Reload systemd and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart myservice.service
Confirm the change landed:
ps -eo user,pid,cmd | grep myservice
Fix File Ownership After a Linux Change User Operation
File permissions hinge on ownership. After a restore, migration, or container rebuild, files often end up owned by the wrong account. The chown command fixes that.
Change the owner of one file:
sudo chown username file.txt
Change owner and group together:
sudo chown username:groupname file.txt
Apply recursively to a whole tree:
sudo chown -R username:groupname /var/www/site
Handle symlinks without touching their targets:
sudo chown -h username:group symlink
Before running a recursive chown on anything large, preview what you’re about to touch:
find /path -maxdepth 2 -printf '%u:%g %p\n' | head
Ownership works alongside permission modes. When plain ownership isn’t granular enough, the Linux capabilities reference covers fine-grained rights that let you split root-level abilities across accounts.
Check Your Current Identity
Before touching anything, verify which account is actually active. People get this wrong after several su hops.
whoami
id
To see who kicked off the current sudo session:
echo $SUDO_USER
Rename Accounts and Change UIDs With usermod
When the linux change user task is account-level rather than session-level, usermod handles renaming, UID changes, and group membership.
Rename an account:
sudo usermod -l newname oldname
Move and rename the home directory in one step:
sudo usermod -d /home/newname -m newname
Rename the group to match:
sudo groupmod -n newname oldname
Change a user’s numeric ID:
sudo usermod -u 2001 username
A UID change leaves old files owned by the previous number, so walk the filesystem and reassign:
sudo find / -user oldUID -exec chown -h username {} \;
Give a user sudo rights on Debian or Ubuntu:
sudo usermod -aG sudo username
On RHEL-family systems swap sudo for wheel. For details on how sudo authorization reads those group memberships, see the sudoers configuration guide. Confirm the result:
id username
Removing a stale account after the rename is a separate cleanup step, documented in the deluser command reference.
Quick Reference
| Task | Command |
|---|---|
| Switch user interactively | su - user or sudo -iu user |
| Run one command as another user | sudo -u user command |
| Open a root shell | sudo -i |
| Change file ownership recursively | sudo chown -R user:group /path |
| Rename an account | sudo usermod -l newname oldname |
| Add user to a group | sudo usermod -aG groupname user |
FAQs
How do I switch to another user in Linux without a password?
Use sudo with the -iu flag: sudo -iu username. This uses your sudo rights instead of the target user’s password. You must already have sudo privileges on your own account for this to work.
What is the difference between su and sudo su?
su asks for the target user’s password and switches your shell. sudo su asks for your own password, checks sudoers, then runs su as root. Most modern systems disable the root password, making sudo the standard path.
How do I change a Linux user’s name permanently?
Run sudo usermod -l newname oldname to rename the login. Add -d /home/newname -m to move the home directory. Then rename the matching group with sudo groupmod -n newname oldname.
Why does su give a “permission denied” error?
Three common causes: the target user does not exist, the root password is disabled on your distribution, or the account has /sbin/nologin as its shell. Verify the user with id username and check /etc/passwd for the assigned shell.
How do I change the user a systemd service runs as?
Run sudo systemctl edit myservice, add User= and Group= lines under [Service], then run sudo systemctl daemon-reload followed by sudo systemctl restart myservice. Verify with ps -eo user,pid,cmd.