file-ownership

Introduction

Linux files and directories always belong to a user and group. That is what we call ownership. If this can be boring to new users, remember that this is a huge part of what makes Linux as secure as it is.

Check current ownership

Check ownership in the current directory:

ls -l
Example output:
2
total 32
3
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:26 lgsm
4
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:23 log
5
drwxr-xr-x 4 ultimatebyte ultimatebyte 4096 Jan 29 22:26 serverfiles
6
drwxr-xr-x 8 ultimatebyte ultimatebyte 4096 Jan 29 22:26 Steam
7
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:24 steamcmd
8
-rwxr-x--- 1 ultimatebyte ultimatebyte 5691 Jan 30 01:14 twserver
Copied!
Syntax of the output is:
  • d for directory or - for file
  • user, then group, then others permissions
  • number specifies the number of links or directories inside of a directory
  • The user that owns the file, or directory
  • The group that file or directory belongs to
  • The size in bytes
  • The date of last modification
  • The name of the file

Check ownership from all files and hidden files of any directory

ls -al /path/to/file/or/dir
Sample output:
1
[email protected]:~$ ls -al /home/ultimatebyte
2
total 132
3
drwxr-xr-x 11 ultimatebyte ultimatebyte 4096 Feb 2 07:36 . # Ownership from the current given dir
4
drwxr-xr-x 22 root root 4096 Feb 2 07:36 .. # Ownership from the parent dir
5
-rw------- 1 ultimatebyte ultimatebyte 49744 Feb 12 03:04 .bash_history
6
-rw-r--r-- 1 ultimatebyte ultimatebyte 220 Apr 15 2016 .bash_logout
7
-rw-r--r-- 1 ultimatebyte ultimatebyte 3515 Apr 15 2016 .bashrc
8
drwxr-xr-x 4 ultimatebyte ultimatebyte 4096 Jan 15 23:26 .config
9
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:26 lgsm
10
drwxr-xr-x 3 ultimatebyte ultimatebyte 4096 Jan 14 18:10 .local
11
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:23 log
12
-rw------- 1 ultimatebyte ultimatebyte 1005 Jan 30 01:14 .nano_history
13
-rw-r--r-- 1 ultimatebyte ultimatebyte 675 Apr 15 2016 .profile
14
-rw-r--r-- 1 ultimatebyte ultimatebyte 66 Sep 1 17:43 .selected_editor
15
drwxr-xr-x 4 ultimatebyte ultimatebyte 4096 Jan 29 22:26 serverfiles
16
drwxr-xr-x 3 ultimatebyte ultimatebyte 4096 Jan 15 01:40 .steam
17
drwxr-xr-x 8 ultimatebyte ultimatebyte 4096 Jan 29 22:26 Steam
18
drwxr-xr-x 5 ultimatebyte ultimatebyte 4096 Jan 29 22:24 steamcmd
19
-rwxr-x--- 1 ultimatebyte ultimatebyte 5691 Jan 30 01:14 twserver
20
-rw-r--r-- 1 ultimatebyte ultimatebyte 29 Jan 30 02:17 .tw-server.lock
Copied!

All these commands require elevated privileges!

Change owner

Change owner from files or directories

Command

chown
As usual, chown --help will provide you with all available arguments

Single file syntax

chown newowner /path/to/file/or/directory

Recursive syntax (include sub files and directories)

chown -R newowner /path/to/directory

Example

chown -R myuser /home/myuser

Group ownership

Change group ownership on files or directories

Command

chgrp
As usual, chgrp --help will provide you with all available arguments

Single file syntax

chgrp newgroup /path/to/dir/or/file

Recursive syntax (include sub files and directories)

chgrp -R newgroup /path/to/dir/

User and group ownership

Change user and/or ownership for files and directories at the same time

Command

chown
As usual, chown --help will provide you with all available arguments

Single file command

chown user:group /path/to/dir/or/file

Recursive

chown -R user:group /path/to/dir

Example:

chown -R myuser:myuser /home/myuser
Last modified 3yr ago