Learning Linux- Setting up Users, Groups, and Permissions
Beginner’s Guide to Learning Linux/Ubuntu-Operating System
Computers can have multiple users. You and your brother may be sharing your first computer, but have different user accounts set up. Similarly, in school, you and your colleagues may have individual accounts set up on the same desktop. Users and groups have different permissions to access files and programs on a computer to ensure the privacy and security of individuals on that device. In the following article, we’ll discuss users, groups and their permissions in Linux based operating system-Ubuntu, and explore their respective commands for Linux Terminal.
Users and Groups
Listing Users and Groups on a Linux Computer
The file that stores information of users on a Linux machine is /etc/passwd. To view user information, use the following command:
cat /etc/passwd
Most of the rows represent processes running on your computer and not actual users. However the first row represents the root user: root:x:0:0:root:/root:/bin/bash.
The first field is the username, followed by the password and user Id. Note that the password isn’t stored in this file. It’s encrypted and stored in some other file.
To view groups on your computer run the following command:
cat /etc/group
Similar to the user’s file, the first field in the group file represents the group name, followed by the password, and lastly the group ID. Note that the Group Id and User Id for root are always set to 0.
To use the computer as a root user (superuser), you can either use the flag sudo (stands for superuser do) at beginning of every command or simply type in the following command once:
sudo su -
Change User Password
To change the user password type in the following command:
passwd <username>
Type in the current password, followed by a new password to change it as shown in the image below.
Adding and Removing users
To add a new user use the following command:
sudo useradd <username>
To force the user to add/change password on their first login type in the following command:
sudo passwd -e <user name>
Finally, to delete a user account the command used is:
sudo userdel <user name>
Permissions
Viewing Permissions of a File
To view permission of files within a directory, the command used is:
ls -l <path of directory>
In the example shown above, the first field represents a set of permission, followed by the user and group. Let's analyze the set of permission i.e.
- rw- rw- r--
which are grouped as follows:
Group: 1 2 3 4
(-) (rw-) (rw-) (r--)
- Group 1 contains a single bit which represents if it’s a file or directory. A file is represented by a dash ‘-’ while a directory is represented by the letter ‘d’.
- Group 2 contains 3 bits, that define the permission group of user (u).
- Group 3 contains 3 bits, that define the permission group of group(g).
- Group 4 contains 3 bits, that define the permission group of others(o).
Each bit of the last three groups represents permission type for read(r), write(w), and execute(x) respectively. If permission is not given to a user, group, or other, its bit is replaced by a dash ‘-’.
For example, in the above scenario, the first ‘-’ bit represents that myflowers.txt is a file. The next three bits ‘rw-’ show that the user mariam is given permission to read and write, but not execute. Similarly, the next three bits ‘rw-’ tell us that the group mariam is given permission to read and write, but not execute. Lastly, the last 3 bits ‘r--’ represent that all other users and groups can only read the file.
Changing Permissions of a File
The permissions of a file or directory can be changed. To increase permission ‘+’ is used, and to decrease permission ‘-’ is used. The command used can be defined as follows:
chmod <permission group> <+/-> <permission type> <path of file>
where chmod stands for change mode.
For example, to give the user permission to execute a file, we’ll use the command
chmod u+x <path of file>
Similarly, to decrease a user’s permission to execute the following command can be used:
chmod u-x <path of file>
Note that the permission group and permission type can be changed and concatenated depending on the user’s need as you can see in the following image:
Conclusion
Above we discussed a number of beginner commands for Ubuntu-Linux terminal to set up users and change permissions. You’ll often use the command line, especially if you are a programmer or an IT support personnel. If you are new to Linux do check out the following links to learn more about Basic Commands of Linux and how to excel in Files & Text manipulation. Happy Learning :)