|

Linux CLI 20 ๐Ÿง chgrp, chown and passwd commands

These three commands manage ownership and passwords โ€” who owns files, what group they belong to, and how user passwords are set up.


Overview

CommandPurposeBest For
chgrpChange group ownershipAssigning files to teams/groups
chownChange owner (and group)Transferring file ownership
passwdManage user passwordsSetting, locking, expiring passwords

The chgrp Command

CHange GRouP โ€” changes the group ownership of files and directories.

sudo groupadd testers
sudo chgrp testers 1.txt
sudo chgrp testers data2.txt data3.txt
sudo chgrp -R testers d2
sudo chgrp --reference=a 1.txt
CommandDescription
sudo groupadd testersCreate a new group called testers
sudo chgrp testers 1.txtChange 1.txt‘s group to testers
sudo chgrp testers data2.txt data3.txtChange group for multiple files
sudo chgrp -R testers d2Change group recursively for d2
sudo chgrp --reference=a 1.txtUse a‘s group for 1.txt

Why sudo? Only the owner can change a file’s group โ€” and only to a group they belong to. sudo (root) can change any file’s group.


Creating a Group

$ sudo groupadd testers
$ getent group testers
testers:x:1001:
# GID 1001, no members yet

Alternative (some distros):

$ sudo addgroup testers

Add users to the group:

$ sudo usermod -aG testers alice
$ sudo usermod -aG testers bob

$ getent group testers
testers:x:1001:alice,bob

Changing Group on a Single File

$ ls -l 1.txt
-rw-r--r--  1 kronos users  1024 Jan 15 10:30 1.txt
#              โ”‚      โ”‚
#              โ”‚      โ””โ”€โ”€ group is "users"
#              โ””โ”€โ”€ owner is "kronos"

$ sudo chgrp testers 1.txt
$ ls -l 1.txt
-rw-r--r--  1 kronos testers  1024 Jan 15 10:30 1.txt
#                     โ”‚
#                     โ””โ”€โ”€ group is now "testers"

Multiple Files at Once

$ sudo chgrp testers data2.txt data3.txt
$ ls -l data*.txt
-rw-r--r--  1 kronos testers  1024 Jan 15 10:30 data2.txt
-rw-r--r--  1 kronos testers  2048 Jan 15 10:30 data3.txt

Recursive (-R)

$ ls -lR d2
d2:
-rw-r--r--  1 kronos users  1024 d2/file1.txt
-rw-r--r--  1 kronos users  2048 d2/file2.txt

$ sudo chgrp -R testers d2
$ ls -lR d2
d2:
-rw-r--r--  1 kronos testers  1024 d2/file1.txt
-rw-r--r--  1 kronos testers  2048 d2/file2.txt

Use case: Share a whole project directory with a team.


Using a Reference File (--reference)

Copy the group from another file:

$ ls -l a 1.txt
-rw-r--r--  1 kronos testers   500 a
-rw-r--r--  1 kronos users    1024 1.txt

$ sudo chgrp --reference=a 1.txt
$ ls -l a 1.txt
-rw-r--r--  1 kronos testers   500 a
-rw-r--r--  1 kronos testers  1024 1.txt
#                     โ”‚
#                     โ””โ”€โ”€ same group as "a"

Use case: Match permissions to an existing file quickly.


Common chgrp Options

OptionDescription
-RRecursive
-vVerbose
-cReport only when changes are made
-fSuppress error messages
--reference=fileUse file‘s group

The chown Command

CHange OWNer โ€” changes the owner (and optionally group) of files and directories.

sudo chown root 1.txt
sudo chown -c root a
sudo chown -R root d3
sudo chown root:root log.txt
CommandDescription
sudo chown root 1.txtChange 1.txt‘s owner to root
sudo chown -c root aChange owner and report the change
sudo chown -R root d3Change owner recursively
sudo chown root:root log.txtChange both owner and group

Changing Owner Only

