Ownership: chown

chown changes which user and group owns a file.

Syntaxchown [-R] user[:group] file

chown transfers ownership of a file to a different user, and optionally a different group. It usually needs sudo because changing ownership is an administrative action.

Forms

  • chown alice file — change the owner to alice.
  • chown alice:devs file — change owner to alice and group to devs.
  • chown :devs file — change only the group.
  • chown -R alice website/ — apply recursively to a folder.

Example

Example · bash
# Give a file to another user (needs admin rights)
sudo chown alice report.txt

# Set both user and group
sudo chown alice:developers report.txt

# Hand an entire web folder to the web server user
sudo chown -R www-data:www-data /var/www/html

When to use it

  • A sysadmin runs 'chown www-data:www-data /var/www/html' after copying website files so the nginx process can read them.
  • A developer transfers ownership of files from root to their own user with 'chown -R alice:alice ~/projects' after extracting an archive as root.
  • A DevOps engineer uses 'chown -R node:node /app' in a Dockerfile to ensure the app runs as a non-root user.

More examples

Change file owner and group

Shows chown with owner-only and owner:group forms, then confirms the change with ls -l.

Example · bash
# Change owner only
chown alice /tmp/report.csv

# Change owner and group together
chown alice:developers /tmp/report.csv

ls -l /tmp/report.csv
# -rw-r--r-- 1 alice developers 1024 Jul 16 report.csv

Recursive ownership change

Uses -R to change ownership of a directory tree, common after deploying web application files.

Example · bash
# Give the web server user ownership of the web root
chown -R www-data:www-data /var/www/html/

# Verify one level
ls -la /var/www/html/ | head -5

Change owner in a Dockerfile

Demonstrates changing ownership inside a Docker image so the app runs as a non-root 'node' user.

Example · dockerfile
FROM node:20-alpine

WORKDIR /app
COPY --chown=node:node . .

# Or at the shell level
RUN chown -R node:node /app

USER node
CMD ["node", "server.js"]

Discussion

  • Be the first to comment on this lesson.
Ownership: chown — Linux | SoundsCode