Understanding Linux File Permissions and Ownerships

From one of our previous articles "How to manage Linux Users and Groups" we discussed on how Linux becomes a multi-user OS, what is a user and a group with their configurations. By design, even though Linux allows multiple users can use the same computer in the same time without affecting others, Linux doesn't allow you to access or modify files belonging to other users. If Linux allows you to do it, that would be a security risk. But somehow they have implemented a security measure to mitigate that security risk. With that we can make sure only desired users and groups can access the relevant files and directories.

If we take a small demonstration. Here, we will log in as a normal user and try to access a root directory.

It gave a permission denied error when accessing. Why ? That's because /root directory is owned by user root. Only a privileged user can access or modify that.

So, Linux introduces two kind of factors which tells who can access or modify a file as w…

Getting touch with Linux commands (part 1)

Hi, If you want to work in Linux, you must know the commands. But If you are still a beginner, It's OK. No need to be confuse. It's easy. This article will cover basic commands that you need to work with Linux. You have to gain this little by little. So we will start.

When you logged into your account,  you can see a '$' mark. That's called as a prompt. It comes when you are logged in as a normal user. If you are in root account, the prompt will be '#'. It's a sign of identifying where are you.

Once you logged in, you will go to your home directory (directory is like a folder in windows), like below.

[student@localhost ~]$

student - Your account name
localhost - Your hostname
~ - Your current directory ( your present folder )
$ - Your prompt

Hereafter, we will learn simple basic commands that we need in day today life.

1) pwd : print working directory

[student@localhost ~]$ pwd

It shows where are you now. It displays the directory currently you are.

2) ls : list files

In a linux command, we can see a format like below,

command [options] [arguments]

options are used to customize our command to have a different output with more details. arguments are to which elements the command is affecting for. we can use commands with or without options or arguments. Keep it remind. It depends on the command. Let's have a look the below commands with options and arguments.

[student@localhost ~]$ ls

It lists files in your present directory

-l ( ls -l ) : long listing of files. This lists more details about the files and directories under present working directory. This shows the user and file permissions, file modified dates, file sizes with file name.

-a ( ls -a ) : list all the files including hidden files inside of the present working directory. ls command lists only files without hidden.

-d ( ls -ld ) : -d options is used to show the details of the present working directory. ls -l command shows details of the files under present working directory. But -d options shows working directory information. Use ls -ld, not ls -d. Because ls -d doesn't show what you expect. Try it. Then understand what is the output of commands.

-R ( ls -lR) : -R is recursive option. If you want to list files of the directories to the end of the tree from the current directory. ls -lR displays all the files with brief details of all the files in every directory with it's directory name.

-h ( ls -lh ) : -h is used to show the file sizes in human readable format. It will display the sizes in KB or MB or GB.

-r ( ls -lr ) : -r option is used to reverse the output. Simply it shows the reverse of the original list of files inside of the current directory.

-t ( ls -lt ) : -t is used to sort the files in time modified. ls -lrt is used to reverse the output.

-S ( ls -lS ) : used to sort the files in size.

-i ( ls -li ) : -i is used to display the details of the file's inodes. inodes is an entry which stores information about a file. Like file type, permissions, user id, group id, sizes, time stamp.

--help ( ls --help ) : --help is a command that you need to learn in first stages. Because with --help options you can learn about the commands. with help you can learn many more thing than here i have mentioned. So as a beginner, i prefer you to read those help pages. Because i did the same thing.

*** As arguments, you can give whatever location( path ) you need. It's not just only your current location.
eg - ls -ld /tmp , ls -l /home/student/Documents, ls /home

3) cd : change directory

[student@localhost ~]$ cd /home/student/Videos

This command changes my present directory to /home/student/Videos. Simpy it commands to go to that mentioned directory. In windows when you click the button and goes to the required place. But in linux you need to run the command and do it.

cd /home/student : Go to directory /home/student
cd /var/log : Go to directory /var/log
cd - : Go to my previous location where i was ( keep 'cd -' in your remind, it's very useful)
cd /home : Go to directory /home
cd .. : Go to one step back from the current directory ( means go to the backward directory )
cd . : Go to the present directory ( simply doesn't go anywhere )
cd ~ : Go to home directory. simply if i am in student account, cd ~ command will go to /home/student directory
cd ../.. : Go backward twice. Means go two directories back. If i am now in /home/student directory, this command will go to / directory.
cd : only cd command will go to it's home folder itself. its like cd ~ command.

4) vim - Create a text file

[student@localhost ~]$ vim mytextfile.txt

This command will create a text file with name "mytextfile.txt" in the present directory. Vim command opened it's text editor  when you run the command. You have to type what ever you want to write and saved it and quit from the editor.
To save the content you wrote, you have to do something more.

To save : press ESC and ':w' in keyboard [ esc + :w ]
To save and quit : press ESC and ':wq' [ esc + :wq ]
To quite without saving : press ESC and ':q' [ esc + :q ]

If you get an error when quit without saving or quit with saving, do force quit. its, ESC + :q! or
ESC + :wq! ( ! is using for forcing )

5) touch - touch command

[student@localhost ~]$ touch mytextfile2.txt

This command will also will create the text file with the name mytextfile2.txt and it will not open at that time like vim. you can edit the file later.

6) cat - cat command

[student@localhost ~]$ cat mytextfile.txt

cat command is used to view a file.

7) less - less command

[student@localhost ~]$ less mytextfile2.txt

less command is also used to view a file. But it is different from cat. Because if its a 1000 word file. It we use cat command we can see only the end part of the file. But less command gives us the feature for scrolling up and down through the file. Because of that we can see the complete file even if the file is larger too much.

8) more - more command

[student@localhost ~] cat mytextfile.txt | more

This is also doing the same thing in less. You can see "|". That is pipeline. With pipeline we can send the output of the left side to the right side as an input. So cat command will view the file and more command will use to view it like less command.


Popular posts from this blog

Different types of users in linux

Understanding Linux File Permissions and Ownerships

How to manage Linux Users and Groups