$ ls -l 1.txt
-rw-r--r--  1 kronos users  1024 Jan 15 10:30 1.txt

$ sudo chown root 1.txt
$ ls -l 1.txt
-rw-r--r--  1 root users  1024 Jan 15 10:30 1.txt
#              โ”‚
#              โ””โ”€โ”€ owner is now "root"

Changing Owner with Feedback (-c)

$ sudo chown -c root a
changed ownership of 'a' from kronos to root
$ ls -l a
-rw-r--r--  1 root users  500 Jan 15 10:30 a

Use case: Verify what actually changed.


Recursive Change (-R)

$ ls -lR d3
d3:
-rw-r--r--  1 kronos users  file1.txt
-rw-r--r--  1 kronos users  file2.txt

$ sudo chown -R root d3
$ ls -lR d3
d3:
-rw-r--r--  1 root users  file1.txt
-rw-r--r--  1 root users  file2.txt

Changing Both Owner and Group

Use the user:group syntax:

$ sudo chown root:root log.txt
$ ls -l log.txt
-rw-r--r--  1 root root  1024 Jan 15 10:30 log.txt
#              โ”‚    โ”‚
#              โ”‚    โ””โ”€โ”€ group is now "root"
#              โ””โ”€โ”€ owner is now "root"

Alternatives:

sudo chown root: log.txt         # Owner root, group = root's default
sudo chown :root log.txt         # Only group changes
sudo chown root:developers log.txt

Common chown Options

OptionDescription
-RRecursive
-vVerbose
-cReport only changes
-fSuppress errors
-hChange symlinks instead of targets
--reference=fileUse file‘s owner and group

chown vs chgrp

Aspectchownchgrp
ChangesOwner (and optionally group)Group only
Syntaxchown user filechgrp group file
Both at oncechown user:group fileOnly group
Requires sudo?UsuallyUsually

When to use which:

  • chgrp โ€” when you only need to change the group
  • chown โ€” when you need to change the owner (or both)

The passwd Command

Manages user passwords โ€” setting, locking, expiring, and viewing.

cat /etc/passwd
passwd -S kronos
CommandDescription
passwdChange your own password
sudo passwd userChange another user’s password
passwd -S userShow password status
passwd -l userLock the password
passwd -u userUnlock the password
passwd -d userDelete the password (no password needed)
passwd -e userExpire the password (force change)
passwd -n 5 userMinimum days between changes

Viewing Users โ€” /etc/passwd

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
kronos:x:1000:1000:Kronos:/home/kronos:/bin/bash
alice:x:1001:1001:Alice:/home/alice:/bin/bash

Format:

username:x:UID:GID:comment:home:shell
FieldDescription
usernameLogin name
xPassword placeholder (real hash is in /etc/shadow)
UIDUser ID
GIDPrimary group ID
commentFull name / description
homeHome directory
shellLogin shell

Password Status (-S)

$ passwd -S kronos
kronos P 01/15/2024 0 99999 7 -1
       โ”‚ โ”‚          โ”‚  โ”‚     โ”‚  โ”‚
       โ”‚ โ”‚          โ”‚  โ”‚     โ”‚  โ””โ”€โ”€ Inactive (-1 = never)
       โ”‚ โ”‚          โ”‚  โ”‚     โ””โ”€โ”€โ”€โ”€โ”€ Warning days
       โ”‚ โ”‚          โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Max days
       โ”‚ โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Min days
       โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Last change date
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Status (P=password, L=locked, NP=no password)

Status codes:

CodeMeaning
PPassword is set
LPassword is locked
NPNo password set

Lock a Password (-l)

Prevents login by prepending ! to the password hash.

$ sudo passwd -l alice
passwd: password expiry information changed.

$ passwd -S alice
alice L 01/15/2024 0 99999 7 -1
#    โ”‚
#    โ””โ”€โ”€ L = locked!

$ su - alice
Password:
su: Authentication failure
# โœ… Cannot log in

