Linux Command Line Basics: Part 4

Today we’ll start from where we left in the third part of this series. For those of you who are new to this site, you can read the first three parts of the Linux command line basics here- Part 1, Part 2, Part3

To understand today’s post it is essential that you go through Part 2 and Part 3 of this series. We’ll now learn how to add users to groups via command line and how to modify file/folder permissions.

Let’s create two groups in the system- ‘team1′ and ‘team2′

Issue the following commands at the terminal:

sudo addgroup team1

sudo addgroup team2

1. useraddadds a new user to an existing group.

Syntax 1 – sudo useradd -g <group-name> <user-name>

The above command will add a new user to an existing group. The option ‘-g’ will make the specified group new user’s primary group. In order to add new users ‘user1′ and ‘user2′ with primary group ‘team1′, issue the following commands at the terminal:

sudo useradd -g team1 user1

sudo useradd -g team2 user2

Syntax 2 – sudo useradd -G <group-name> <user-name>

The above command will add a new user to an existing group. The option ‘-G’ will make the specified group new user’s secondary group. In order to add new users ‘user3′ and ‘user4′ with secondary group ‘team2′, issue the following commands at the terminal:

sudo useradd -G team2 user3

sudo useradd -G team2 user4

2. usermodadds an existing user to an existing group.

Syntax 1 – sudo usermod -g <group-name> <user-name>

The above command will add an existing user to an existing group. The option ‘-g’ will make the specified group existing user’s primary group. In order to add existing users ‘user3′ and ‘user4′ with primary group ‘team1′, issue the following commands at the terminal:

sudo usermod -g team1 user3

sudo usermod -g team1 user4

Syntax 2 – sudo usermod -a -G <group-name> <user-name>

The above command will add an existing user to an existing group. The options ‘-a’ and ‘-G’ will make the specified group existing user’s secondary group. In order to add existing users ‘user1′ and ‘user2′ with secondary group ‘team2′, issue the following commands at the terminal:

sudo usermod -a -G team2 user1

sudo usermod -a -G team2 user2

3. chmod – modifies file/folder permissions. (Make sure you have read Part 2)

Syntax – chmod <permission value> <file/folder name>

Issue the following command at the terminal:

chmod 777 firefox-3.6.8.tar.bz2

The above command will allow everybody to read, write and execute the file.

Issue the following command at the terminal:

chmod 755 firefox-3.6.8.tar.bz2

The above command will allow the file owner to read, write and execute the file. Group users can read and execute the file. Others can only execute the file.

Issue the following command at the terminal:

chmod 700 firefox-3.6.8.tar.bz2

The above command will allow file owner to read, write and execute the file. Group users and others have no permissions on the file.

Now, let us look how ‘chmod’ is used to modify folder permissions.

Issue the following command at the terminal(‘test’ is folder in our home directory):

chmod 777 test

The output of ‘ls -la’ command shows that the above command allows owner, group members and others to list files in directory ‘test’, create files in directory ‘test’, delete files from the directory ‘test’ and to change to the directory ‘test’.

Issue the following command at the terminal:

chmod 755 test

The output of ‘ls -la’ command shows that the above command allows owner to list files in directory ‘test’, create files in directory ‘test’, delete files from the directory ‘test’ and to change to the directory ‘test’. Group members and others can change to the directory ‘test’ and list the files only.

Issue the following command at the terminal:

chmod 700 test

The output of ‘ls -la’ command shows that the above command allows owner to list files in directory ‘test’, create files in directory ‘test’, delete files from the directory ‘test’ and to change to the directory ‘test’. Group members and others do not have any permission on the directory ‘test’.

4. chownchanges file ownership.

Syntax – sudo chown <user-name(of new owner)> <file-name>

Issue the following command at the terminal:

sudo chown user1 firefox-3.6.8.tar.bz2

The output of command ‘ls -la firefox-3.6.8.tar.bz2‘ shows that ‘user1′ is the new owner of the file.

5. chgrpchanges file’s group ownership

Syntax – sudo chgrp <group-name(of new owner group)> <file-name>

Issue the following command at the terminal:

sudo chgrp team1 firefox-3.6.8.tar.bz2

The output of command ‘ls -la firefox-3.6.8.tar.bz2′ shows that file’s group ownership has changed to ‘team1′.

Hope you’ve found today’s post useful. We’ll be back in next part with more commands to learn.

[ Check out rest of the series : Part V and Part VI ]