Introduction to Linux standard file permissions
Hi and welcome . In this tutorial we will introduce linux standard file permissions. We will discuss how to determine whether linux content in directory is file, or subdirectory or symbolic link. And we will talk about chgrp and chown commands to change group and change owner of any linux file.
file ownership
user owner and group owner
we can manager users and groups in linux locally in two configuration files /etc/passwd and /etc/group or if on network they can be managed in NIS, LDAP or samba domain. These groups and users can own files/directories/ In Linux every file or directory has owner and group assigned to it.See below example
[root@shahid security]# ls -lh total 0 -rw-rw-r--. 1 rob shahid 0 Feb 20 18:54 file -rw-rw-r--. 1 shahid shahid 0 Feb 20 18:53 file1 -rw-rw-r--. 1 tim tim 0 Feb 20 18:53 file2 -rw-r--r--. 1 michael michael 0 Feb 20 18:57 file3 -rw-r--r--. 1 root root 0 Feb 20 19:00 testfile [root@shahid security]#
user rob owns file but file has group permission for shahid. file1 has owner shahid and also has group permission of shahid. file2 has owner tim and group tim
file3 owner and group is michael. testfile has root user and root group permission
Displaying user accounts
You can use the following command to display all local user accounts.
[root@shahid security]# cut -d: -f1 /etc/passwd | column root abrt gdm bin tss gnome-initial-setup daemon unbound avahi adm colord postfix lp usbmuxd sshd sync libstoragemgmt tcpdump shutdown saslauth shahid halt rpc john mail rtkit michael operator radvd alex games rpcuser khalid ftp nfsnobody david nobody ntp rob avahi-autoipd qemu tim dbus chrony polkitd pulse [root@shahid security]#
chgrp
To change the group owner of a file use the chgrp command.
[root@shahid security]# ls -lh total 0 -rw-rw-r--. 1 rob shahid 0 Feb 20 18:54 file -rw-rw-r--. 1 shahid shahid 0 Feb 20 18:53 file1 -rw-rw-r--. 1 tim tim 0 Feb 20 18:53 file2 -rw-r--r--. 1 michael michael 0 Feb 20 18:57 file3 -rw-r--r--. 1 root root 0 Feb 20 19:00 testfile [root@shahid security]# chgrp rob file [root@shahid security]# ls -lh total 0 -rw-rw-r--. 1 rob rob 0 Feb 20 18:54 file -rw-rw-r--. 1 shahid shahid 0 Feb 20 18:53 file1 -rw-rw-r--. 1 tim tim 0 Feb 20 18:53 file2 -rw-r--r--. 1 michael michael 0 Feb 20 18:57 file3 -rw-r--r--. 1 root root 0 Feb 20 19:00 testfile [root@shahid security]#
root@rhel65:/home/paul/owners# ls -l file2
-rw-r–r–. 1 root tennis 185 Apr 8 18:46 file2
root@rhel65:/home/paul/owners# chgrp snooker file2
root@rhel65:/home/paul/owners# ls -l file2
-rw-r–r–. 1 root snooker 185 Apr 8 18:46 file2
root@rhel65:/home/paul/owners#
chown
To change owner of a file you can use chown command.
[root@shahid security]# ls -l file1 -rw-rw-r--. 1 shahid shahid 0 Feb 20 18:53 file1 [root@shahid security]# ls -l file1 -rw-rw-r--. 1 rob shahid 0 Feb 20 18:53 file1
To change both user and group for a file use below method
[root@shahid security]# chown rob:rob file1 [root@shahid security]# ls -l file1 -rw-rw-r--. 1 rob rob 0 Feb 20 18:53 file1 [root@shahid security]# chown rob:shahid file1
list of special files
When you execute command ls -l, for each file you see ten characters before the user and group owner.
The first character represents the type of file.hyphen (-) represents normal regular file,d means file is direectory, symbolic
links are represented with letter l, pipes get a p, character devices gets c, block devices represented by b, and sockets are shown as
an s.
Unix special files
first character file type
– normal file
d directory
l symbolic link
p named pipe
b block device
c character device
s socket
Below comamnd will display a character device (the console)
[root@shahid security]# ls -ld /dev/console crw-------. 1 root root 5, 1 Feb 19 11:44 /dev/console [root@shahid security]#
Below comamnd will display a block device (hard disk)
[root@shahid security]# ls -ld /dev/sda brw-rw----. 1 root disk 8, 0 Feb 19 11:44 /dev/sda [root@shahid security]# [root@shahid security]#
I created a sampledirectory and below command will display its directory and will see first letter as d
mkdir sampledirectory [root@shahid security]# ls -ld sampledirectory/ drwxr-xr-x. 2 root root 6 Feb 20 20:37 sampledirectory/ [root@shahid security]#
In below example you will see normal files have – as first character (file1, file2,file3, file)
[root@shahid security]# ls -lh total 0 -rw-rw-r--. 1 rob rob 0 Feb 20 18:54 file -rw-rw-r--. 1 rob shahid 0 Feb 20 18:53 file1 -rw-rw-r--. 1 tim tim 0 Feb 20 18:53 file2 -rw-r--r--. 1 michael michael 0 Feb 20 18:57 file3 drwxr-xr-x. 2 root root 6 Feb 20 20:37 sampledirectory
below command show symbolica link my_test_link represented by letter l
[root@shahid security]# ls -lh total 0 -rw-rw-r--. 1 rob rob 0 Feb 20 18:54 file -rw-rw-r--. 1 rob shahid 0 Feb 20 18:53 file1 -rw-rw-r--. 1 tim tim 0 Feb 20 18:53 file2 -rw-r--r--. 1 michael michael 0 Feb 20 18:57 file3 lrwxrwxrwx. 1 root root 15 Feb 20 20:43 my_test_link -> sampledirectory drwxr-xr-x. 2 root root 6 Feb 20 20:37 sampledirectory
This is end of this tutorial in next tutorial we will discuss about next 9 characters , their meanings and how they can be represented by bits.
In this tutorial we have covered basics of chgrp and chown commands and its usage. I hope you have enjoyed tutorial.
And feel free to subscribe to our newsletter