Showing posts with label File Permissions. Show all posts
Showing posts with label File Permissions. Show all posts

Linux Users and Groups | Linux File Permissions

If you are new to Linux/Unix the concept of permissions may be confusing. This guide will provide you with an explanation of what permissions are, how they work, and how to manage them. A number of examples will be provided to illustrate how to set and change permissions for both users and groups.

What are User and Group Permissions?

Linux/Unix operating systems have the ability to multitask in a manner similar to other operating systems. However, Linux's major difference from other operating systems is its ability to have multiple users. Linux was designed to allow more than one user access to the system at the same time. In order for this multiuser design to work properly, there needs to be a method to protect users from each other. This is where permissions come in to play.

Read, Write & Execute Permissions

Permissions are the "rights" to act on a file or directory. The basic rights are read, write, and execute.
  • Read - A readable permission allows the contents of the file to be viewed. A read permission on a directory allows you to list the contents of a directory.
  • Write - A write permission on a file allows you to modify the contents of that file. For a directory, the write permission allows you to edit the contents of a directory (e.g. add/delete files).
  • Execute - For a file the executable permission allows you to run the file and execute a program or script. For a directory, the execute permission allows you to change to a different directory and make it your current working directory. Users usually have a default group, but they may belong to several additional groups.

Viewing File Permissions

To view the permissions on a file or directory, issue the command ls -l <directory/file>. Remember to replace the information in the < > with the actual file or directory name. Below is sample output for the ls command:
-rw-r--r-- 1 root root 1031 Nov 18 09:22 /etc/passwd
The first ten characters show the access permissions. The first dash (-) indicates the type of file (d for directory, s for special file, and - for a regular file). The next three characters (rw-) define the owner's permission to the file. In this example, the file owner has read and write permissions only.

The next three characters (r--) are the permissions for the members of the same group as the file owner (which in this example is read only). The last three characters (r--) show the permissions for all other users and in this example it is read only.

Working with Users, Groups, and Directories

The following sections will go over the commands needed to create, delete, and modify user accounts. Groups will be covered, as well as commands for creating and deleting directories. You will be provided with the commands and descriptions needed for working with users, groups, and directories.

Creating and Deleting User Accounts

To create a new standard user, use the useradd command. The syntax is as follows:
useradd <name>
The useradd command utilizes a variety of variables, some of which are shown in the table below:
Option Description Example
-d <home_dir> home_dir will be used as the value for the user's login directory useradd <name> -d /home/<user's home>
-e <date> the date when the account will expire user add <name>** -e <YYYY-MM-DD>
-f <inactive> the number of days before the account expires useradd <name> -f <0 or -1>
-s <shell> sets the default shell type useradd <name> -s /bin/<shell>
You will need to set a password for the new user by using the passwd command. Note you will need root privileges to change a user password. The syntax is as follows:

passwd <username>
The user will be able to change their password at any time using the passwd command with the syntax. Below is an example:
$ passwd
Changing password for lmartin.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
There is another way of creating user accounts that might be easier for first-time administrators. However, you may need to install the package (if it is not already installed). The installation command for Debian/Ubuntu is as follows:
apt-get install adduser
The adduser command automatically creates a home directory and sets the default group, shell, etc. To create a new standard user with the adduser command the syntax is as follows:
adduser <name>
Once you enter the command you will receive a series of prompts; most of this information is optional. However, you should include at least the user's name (for this example the user name is cjones) and of course a password.
root@localhost:~# adduser cjones
  Adding user `cjones' ...
  Adding new group `cjones' (1001) ...
  Adding new user `cjones' (1001) with group `cjones' ...
  Creating home directory `/home/cjones' ...
  Copying files from `/etc/skel' ...
  Enter new UNIX password:
  Retype new UNIX password:
  passwd: password updated successfully
  Changing the user information for cjones
  Enter the new value, or press ENTER for the default
      Full Name []: Chuck Jones
      Room Number []: 213
      Work Phone []: 856-555-1212
      Home Phone []:
      Other []:
  Is the information correct? [Y/n] Y
It is important to note that security should always be taken very seriously. Therefore, it is strongly recommended to use unique passwords for each account. Never share or give your password to other users.
To remove a user account, enter the following command:
userdel <name>
Issuing the command above will only delete the user's account. Their files and home directory will not be deleted.
To remove the user, their home folder, and their files, use this command:
userdel -r <name>

Understanding Sudo

