How to use the chown command on Linux

Files and directories on Linux systems all belong to someone. You can change their ownership with the. change chown Command. We’ll show you how.

Each file belongs to a user and a group

Linux is a multi-user system. The operating system enables multiple user accounts to be defined and any valid user to log on to the computer. In addition, multiple users can use a single computer at the same time.

To document which files belong to which user and to ensure a certain level of security, Linux uses the concept of ownership. Each file belongs to an owner – a user – and a group.

When a file is created, it is owned by the user who created it. The group to which the file belongs – the “owning” group – is the user’s current group. Users and groups have names, and they also have numeric identities called a user (or unique) identifier (UID) and group identifier (GID).

When you create a file, it is yours and belongs to your current group. Usually this is the group you signed up to. By default, this is a group that has the same name as your username and was created when you were created as a user on the system.


You can use the … chown Command to can change the ownership values ​​to something else. You can set a new owner, a new group, or a new owner and a new group at the same time. The owner of a file can change group ownership, but only root can change user ownership because it affects another user. Without root privileges, you cannot get another user of the system to unknowingly “take over” a file.

Why do you want to change hands?

Here are some examples of situations you might want to do this:

  • If you are transferring files between different Linux or Unix-like operating systems, you will need to change the user and group owners to the new user and group owners of the account under which you want to use the files on the new Linux computer.
  • One user can leave your organization and someone else is responsible for all of their files. You need to change the owner and group owner to the employee who is now responsible for these files.
  • You can create a script that will be used by a specific user.
  • You can create a file or directory that is logged in as root but should be accessible to a specific user.

View your groups, UID and GID

to List the groups you’re in, you can use it groups Command.

groups

To get a list of the groups, their numeric IDs, and your UID and GID, use the id Command:

id

You can use some options with ID to refine the output.

  • -u: List your UID.
  • -g: List your effective (current) GID.
  • -nu: List your username.
  • -ng: List your current group name.
id -u
id -g
id -nu
id -ng

View user and group ownership of a file

To view the owners of a file or directory, use the -l (long listing) option with ls.

ls -l



We can see the name dave appears twice in the listing. The leftmost illustration tells us that the file owner is a user named. is dave. The one on the far right dave tells us that the file belongs to a group, also called dave.

By default, when a Linux user is created, they are added to a private group named after their username. You are the only member of this group.

This executable is owned by the user mary and the group to which the file belongs is mary's private group.

ls -l

This file belongs to the user oscar , but the group to which the file belongs is called researchlab . This means that other members of the researchlab Group can access this file according to the file permissions set for the members of this group.

Change user ownership

Let’s work through a few examples. This command changes the user ownership of the while.c file to the user mary.

sudo chown mary while.c

We can use ls to view the changes to the file properties.

ls -l while.c


You can use chown to change ownership of multiple files at the same time.

sudo chown mary getval.c global.c goto.c

This changes user ownership of all three files.

ls -l getval.c global.c goto.c

You can use wildcards to select groups of files. This command changes ownership of all files that begin with the letter “c”.

sudo chown mary c*.*

All files now have mary than their owner. Notice that none of the group ownership has changed.

ls -l mary c*.*

Let’s change ownership of a directory. We just pass the directory name to chown instead of a file name.

sudo chown mary ./archive/

To check the ownership of the directory we are using ls, but also use the -d (Directory) option to do so. This lists the properties of the directory, not the files it contains.

ls -l -d ./archive/

To change ownership of all files in a directory, you can use the -R (recursive) option. This option changes the user ownership of all files within the archive Folder.

sudo chown -R mary ./archive/


Now let’s look at the files in the archive directory.

ls -l ./archive/

As expected, all files now belong to mary.

Change group ownership

There are several ways to change group ownership.

To change group ownership at the same time as changing user ownership, pass the new owner name and the new group name with a colon “:” to separate them. The group must already exist.

sudo chown mary:researchlab charm.c

The user owner and the group to which the file belongs have both changed.

ls -l charm.c

A shorthand way of changing group ownership to the new owner’s current group, just enter the colon and leave out the group name.

sudo chown mary: caps.c

ls -l caps.c

Both user ownership and group ownership were changed to mary.


To change group ownership only, give it a colon and omit the username. The user owner is not changed.

sudo chown :researchlab at.c

ls -l at.c

Group ownership has changed, but user ownership remains the same.

Use chown with UID and GID values

You can change the numeric UID and GID values ​​with the chown Command. This command sets up the user and group ownership mary.

sudo chown 1001:1001 at.c

ls -l at.c

Possession is nine tenths of the law

At least that’s what they say. But on Linux, ownership is an integral part of file security, while file permissions cover the rest of it. Use the chown and chmod Commands to secure access to files on your system.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts