You are on page 1of 22

Linux File Permissions

Permission Types

• Four symbols are used when displaying permissions:


r : permission to read a file or list a directory's contents
w: permission to write to a file or create and remove files
from a directory
x: permission to execute a program or change into a
directory and do a long listing of the directory
-: no permission (in place of the r, w, or x) Slackware
• File permissions
r = read, view
w = write, update
x = execute, run
- = a permission isn't set
• Directory permission
r = list contents
w = create/delete contents
x = access
- = a permission isn't set
Manage permissions in the GUI

• Create file or Directory


• Right click -> properties -> permissions
Examining Permissions
• File permissions may be viewed using ls -l file/directory
• ( ls -ld directory)
• Eg :-
# ls -l /etc/passwd
-rwxr-xr-x 1 root root 19080 Apr 1 18:26 /etc/passwd
permission field,owner,group,size,date&time,file name
• File type and permissions represented by a 10- character
string
• ie., permission are defined for three types of users

Owner Group Others

- --- --- ---


• linux file type owner group others
Interpreting Permissions

• -rwxr-x--- 1 andersen trusted 2948 Oct 11 14:07 myscript


• Read, Write and Execute for the owner, andersen
• Read and Execute for members of the trusted group
• No access for all others
Changing Permissions in CLI

Changing permissions are represented in two ways


1. Symbolic method
2. Numeric method
Changing Permissions
Symbolic Method

• To change access modes:


• # chmod WhoWhatWhich file / directory

Who = u,g,o or a for user, group, other and all


What = + ,- or = for grant, deny and assign.
Which = r, w or x for read, write and execute
• Examples:
• Eg :-
# touch file1
# ls -l file1
-rw-r--r– file 1
# chmod g+w file1
# ls -l file1
-rw-rw-r– file1
# chmod o-r file1
# ls -li file1
-rw-rw---- file1
# chmod o=w redhat
-rw-rw--w- redhat
Changing Permissions
Numeric Method

• Uses a three-digit mode number


first digit specifies owner's permissions
second digit specifies group permissions
third digit represents others' permissions
• Permissions are calculated by adding:
4 (for read)
2 (for write)
1 (for execute)
• Example:
chmod 640 myfile
Permissions Symbolic Numeric

Read r 4
Write w 2
Execute x 1
Full Permission rwx 7

644 Owner : read and write permission


Group : only read permission
Others: only read permission

755 Owner : full permission


Group : read and execute permission
Others : read and execute permission
• Example For a user
• User1 and user2
• Log user1
# touch file1
• Log user2
# cd /home/user1
: permission denied
• Log user1
# chmod 775 /home/user1
• Log user2
# cd /home/user1
# ls
Default Permissions

• Root :
directory :- 755
file :- 644
• User :
directory :- 775
file :- 664
Umask
• Root : 022
• User : 002
• Eg :
777 – (default directory permission)
022
755
666 – (default file permission)
022
644
File -- # vi /etc/bashrc (Check Umask value)
#umask (checking umask value)
#umask <value>
Changing File Ownership

• Only root can change a file's owner


• Only root or the owner can change a file's group
• Ownership is changed with chown:
chown <user_name> <file|directory>
• Group-Ownership is changed with chgrp:
chgrp <group_name> <file|directory>
• Eg :-
# touch redhat
# ls -l redhat
# useradd user1
# chown user1 redhat
# groupadd group1
# chgrp group1 redhat
# ls -l redhat
VI EDITOR
VI EDITOR
• This is command mode editor for files. Other editors in
linux are emacs, gedit, nano but vi editor is most popular.

• It has 3 modes:
1. Insert Mode
2. Command Mode
3. Extended Mode
Insert Mode:
 i--inserts the text at current cursor position
 I - inserts the text at beginning of line
 a - appends the text after current cursor position
 A - appends the text at end of line
 o - inserts a line below current cursor position
 O - inserts a line above current cursor position
 r - replace a single char at current cursor position
Command Mode:
dd - Deletes a line
ndd - Deletes n lines
yy - Copy a line
nyy - Copies n lines
p - After deleting or copying, by pressing p the deleted or
copied contents will be pasted below the position of
cursor.
P - pasted above the position of cursor.
u - Undo (can undo 1000 times)
U – Undo entire Line
Ctrl+r - Redo
G - Moves cursor to last line of file
5G - Moves cursor to 5th line of file
Command Mode:
gg – To go to the beginning of the page
w – To move the cursor forward, word by word
b -- To move the cursor backward, word by word
nw -- To move the cursor forward to n words
nb -- To move the cursor backward to n words
dw – To delete the word letter by letter (like backspace)
x-- To delete the word letter by letter (like delete)
/ -- To search a word in the file
Extended Mode:
:q - quit without saving
:q! - quit forcefully without saving
:w - save
:wq - save & quit
:wq! - save & quit forcefully
:x - save & quit
:X – To give password to the file and remove password
Shift+ZZ - save & quit
:sh - Provides temporary shell
Extended Mode:
:set number - Setting line numbers
:se nu - Setting line numbers
:set nonumber - Removing line numbers
:se nonu - Removing line numbers
:84 - Press enter goes to line 84

To Open Multiple files in vi editor:


#vi –o file1 file 2
Switch between files use ctrl + w

You might also like