Root is the super user and has the ability to do anything on a system. Therefore, in order to have protection against potential damage sudo is used in place of root. Sudo allows users and groups access to commands they normally would not be able to use. Sudo will allow a user to have administration privileges without logging in as root. The sample of the sudo command is as follows:
sudo apt-get install <package>
Before using sudo, it may need to be installed if it is not part of your distribution. The command for Debian is as follows:
apt-get install sudo
For CentOS the command is as follows:
yum install sudo
In order to provide a user with sudo ability, their name will need to be added to the sudoers file. This file is very important and should not be edited directly with a text editor. If the sudoers file is edited incorrectly it could result in preventing access to the system.
Therefore the visudo command should be used to edit the sudoers file. At a command line log into your system as root and enter the command visudo.
Below is the portion of the sudoers file that shows the users with sudo access.
# User privilege specification
root    ALL=(ALL:ALL) ALL
cjones  ALL=(ALL:ALL) ALL
kbrown  ALL=(ALL:ALL) ALL
lmartin ALL=(ALL:ALL) ALL
After you have given your user account sudo privileges, save the sudoers file and log out as root. Now log in as your user and test the privileges as your user with sudo access. When a new user needs sudo access, you will now be able to edit the sudoers file with your own login using the following command:
sudo visudo

Working with Groups

Linux uses groups as a way to organize users. Groups organize collections of accounts, primarily as a security measure. Control of group membership is administered through the /etc/group file, which shows a list of groups and its members. Every user has a default or primary group. When a user logs in, the group membership is set for their primary group.

This means that when a user launches a program or creates a file, both the file and the running program will be associated with the user's current group membership. A user may access other files in other groups, as long as they are also a member of that group and the access permissions are set. To run programs or create a file in a different group, the user must run the newgrp command to switch their current group. A sample of the newgrp command is as follows:
$ newgrp <marketing>  
If the user entering the above-referenced command is a member of the marketing group in the /etc/group file, then the current group membership will change. It is important to note that any files created will now be associated with the marketing group rather then the user's primary group. Users may also change their group by using the chgrp command. The syntax for the chgrp command is as follows:
$ chgrp <newgroup> 

Creating and Removing Directories

To make a directory use the command:
mkdir <directory name>
To make a directory and set the permissions at the same time, use the following option and syntax:
mkdir -m a=rwx <directory name>
The -m option is short for mode and a=rwx means that all users have read, write, and execute permissions on the directory. To see a complete list of all options for the mkdir command enter man mkdir at a command prompt.
To remove a file use the following:
rm <file>
To remove a directory:
rm -r <directory name>
It is important to note that if you remove a directory all the files inside will be deleted, as well.

Changing Directory and File Permissions

To view file permissions and ownership on files and directories, use the ls -al command. The a option is to show hidden files and the l options is for long listing. The output will be similar to the following:
drwxr-xr-x 2 user user 4096 Jan  9 10:11 documents
-rw-r--r-- 1 user user  675 Jan  7 12:05 .profile
drwxr-xr-x 4 user user 4096 Jan  7 14:55 public
The first column with the ten letters and dashes shows the permissions of the file or directory. The second column (with the single number) indicates the number of files or directories contained in the directory. The next column indicates the owner, followed by the group name, the size, date and time of last access, and finally the name of the file . For example, using the first line from the output above, the details are as follows:
``drwxr-xr-x`` are the permissions
``2`` is the number of files or directories
``user`` is the owner
``user`` is the group
``4096`` is the size
``Jan  9 10:11`` is the date/time of last access
``documents`` is the directory
Note
Since a directory itself is a file, any directory will always show 4096 as it's size. This does not reflect the size of the contents of the directory.

Chmod Command

The command chmod is short for change mode. Chmod is used to change permissions on files and directories. The command chmod may be used with either letters or numbers (a.k.a octal) to set the permissions. The letters used with chmod are in the table below:
Letter Permission
r Read
w Write
x Execute
X Execute (only if file is a directory)
s Set user or group ID on execution
t Save program text on swap device
u Current permissions the file has for owner
g Current permissions the file has for users in the same group
o Current permissions the file has for others not in the group
It is important to remember that the first character of the first column of a file listing denotes whether it is a directory or a file. The other nine characters are the permissions for the file/directory. The first three characters are for the user; the next three are for the group, and the last three are for others. The example drwxrw-r-- is broken down as follows:
d is a directory
rwx the user has read, write, and execute permissions
rw- the group has read and write permissions
r-- all others have read only permissions
Note that the dash (-) denotes permissions are removed. Therefore, with the others group r-- translates to read permission only, the write and execute permissions were removed.
Conversely, the plus sign (+) is equivalent to granting permissions: chmod u+r,g+x <filename>
The example above translates as follows:
u is for user
r is for read
g is for group
x is for execute
In other words, the user was given read permission and the group was given execute permission for the file. Note, when setting multiple permissions for a set, a comma is required between sets.

Chmod Octal Format

To use the octal format, you have to calculate the permissions for each portion of the file or directory. The first ten characters mentioned above will correspond to a four digit numbers in octal. The execute permission is equal to the number one (1), the write permission is equal to the number two (2), and the read permission is equal to the number four (4). Therefore, when you use the octal format, you will need to calculate a number between 0 and 7 for each portion of the permission. A table has been provided below for clarification.
Although octal format may seem difficult to understand, it is easy to use once you get the gist of it. However, setting permissions with r, w, and x may be easier. Below are examples of how to use both letters and octal format to set permissions on a file or directory.
Sample syntax: chmod <octal or letters> <file/directory name>
Letter format: chmod go-rwx Work (Deny rwx permission for the group and others)
The output of ls -al after the chmod command above would looks as follows:
dr-------- 2 user user 4096 Dec 17 14:38 Work
Octal format: chmod 444 Work
The output of ls -al after the chmod command above would look as follows:
dr--r--r-- 2 user user 4096 Dec 17 14:38 Work
An octal table showing the numeric equivalent for permissions is provided below.

