0
0
Linux CLIscripting~5 mins

chown (change ownership) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes files or folders belong to the wrong user or group, which can stop you or others from opening or editing them. The chown command changes who owns a file or folder so the right people can access it.
When you create files as root but want a regular user to own them for easier access.
When you copy files from another system and need to fix ownership to match your users.
When a script creates files owned by one user but another user needs to use them.
When setting up shared folders where a specific group should own the files.
When fixing permissions after restoring backups that changed ownership.
Commands
Create a new empty file named example.txt to demonstrate ownership change.
Terminal
touch example.txt
Expected OutputExpected
No output (command runs silently)
Show the current owner and group of example.txt before changing ownership.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 youruser youruser 0 DATE TIME example.txt
-l - List files with detailed information including ownership.
Change the owner and group of example.txt to root. This requires sudo because root owns the file now.
Terminal
sudo chown root:root example.txt
Expected OutputExpected
No output (command runs silently)
Verify that the owner and group of example.txt have changed to root.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 root root 0 DATE TIME example.txt
-l - Show detailed file info to confirm ownership change.
Change the owner and group back to your regular user to regain access.
Terminal
sudo chown youruser:youruser example.txt
Expected OutputExpected
No output (command runs silently)
Confirm the ownership is back to your user and group.
Terminal
ls -l example.txt
Expected OutputExpected
-rw-r--r-- 1 youruser youruser 0 DATE TIME example.txt
-l - Check ownership details.
Key Concept

If you remember nothing else from this pattern, remember: chown changes who owns a file or folder, controlling who can access or modify it.

Common Mistakes
Trying to change ownership without sudo when not the file owner.
Only root or the file owner can change ownership; without sudo, the command fails.
Use sudo before chown to run it with root privileges when changing ownership to another user.
Using chown without specifying both user and group when needed.
If you only specify user without group, the group stays unchanged, which might cause access issues.
Specify both user and group separated by a colon, like user:group, to set both correctly.
Changing ownership on files recursively without the -R flag.
Without -R, only the specified file changes, not files inside folders, leading to inconsistent ownership.
Use the -R flag to change ownership recursively on directories and their contents.
Summary
Use chown to change the owner and group of files or folders to control access.
You often need sudo to change ownership to another user or root.
Verify ownership changes with ls -l to ensure the right user and group own the file.