Use case: Temporarily disable an account without deleting it.


Unlock a Password (-u)

Reverses the lock.

$ sudo passwd -u alice
passwd: password expiry information changed.

$ passwd -S alice
alice P 01/15/2024 0 99999 7 -1
#    โ”‚
#    โ””โ”€โ”€ P = password restored

Delete a Password (-d)

Removes the password entirely โ€” the user can log in without a password.

$ sudo passwd -d alice
passwd: password expiry information changed.

$ passwd -S alice
alice NP 01/15/2024 0 99999 7 -1
#    โ”‚
#    โ””โ”€โ”€ NP = no password!

$ su - alice
# No password prompt!

โš ๏ธ Warning: Extremely insecure โ€” only use for testing or special accounts.


Expire a Password (-e)

Forces the user to change their password at next login.

$ sudo passwd -e alice
passwd: password expiry information changed.

# Next time alice logs in:
$ su - alice
Password:
You are required to change your password immediately.
Current password:
New password:
Retype new password:
passwd: password updated successfully

Use case: Enforce password rotation after a security incident.


Password Aging (-n, -x, -w)

Control when passwords expire.

OptionDescription
-n DAYSMinimum days between changes
-x DAYSMaximum days before change required
-w DAYSWarning days before expiry
-i DAYSDays after expiry before account is disabled

Example โ€” require change every 90 days:

$ sudo passwd -x 90 alice
$ sudo passwd -n 5 alice   # Can't change for 5 days after
$ sudo passwd -w 7 alice   # Warn 7 days before expiry
$ sudo passwd -i 14 alice  # Disable 14 days after expiry

Verify:

$ passwd -S alice
alice P 01/15/2024 5 90 7 14
#                  โ”‚ โ”‚  โ”‚ โ”‚
#                  โ”‚ โ”‚  โ”‚ โ””โ”€โ”€ Inactive: 14 days after expiry
#                  โ”‚ โ”‚  โ””โ”€โ”€โ”€โ”€ Warning: 7 days before
#                  โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Max: 90 days
#                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Min: 5 days

Complete Example Session

# ============================================
# PART 1: CHGRP
# ============================================

# Create a group
$ sudo groupadd testers

# Add users to group
$ sudo usermod -aG testers alice
$ sudo usermod -aG testers bob

# Change group on a file
$ sudo chgrp testers 1.txt
$ ls -l 1.txt
-rw-r--r--  1 kronos testers  1024 Jan 15 10:30 1.txt

# Change group on multiple files
$ sudo chgrp testers data2.txt data3.txt

# Recursive change
$ sudo chgrp -R testers d2

# Use reference file
$ sudo chgrp --reference=a 1.txt
$ ls -l a 1.txt
-rw-r--r--  1 kronos testers  500  a
-rw-r--r--  1 kronos testers  1024 1.txt

# ============================================
# PART 2: CHOWN
# ============================================

# Change owner
$ sudo chown root 1.txt
$ ls -l 1.txt
-rw-r--r--  1 root testers  1024 Jan 15 10:30 1.txt

# Change owner with feedback
$ sudo chown -c root a
changed ownership of 'a' from kronos to root

# Recursive change
$ sudo chown -R root d3

# Change both owner and group
$ sudo chown root:root log.txt
$ ls -l log.txt
-rw-r--r--  1 root root  1024 Jan 15 10:30 log.txt

# ============================================
# PART 3: PASSWD
# ============================================

# Change your own password
$ passwd
Changing password for kronos.
Current password:
New password:
Retype new password:
passwd: password updated successfully

# Change another user's password
$ sudo passwd alice
New password:
Retype new password:
passwd: password updated successfully

# Check password status
$ passwd -S kronos
kronos P 01/15/2024 0 99999 7 -1

# Lock a user's account
$ sudo passwd -l alice
$ passwd -S alice
alice L 01/15/2024 0 99999 7 -1