Additional File Permissions

In addition to the most common read/write/execute file permissions, there are some additional modes that you might find useful, specifically the +t mode (sticky bit) and the +s mode (setuid bit). These functions describe the behavior of files and executables in multi-user situations.
When set on a file or directory, the sticky bit, or +t mode, means that only the owner (or root) can delete the file, regardless of which users have write access to this file/directory by way of group membership or ownership. This is useful when a file or directory is owned by a group through which a number of users share write access to a given set of files.
To set the sticky bit on a file named /root/sticky.txt, issue the following command:
chmod +t /root/sticky.txt
To remove the sticky bit from a file, use the "chmod -t" command. Note that to change the sticky bit, you need to be either root or the file owner. The root user will be able to delete files regardless of the status of the sticky bit.
The setuid bit, or +s, when set on files allows users with permissions to execute a given file the ability to run that file with the permissions of file owner. For instance, if the file work was owned by the root user and the marketing group, members of the marketing group could run the work program as if they were the root user. This may pose potential security risks in some cases and executables should be properly evaluated before receiving the +s flag. To set the +s bit on a file named /usr/bin/work, issue the following command:
chmod g+s /usr/bin/work
In contrast to the +s mode for the ownership of a file, the effect of the +s mode on a directory is somewhat different. Files created in +s directories receive the ownership of that directory's user and group, rather than the ownership of the user that created the file and their default group. To set the setguid (group id) option on a directory, use the following command:
chmod g+s /var/doc-store/
To set the setuid (user id) for a directory named /var/doc-store, issue the following command:
chmod o+s /var/doc-store/

Changing File Ownership

By default, all files are "owned" by the user who creates them and by that user's default group. To change the ownership of a file, use the chown command in the "chown user:group /path/to/file" format. In the following example, the ownership of the "list.html" file will be changed to the "cjones" user in the "marketing" group:
chown cjones:marketing list.html
To change the ownership of a directory and all the files contained inside, use the recursive option with the -R flag. In the following example change the ownership of /srv/smb/leadership/ to the "cjones" user in the "marketing" group:

chown -R cjones:marketing /srv/smb/leadership/

Leveraging Users and Groups

In many cases user permissions are used to provide your system with greater security without any direct interaction, as packages for many operating systems create specific system user accounts during the installation process.

The best practice is to give each user their own login to your system. This protects each user's files from all other users. Furthermore, using specific accounts for users allows more accurate system logging, particularly when combined with tools like sudo. We recommend avoiding situations where more than one individual knows the password for a user account for maximum security.

In contrast, groups are useful for allowing multiple independent user accounts to collaborate and share files. If you create groups on a machine for common tasks on a per-task basis (e.g. web editors, contributors, content submitters, support) and add relevant users to the relevant groups, these users can all edit and run the same set of files without sharing these files with the world.

Use of the chown command with file permissions of 770 and 740 would help accomplish this goal.

File Manipulation Examples Using Tac, Rev, Paste, and Join Unix Commands

In this article, let us review how to use Unix tac command, rev command, paste command, and join command with practical examples.

1. tac command – Print file in reverse (last line first)

The word tac is reverse of the word cat. The tac command functionality is also reverse of the cat command. cat command prints the file. tac command prints the file in reverse order with the last line first.
$ cat thegeekstuff.txt
1. Linux Sysadmin, Scripting etc.,
2. Databases Oracle, mySQL etc.,
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10. Windows Sysadmin, reboot etc.,
11. Adding 1's and 0's

$ tac thegeekstuff.txt
11. Adding 1's and 0's
10. Windows Sysadmin, reboot etc.,
9. Software Development
8. Website Design
7. Productivity (Too many technologies to explore, not much time available)
6. Cool gadgets and websites
5. Storage
4. Security (Firewall, Network, Online Security etc)
3. Hardware
2. Databases Oracle, mySQL etc.,
1. Linux Sysadmin, Scripting etc.,

2. rev command – Reverse the order of characters in every line