# Try to log in โ€” fails
$ su - alice
Password:
su: Authentication failure

# Unlock
$ sudo passwd -u alice
$ passwd -S alice
alice P 01/15/2024 0 99999 7 -1

# Force password change
$ sudo passwd -e alice
# Next login forces password change

# ============================================
# PART 4: PRACTICAL SCENARIOS
# ============================================

# Scenario 1: Setup a team project
$ sudo groupadd dev-team
$ sudo usermod -aG dev-team alice
$ sudo usermod -aG dev-team bob
$ sudo mkdir /projects/app
$ sudo chgrp dev-team /projects/app
$ sudo chmod 775 /projects/app
# Now alice and bob can both work in the project

# Scenario 2: Give root ownership of system file
$ sudo chown root:root /etc/myservice.conf
$ sudo chmod 644 /etc/myservice.conf
# Standard for system files

# Scenario 3: Lock a user's account temporarily
$ sudo passwd -l alice
# ...work...
$ sudo passwd -u alice

# Scenario 4: Force password rotation
$ sudo passwd -e bob
# Bob must change password on next login

# Scenario 5: Check who has accounts
$ cut -d: -f1,3 /etc/passwd
root:0
daemon:1
bin:2
...
kronos:1000
alice:1001

# ============================================
# PART 5: TROUBLESHOOTING
# ============================================

# User can't change to a directory
$ ls -ld shared/
drwxr-x---  2 alice dev-team  4096 shared/
# Bob can't access โ€” is he in dev-team?
$ groups bob
bob : bob users
# No! Add him:
$ sudo usermod -aG dev-team bob
# Bob must log out and back in
$ groups bob
bob : bob users dev-team
# โœ… Now he can access

# ============================================
# PART 6: SUMMARY OF COMMANDS
# ============================================

$ cat /etc/passwd       # List all users
$ passwd -S kronos      # Check password status
$ sudo passwd -l user   # Lock account
$ sudo passwd -u user   # Unlock account
$ sudo passwd -d user   # Delete password
$ sudo passwd -e user   # Expire password
$ sudo passwd -n 5 user # Min days between changes
$ sudo chgrp group file # Change group
$ sudo chown user file  # Change owner
$ sudo chown user:group file  # Change both

Quick Reference

chgrp Options

OptionDescription
-RRecursive
-vVerbose
-cReport changes only
-fSuppress errors
--reference=fileUse file’s group

chown Options

OptionDescription
-RRecursive
-vVerbose
-cReport changes only
-fSuppress errors
-hAffect symlinks, not targets
--reference=fileUse file’s owner/group

passwd Options

OptionDescription
-S userShow password status
-l userLock password
-u userUnlock password
-d userDelete password
-e userExpire (force change)
-n DAYSMin days between changes
-x DAYSMax days before change
-w DAYSWarning days before expiry
-i DAYSDays before account disabled

/etc/passwd Format

username:x:UID:GID:comment:home:shell

Best Practices

โœ… Do This:

# Create groups for teams
sudo groupadd developers

# Add users to group
sudo usermod -aG developers alice

# Change group on shared files
sudo chgrp developers /shared/project

# Use recursive for directories
sudo chgrp -R developers /shared/project
sudo chown -R alice:developers /shared/project

# Check password status
passwd -S alice

# Lock unused accounts
sudo passwd -l olduser

# Force password change on suspicious accounts
sudo passwd -e suspicious_user

# Set password aging for security
sudo passwd -x 90 -n 5 -w 7 alice

โŒ Don’t Do This:

# Don't change owner without sudo
chown root file.txt      # โŒ Permission denied
sudo chown root file.txt # โœ…

# Don't use -d to delete passwords
sudo passwd -d alice     # โŒ Insecure โ€” no password needed
sudo passwd -l alice     # โœ… Lock instead

# Don't forget to check ownership after changes
sudo chown root file
ls -l file               # โœ… Verify

# Don't mix chown and chgrp unnecessarily
sudo chown alice file; sudo chgrp developers file  # โš ๏ธ Two commands
sudo chown alice:developers file                   # โœ… One command

# Don't leave accounts without passwords
# This is a major security risk

Common Pitfalls

PitfallProblemSolution
Forgot sudoPermission deniedAdd sudo
Wrong group syntaxchgrp user instead of groupUse group name
chown user:group missing colonTreated as one argUse user:group
Locked user can’t log inExpected โ€” that’s the pointUse -u to unlock
Password expired unexpectedlySet by adminChange when prompted
Recursive on symlinksChanged targetUse -h for symlinks

Real-World Examples

1. Team Project Setup

sudo groupadd webdev
sudo usermod -aG webdev alice
sudo usermod -aG webdev bob

sudo mkdir /var/www/project
sudo chown -R root:webdev /var/www/project
sudo chmod -R 775 /var/www/project

# Now alice and bob can both work on the project
# Their files will have group "webdev"

2. Web Server Files

# Owned by www-data
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
sudo chmod 644 /var/www/html/*.html

3. User Account Management

# Create user
sudo useradd -m -s /bin/bash alice
sudo passwd alice

# Set password policy
sudo passwd -x 90 -n 5 -w 7 -i 14 alice

# Lock account (employee leaving)
sudo passwd -l alice

# Delete user (permanently)
sudo userdel -r alice

4. Audit User Accounts

# List all users
cut -d: -f1 /etc/passwd

# Users with UID >= 1000 (regular users)
awk -F: '$3 >= 1000 {print $1, $3}' /etc/passwd

# Check locked accounts
sudo passwd -S -a | grep " L "

5. Fix Permissions After Copy

# Files copied from another user
sudo chown -R alice:alice /home/alice/backup

6. Transfer Ownership

# Employee leaves, transfer their files
sudo chown -R newuser:newgroup /home/olduser/projects

Visual: Ownership Model

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              File: report.pdf                โ”‚
โ”‚                                              โ”‚
โ”‚  Owner: alice      Group: developers         โ”‚
โ”‚                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚  alice   โ”‚  โ”‚developersโ”‚  โ”‚  others  โ”‚   โ”‚
โ”‚  โ”‚   rwx    โ”‚  โ”‚   r-x    โ”‚  โ”‚   r--    โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                                              โ”‚
โ”‚  chown alice       โ†’ changes owner           โ”‚
โ”‚  chgrp developers  โ†’ changes group           โ”‚
โ”‚  chown alice:devs  โ†’ changes both            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

CommandPurposeExample
chgrpChange groupsudo chgrp testers file
chownChange ownersudo chown root file
chown user:groupChange bothsudo chown alice:devs file
passwdChange passwordpasswd
passwd -SCheck statuspasswd -S alice
passwd -lLock accountsudo passwd -l alice
passwd -uUnlock accountsudo passwd -u alice
passwd -eExpire passwordsudo passwd -e alice

Key takeaways:

  • chgrp changes group ownership โ€” use for team sharing
  • chown changes owner โ€” and optionally group with user:group
  • chown -R and chgrp -R apply recursively to directories
  • /etc/passwd lists all users โ€” fields are username:UID:GID:comment:home:shell
  • passwd -S shows password status: P (set), L (locked), NP (none)
  • passwd -l and -u lock/unlock accounts
  • passwd -e forces password change at next login
  • Password aging (-n, -x, -w, -i) enforces rotation policies
  • Most of these commands need sudo (root privileges)

Remember: These three commands are about who and how:

  • chgrp โ€” “which team does this belong to?”
  • chown โ€” “who owns this?”
  • passwd โ€” “how does this user log in?”

Use groups for collaboration (chgrp + chmod 775), chown for transfers when people leave, and passwd -l to lock accounts without deleting them. And always verify your changes with ls -l or passwd -S โ€” a quick look can prevent a big headache!


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!