Reverse the order of characters in every line as shown in the example below. It is different from tac command, as rev command reverses each character of the line, whereas tac command reverses each line of the file.
$ rev thegeekstuff.txt
,.cte gnitpircS ,nimdasyS xuniL .1
,.cte LQSym ,elcarO sesabataD .2
erawdraH .3
)cte ytiruceS enilnO ,krowteN ,llaweriF( ytiruceS .4
egarotS .5
setisbew dna stegdag looC .6
)elbaliava emit hcum ton ,erolpxe ot seigolonhcet ynam ooT( ytivitcudorP .7
ngiseD etisbeW .8
tnempoleveD erawtfoS .9
,.cte toober ,nimdasyS swodniW .01
s'0 dna s'1 gniddA .11

3. paste command – Merge file lines

Paste the line1 of file1, file2, .. fileN into the line1 of the output. It will repeat the same for all lines. Each file’s line will be delimited by tab.
Paste output:
$ paste f1 f2 f3
f1-line1<tab>f2-line1<tab>f3-line1
f1-line2<tab>f2-line2<tab>f3-line2
f1-line3<tab>f2-line3<tab>f3-line3
...
In the following example, corresponding lines from three different files are combined and shown appropriately.
$ cat emp-number.txt
100
200
300
400
500

$ cat emp-firstname.txt
Emma
Alex
Madison
Sanjay
Nisha

$ cat emp-lastname.txt
Thomas
Jason
Randy
Gupta
Singh

$ paste emp-number.txt emp-firstname.txt emp-lastname.txt
100     Emma    Thomas
200     Alex    Jason
300     Madison Randy
400     Sanjay  Gupta
500     Nisha   Singh

4. join – Join lines of two files based on a common field

You can join two files based on a common field, that you can specify using field.
Syntax:
$ join -t':' -1 N -2 N file1 file2
  • -t’:’ – : is the field separator
  • -1 N : Nth field in 1st file
  • -2 N : Nth field in 2nd file
  • file1 file2 : files that should be joined
In this example, let us combine employee.txt and bonus.txt files using the common employee number field.
$ cat employee.txt
100     Emma    Thomas
200     Alex    Jason
300     Madison Randy
400     Sanjay  Gupta
500     Nisha   Singh

$ cat bonus.txt
$5,000  100
$5,500  200
$6,000  300
$7,000  400
$9,500  500

$ join  -1 1 -2 2 employee.txt bonus.txt
100 Emma Thomas $5,000
200 Alex Jason $5,500
300 Madison Randy $6,000
400 Sanjay Gupta $7,000
500 Nisha Singh $9,500

Linux File Permissions

linux-file-permissions_featured-image
What we’ll cover in this article is how to identify permissions for files & directories and how to change them, as well as changing ownerships, groups, etc. Depending on what you want to do, you’ll want to make sure you have the appropriate permissions (obviously), so let’s find out how to change them.
Let’s start by making a file we can use.
I issued the “touch” command to make a file creatively named testfile.
Touch will just create an empty file but has all the same attributes as an actual file. You can see this by using “ls –l.”
Commands:
touch test file
mkdir workfolder
Linux File Permissions 1
The permisions are broken into 4 sections.
Linux File Permissions 2

chmod – adds and removes permissions

If you wanted to add or remove permissions to the user, use the command “chmod” with a “+” or “–“, along with the r (read), w (write), x (execute) attribute followed by the name of the directory or file.
chmod +rwx “name of the file”
chmod –rwx “name of the directory”
Linux File Permissions 3
chmod +x testfile – this would allow me to execute
chmod –wx testfile – this would take out write and executable permissions
You’ll notice that this only changes the permissions for the owner of the file, in this case roman.

Changing Permissions for the Group Owners & Others

The command is similar to what we did before, but this time you add a “g” for group or “o” for users.
chmod g+w testfile
chmod g-wx testfile
Linux File Permissions 4
chmod o+w testfile
chmod o-rwx workfolder
Linux File Permissions 5
Lastly you can change it for everyone: “u” for users, “g” for group, & “o” for others; uog or a (for all).
chmod ugo+rwx workfolder – will give read, write, execute to everyone
chmod a=r workfolder – will give only read perission for everyone

chgrp – changing groups of files & directories

Another useful option is to change file permission to the group owning the file. Perhaps you create the files, but people on the db2 team can write/execute as well. We use chgrp for this purpose.
Linux File Permissions 6
You can see above that testfile and the work folder belong to the users group.
Linux File Permissions 7
By issuing the command – chgrp “name of the group” “name of the file” – you can change this.
chgrp sales testfile
chgrp sales workfolder
This give sales control of the file & then I can take away permissions for everyone else.
Note: The group must exit before you try to assign groups to files and directories.

chown – changing ownership

Another helpful command is changing ownerships of files and directories. The command is “chwon” along with “name of new owner” & “name of file.”
Linux File Permissions 8
The files belonged to roman. To give ownership to tom, issue the command:
chown tom testfile
chown tom workfolder
We can also combine the group and ownership command by:
Linux File Permissions 9
chown -R tom:sales /home/roman/tsfiles
The above command gives tom the ownership of the directory tsfiles, and all files and subfolders. The -R stands for recursive which is why all sub folders and files belong to tom as well.
As opposed to: chown tom workfolder
This command will give ownership to tom but all sub files and directories still belong to the original owner. The -R will transfer ownership of all sub directories to the new owner.
As you can see, you have several options when it comes to permissions. You have the capability to dictate who can do what & the flexibility to limit usability among users. It may be easier to just give all permission to everyone but this may end up biting you in the end, so choose wisely.

Permission in numeric mode

The above way of changing permissions will work fine but you may also need to know how to change permissions in numeric mode. chmod is used in much the same way, but instead of r, w, or x you will use numbers instead.
What are the numbers?
0 = No Permission
1 = Execute
2 = Write
4 = Read
You basically add up the numbers depending on the level of permission you want to give.
Linux File Permissions 10
Examples:
chmod 777 workfolder
Will give read, write, and execute permissions for everyone.
Linux File Permissions 11
chmod 700 workfolder
Will give read, write, and execute permission for the user, but nothing to everyone else.
Linux File Permissions 12
chmod 327 workfolder
Will give write and execute (3) permission for the user, w (2) for the group, and read, write, and execute for other users.
Permission numbers
0 = —
1 = –x
2 = -w-
3 = -wx
4 = r—
5 = r-x
6 = rw-
7 = rwx
Either variation of changing permissions will work, just remember how to use the numeric values.

File Permissions - chmod , su , chown , chgrp

The Unix operating system (and likewise, Linux) differs from other computing environments in that it is not only a multitasking system but it is also a multi-user system as well.

What exactly does this mean? It means that more than one user can be operating the computer at the same time. While your computer will only have one keyboard and monitor, it can still be used by more than one user. For example, if your computer is attached to a network, or the Internet, remote users can log in via telnet or ssh (secure shell) and operate the computer. In fact, remote users can execute X applications and have the graphical output displayed on a remote computer. The X Windows system supports this.

The multi-user capability of Unix is not a recent "innovation," but rather a feature that is deeply ingrained into the design of the operating system. If you remember the environment in which Unix was created, this makes perfect sense. Years ago before computers were "personal," they were large, expensive, and centralized.

A typical university computer system consisted of a large mainframe computer located in some building on campus and terminals were located throughout the campus, each connected to the large central computer. The computer would support many users at the same time.

In order to make this practical, a method had to be devised to protect the users from each other. After all, you could not allow the actions of one user to crash the computer, nor could you allow one user to interfere with the files belonging to another user.
This lesson will cover the following commands:
  • chmod - modify file access rights
  • su - temporarily become the superuser
  • chown - change file ownership
  • chgrp - change a file's group ownership

File permissions

Linux uses the same permissions scheme as Unix. Each file and directory on your system is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file (i.e., run the file as a program).
To see the permission settings for a file, we can use the ls command as follows:
[me@linuxbox me]$ ls -l some_file 

-rw-rw-r-- 1 me me 1097374 Sep 26 18:48 some_file
We can determine a lot from examining the results of this command:
  • The file "some_file" is owned by user "me"
  • User "me" has the right to read and write this file
  • The file is owned by the group "me"
  • Members of the group "me" can also read and write this file
  • Everybody else can read this file
Let's try another example. We will look at the bash program which is located in the /bin directory:
[me@linuxbox me]$ ls -l /bin/bash 

-rwxr-xr-x 1 root root 316848 Feb 27 2000 /bin/bash
Here we can see:
  • The file "/bin/bash" is owned by user "root"
  • The superuser has the right to read, write, and execute this file
  • The file is owned by the group "root"
  • Members of the group "root" can also read and execute this file
  • Everybody else can read and execute this file
In the diagram below, we see how the first portion of the listing is interpreted. It consists of a character indicating the file type, followed by three sets of three characters that convey the reading, writing and execution permission for the owner, group, and everybody else.

chmod

The chmod command is used to change the permissions of a file or directory. To use it, you specify the desired permission settings and the file or files that you wish to modify. There are two ways to specify the permissions, but I am only going to teach one way.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here's how it works:
rwx rwx rwx = 111 111 111
rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000

and so on...

rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4

Now, if you represent each of the three sets of permissions (owner, group, and other) as a single digit, you have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set some_file to have read and write permission for the owner, but wanted to keep the file private from others, we would:
[me@linuxbox me]$ chmod 600 some_file
Here is a table of numbers that covers all the common settings. The ones beginning with "7" are used with programs (since they enable execution) and the rest are for other kinds of files.

Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755 (rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700 (rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666 (rw-rw-rw-) All users may read and write the file.
644 (rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600 (rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.

Directory permissions

The chmod command can also be used to control the access permissions for directories. In most ways, the permissions scheme for directories works the same way as they do with files. However, the execution permission is used in a different way. It provides control for access to file listing and other things. Here are some useful settings for directories:

Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
755 (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
700 (rwx------) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.

Becoming the superuser for a short while

It is often useful to become the superuser to perform important system administration tasks, but as you have been warned (and not just by me!), you should not stay logged on as the superuser. In most distributions, there is a program that can give you temporary access to the superuser's privileges. This program is called su (short for substitute user) and can be used in those cases when you need to be the superuser for a small number of tasks. To become the superuser, simply type the su command. You will be prompted for the superuser's password:
[me@linuxbox me]$ su
Password:
[root@linuxbox me]#
After executing the su command, you have a new shell session as the superuser. To exit the superuser session, type exit and you will return to your previous session.
In some distributions, most notably Ubuntu, an alternate method is used. Rather than using su, these systems employ the sudo command instead. With sudo, one or more users are granted superuser privileges on an as needed basis. To execute a command as the superuser, the desired command is simply preceeded with the sudo command. After the command is entered, the user is prompted for the user's password rather than the superuser's:
[me@linuxbox me]$ sudo some_command
Password:
[me@linuxbox me]$

Changing file ownership

You can change the owner of a file by using the chown command. Here's an example: Suppose I wanted to change the owner of some_file from "me" to "you". I could:
[me@linuxbox me]$ su
Password:
[root@linuxbox me]# chown you some_file
[root@linuxbox me]# exit
[me@linuxbox me]$

Notice that in order to change the owner of a file, you must be the superuser. To do this, our example employed the su command, then we executed chown, and finally we typed exit to return to our previous session.
chown works the same way on directories as it does on files.

Changing group ownership

The group ownership of a file or directory may be changed with chgrp. This command is used like this:
[me@linuxbox me]$ chgrp new_group some_file

In the example above, we changed the group ownership of some_file from its previous group to "new_group". You must be the owner of the file or directory to perform a chgrp.

File Permissions - chmod

Linux has inherited from UNIX the concept of ownerships and permissions for files. This is basically because it was conceived as a networked system where different people would be using a variety of programs, files, etc. Obviously, there's a need to keep things organized and secure. We don't want an ordinary user using a program that could potentially trash the whole system. There are security and privacy issues here as well. Let's face it, we don't want Bill to read Bob's love letters to the Janet who works in R & D. (because Janet is Bill's fiance) In the end, it's important to know what belongs to me, to you and to everybody.

As we mentioned at the beginning of this course, the big advantage that Linux has is its multi-user concept- the fact that many different people can use the same computer or that one person can use the same computer to do different jobs. That's where the system of file permissions comes in to help out in what could be a very confusing situation. We're going to explain some basic concepts about who owns the file and who can do what with a file. We won't get into an enormous amount of detail here. We'll save that for the Linux system administration course. We will show you how to understand file permission symbols and how to modify certain files so that they're more secure.

File permission symbols

If you run the command

Code:
ls -l
in your home directory, you will get a list of files that may include something like this

Code:
-rw-r--r--  1  bob  users  1892  Jul 10  18:30 linux_course_notes.txt
This basically says, interpreting this from RIGHT to LEFT that the file, linux_course_notes.txt was created at 6:30 PM on July 10 and is 1892 bytes large. It belongs to the group users (i.e, the people who use this computer). It belongs to bob in particular and it is one (1) file. Then come the file permission symbols.

Let's look at what these symbols mean:

The dashes - separate the permissions into three types

The first part refers to the owner's (bob's) permissions.

The dash - before the rw means that this is a normal file that contains any type of data. A directory, for example, would have a d instead of a dash.

The rw that follows means that bob can read and write to (modify) his own file. That's pretty logical. If you own it, you can do what you want with it.

The second part of the these symbols after the second dash, are the permissions for the group. Linux can establish different types of groups for file access. In a one home computer environment anyone who uses the computer can read this file but cannot write to (modify) it. This is a completely normal situation. You, as a user, may want to take away the rights of others to read your file. We'll cover how to do that later.

After the two dashes (two here because there is no write permissions for the group) come the overall user permissions. Anyone who might have access to the computer from inside or outside (in the case of a network) can read this file. Once again, we can take away the possibility of people reading this file if we so choose.

Let's take a look at some other examples. An interesting place to look at different kinds of file permissions is the /bin directory. Here we have the commands that anybody can use on the Linux system. Let's look at the command for gzip, a file compression utility for Linux.

Code:
-rwxr-xr-x  1 root    root        53468 May  1  1999 gzip
As we see here, there are some differences.

The program name, date, bytes are all standard. Even though this is obviously different information, the idea is the same as before.

The changes are in the owner and group. Root owns the file and it is in the group \&quot;root\&quot;. Root is actually the only member of that group.

The file is an executable (program) so that's why the letter x is among the symbols.

This file can be executed by everybody: the owner (root), the group (root) and all others that have access to the computer

As we mentioned, the file is a program, so there is no need for anybody other than root to "write" to the file, so there is no w permissions for it for anybody but root.

If we look at a file in /sbin which are files that only root can use or execute, the permissions would look like this:

Code:
-rwxr--r--  1 root    root        1065 Jan 14  1999 cron
'cron' is a program on Linux systems that allows programs to be run automatically at certain times and under certain conditions. As we can see here, only root, the owner of the file, is allowed to use this program. There are no xpermissions for the rest of the users.

We hope you enjoyed this little walk-through of file permissions in Linux. Now that we know what we're looking for, we can talk about changing certain permissions.

chmod

chmod is a Linux command that will let you \&quot;set permissions\&quot; (aka, assign who can read/write/execute) on a file.

Code:
chmod permissions file
Code:
chmod permission1_permission2_permission3 file
When using chmod, you need to be aware that there are three types of Linux users that you are setting permissions for. Therefore, when setting permissions, you are assigning them for \&quot;yourself\&quot;, "your group" and "everyone else" in the world. These users are technically know as:

Owner
Group
World

Therefore, when setting permissions on a file, you will want to assign all three levels of permissions, and not just one user.

Think of the chmod command actually having the following syntax...

chmod owner group world FileName

Now that you understand that you are setting permissions for THREE user levels, you just have to wrap your head around what permissions you are able to set!

There are three types of permissions that Linux allows for each file.

read
write
execute

Putting it all together:

So, in laymen terms, if you wanted a file to be readable by everyone, and writable by only you, you would write the chmod command with the following structure.

COMMAND : OWNER : GROUP : WORLD : PATH

chmod read & write read read FileName
Code:
chmod 644 myDoc.txt
Wait! What are those numbers?!?

Computers like numbers, not words. Sorry. You will have to deal with it. Take a look at the following output of `ls -l`

Code:
-rw-r--r-- 1 gcawood iqnection 382 Dec 19 6:49 myDoc.txt
You will need to convert the word read or write or execute into the numeric equivalent (octal) based on the table below.

4 read (r)
2 write (w)
1 execute (x)

Practical Examples

chmod 400 mydoc.txt read by owner
chmod 040 mydoc.txt read by group
chmod 004 mydoc.txt read by anybody (other)
chmod 200 mydoc.txt write by owner
chmod 020 mydoc.txt write by group
chmod 002 mydoc.txt write by anybody
chmod 100 mydoc.txt execute by owner
chmod 010 mydoc.txt execute by group
chmod 001 mydoc.txt execute by anybody

Wait! I don't get it... there aren't enough permissions to do what I want!

Good call. You need to add up the numbers to get other types of permissions...

So, try wrapping your head around this!!

7 = 4+2+1 (read/write/execute)
6 = 4+2 (read/write)
5 = 4+1 (read/execute)
4 = 4 (read)
3 = 2+1 (write/execute)
2 = 2 (write)
1 = 1 (execute)

chmod 666 mydoc.txt read/write by anybody! (the devil loves this one!)
chmod 755 mydoc.txt rwx for owner, rx for group and rx for the world
chmod 777 mydoc.txt read, write, execute for all! (may not be the best plan in the world...)

Good luck! Hope this helps. 

11 Linux diff3 Command Examples (Compare 3 Files Line by Line)

As you already know, Linux diff command compares two files.
However Linux diff3 utility compares three files and is also capable of merging the difference between two files into the third one. In this article, we will understand the usage of diff3 command through some examples.

The basic syntax of diff3 is :

diff3 [OPTION]... MYFILE OLDFILE YOURFILE

1. Basic Diff3 Output

For this example, lets use these three files: parent.txt, your.txt and mine.txt.
$ cat parent.txt
Hello
This is parent file.

$ cat your.txt
Hello,
This is your file.

$ cat mine.txt
Hello
This is my file.

Now if diff3 command is run over these three files :

$ diff3 parent.txt your.txt mine.txt
====
1:1,2c
  Hello
  This is parent file.
2:1,2c
  Hello,
  This is your file.
3:1,2c
  Hello,
  This is my file.
We see that output contains comparison information of all these three files. In the above output:
  • The very first line ‘====’ signifies that all the three files differ.
  • Next, the line ’1:1,2c’ means that for the first (1:) file given as argument to diff3 (parent.txt in our case), lines in the range of one to two (1,2) are different when compared with the other two files and need to be changed(c) and these two lines are also shown in the next two lines of the output.
  • Similarly ’2:1,2c’ and ’3:1,2c’ can be comprehended.
Please note that apart from using diff3 utility, it is also helpful to understand how to use diff, colordiff, wdiff, or vimdiff for your typical two file comparision.

2. If two out of three files are similar

For this example, assume that the files mine.txt and parent.txt exactly same.

$ cat parent.txt
Hello
This is a file.

$ cat your.txt
Hello,
This is your file.

$ cat mine.txt
Hello
This is a file.
 
And diff3 is run over these three files now :

$ diff3 mine.txt parent.txt your.txt
====3
1:1,2c
2:1,2c
  Hello
  This is a file.
3:1,2c
  Hello,
  This is your file.
The very first line ‘====3′ signifies that this time the file number 3 ie your.txt is different from the other two. This is visible in the output too.

3. ‘c’ is for change while ‘a’ is for append

Till now, we have seen an alphabet ‘c’ in the output. This letter signifies ‘change’ required in the lines/text. If the alphabet ‘a’ appears then it signifies a line needs to be appended.
Lets take another example :

$ cat mine.txt
Hi,

$ cat parent.txt
Hi,

$ cat your.txt
Hi,
This is your file.
 
Now execute diff3 on these three files:

$ diff3 mine.txt parent.txt your.txt
====3
1:1a
2:1a
3:2c
  This is your file.
The output above signifies that the change in file3 (i.e your.txt) needs to be appended in file1 (mine.txt) and file2 (parent.txt) to make all the three file similar. Alternatively, the second line in your.txt can be changed(c) to make all the three files similar.

4. Output overlapping changes using -x option

The flag -x can be used to output the changes that cause overlaps. The order of file names given as argument to diff3 matters here. For example :
diff3 -x mine.txt parent.txt your.txt
means that you want to merge in mine.txt the changes that would turn parent.txt into your.txt
Lets take an example :
$ cat mine.txt
Hi,
This is your file.
hello

$ cat parent.txt
Hi,
This is your file.

$ cat your.txt
Hi,
This is your file.
bye
Execute diff3 now:
$ diff3 -x mine.txt parent.txt your.txt
3c
bye
.
So we see tha
t the output says that the third line of mine.txt has to changed to ‘bye’ in order to merge the changes that would turn parent.txt to your.txt

5. Output non merged changes using -e option

The option -e can be used to output non merged changes from parent.txt to your.txt into mine.txt.
Considering the same files as used the example above :
$ diff3 -e mine.txt parent.txt your.txt
3c
bye
.
.
So we see that the output shows the non merged changes.

6. Output merged file using -m option

A merged file comprising of contents from all the three files can be produced using the -m option. For example, if following are the three files:
$ cat mine.txt
Hi,
This is your file.
Hello

$ cat parent.txt
Hi,
This is your file.

$ cat your.txt
Hi,
This is your file.
Now if diff3 is run using the -m option :
$ diff3 -m mine.txt parent.txt your.txt
Hi,
This is your file.
Hello
So the above output shows contents of merged file that has merged contents from all the three files supplied as arguments to diff3.

7. Understanding conflicts using -m option

Producing a merged file (from the three files) is not always a cake walk (as shown in the above example).
Consider the input file contents below :
$ cat parent.txt
Hi,
This is your file.
hi

$ cat your.txt
Hi,
This is your file.
hello

$ cat mine.txt
Hi,
This is your file.
Hello
If diff3 is run over the files above, the following output is displayed :
$ diff3 -m mine.txt parent.txt your.txt
Hi,
This is your file.
<<<<<<< mine.txt
Hello
||||||| parent.txt
hi
=======
hello
>>>>>>> your.txt
Here the first two lines are undisputed part of the merged file but the actual conflict is shown between <<<<<<< and >>>>>>> along with file names and contents.

8. Accept one argument from stdin using ‘-’

A ‘-’ can be used in place of any one of the file names to accept input from stdin for that argument.
$ diff3 mine.txt parent.txt -
Hi,
bye====3
1:1a
2:1a
3:2c
  bye
As we can see, when the command above was run, the content of third file was given through stdin. Note that after entering the contents of file from stdin, one needs to press the key combination ctrl+d to get the output of diff3.

9. diff3 uses diff tool to compare files

Though diff3 boasts of comparing 3 files but actually it uses diff tool internally to carry out the comparisons between two files. This can be confirmed by passing an executable as an input argument:
$ diff3 mine.txt parent.txt a.out
diff3: subsidiary program `diff' failed
So we can see in the output above, the diff3 command error outs saying that the subsidiary program ‘diff’ failed.

10. Specify a diff program through –diff-program option

diff3 command provides the flexibility to provide a diff program as an argument so that the same can be used to compare files and provide the results which diff3 can use. This functionality is achieved by using the –diff-program option.
$ diff3 --diff-program=diff mine.txt parent.txt your.txt
====
1:2a
2:3c
  c ya
3:3c
  TC

11. Treat all files as text using -a option

As you can see in the example 9 shown above, when we passed a non text file (an executable a.out) then diff3 returned error. But if we want to tell diff3 to compare even non text files, then an option -a can be used that tells diff3 to consider all input files as text files. For example :
$ diff3 -a mine.txt parent.txt a.out
====
1:1,2c
  Hello
  This is file
2:1,3c
  Hello
  This is file
  c ya
3:1,4c
  ELF    > @ @@h @8 @   @@@@@� �  8 8 @8 @    @@� �      `       @@`@`� �  T T @T @DD P�td @ @ @@ @$$ Q�tdR�td   ` `� �  /lib64/ld-linux-x86-64.so.2   GNU     GNUKd�3�K!����
                     ��3�٥�              __gmon_start__libc.so.6puts__libc_start_mainGLIBC_2.2.5      u i  1`   ` ` H��s�  �� H���5�
...
...
...
So the output above shows that diff3 tried to compare even an executable file treating it as a text file.