Managing Linux Users Groups and File Permissions
Managing Linux Users Groups and File Permissions
# adduser [new_account]
# useradd [new_account]
When a new user account is added to the system, the following operations are
performed.
.bash_logout
.bash_profile
.bashrc
# groups tecmint
# id tecmint
Now let’s execute all the above commands in one go.
Deleting a group
You can delete a group with the following command.
# groupdel [group_name]
If there are files owned by group_name, they will not be deleted, but the group
owner will be set to the GID of the group that was deleted.
Linux File Permissions
Besides the basic read, write, and execute permissions that we discussed
in Archiving Tools and Setting File Attributes – Part 3 of this series, there are
other less used (but not less important) permission settings, sometimes
referred to as “special permissions”.
Like the basic permissions discussed earlier, they are set using an octal file or
through a letter (symbolic notation) that indicates the type of permission.
Group Management
Every time a new user account is added to the system, a group with the same
name is created with the username as its only member. Other users can be
added to the group later. One of the purposes of groups is to implement a
simple access control to files and other system resources by setting the right
permissions on those resources.
OR
# chmod u=rw,g=rw,o= common.txt [notice the space between the last equal sign
and the file name]
However, this will only provide read and write access to the owner of the file
and to those users who are members of the group owner of the file (user1 in
this case). Again, you may be tempted to add user2 and user3to group user1,
but that will also give them access to the rest of the files owned by
user user1 and group user1.
This is where groups come in handy, and here’s what you should do in a case
like this.
Understanding Setuid
When the setuid permission is applied to an executable file, an user running
the program inherits the effective privileges of the program’s owner. Since this
approach can reasonably raise security concerns, the number of files with
setuid permission must be kept to a minimum. You will likely find programs
with this permission set when a system user needs to access a file owned by
root.
Summing up, it isn’t just that the user can execute the binary file, but also that
he can do so with root’s privileges. For example, let’s check the permissions
of /bin/passwd. This binary is used to change the password of an account, and
modifies the /etc/shadow file. The superuser can change anyone’s password,
but all other users should only be able to change their own.
Understanding Setgid
When the setgid bit is set, the effective GID of the real user becomes that of
the group owner. Thus, any user can access a file under the privileges
granted to the group owner of such file. In addition, when the setgid bit is set
on a directory, newly created files inherit the same group as the directory, and
newly created subdirectories will also inherit the setgid bit of the parent
directory. You will most likely use this approach whenever members of a
certain group need access to all the files in a directory, regardless of the file
owner’s primary group.
To set the setgid in octal form, prepend the number 2 to the current (or
desired) basic permissions.
To set the sticky bit in octal form, prepend the number 1 to the current (or
desired) basic permissions.
Without the sticky bit, anyone able to write to the directory can delete or
rename files. For that reason, the sticky bit is commonly found on directories,
such as /tmp, that are world-writable.
# chattr +i file1
# chattr +a file2
After executing those two commands, file1 will be immutable (which means it
cannot be moved, renamed, modified or deleted) whereas file2 will enter
append-only mode (can only be open in append mode for writing).
$ su
$ su -
and then enter root’s password.
# visudo
This opens the /etc/sudoers file using vim (you can follow the instructions given
in Install and Use vim as Editor – Part 2 of this series to edit the file).
These are the most relevant lines.
Defaults secure_path="/usr/sbin:/usr/bin:/sbin"
root ALL=(ALL) ALL
gacanepa ALL=NOPASSWD:/bin/updatedb
Defaults secure_path="/usr/sbin:/usr/bin:/sbin:/usr/local/bin"
This line lets you specify the directories that will be used for sudo, and is used
to prevent using user-specific directories, which can harm the system.
The next lines are used to specify permissions.
The first ALL keyword indicates that this rule applies to all hosts.
The second ALL indicates that the user in the first column can run commands
with the privileges of any user.
The third ALL means any command can be run.
If no user is specified after the = sign, sudo assumes the root user. In this
case, user tecmint will be able to run yum update as root.
gacanepa ALL=NOPASSWD:/bin/updatedb
The NOPASSWD directive allows user gacanepa to run /bin/updatedb without
needing to enter his password.
The % sign indicates that this line applies to a group called “admin”. The
meaning of the rest of the line is identical to that of an regular user. This
means that members of the group “admin” can run all commands as any user
on all hosts.
To see what privileges are granted to you by sudo, use the “-l” option to list
them.
# ldd $(which top) | grep libpam # top does not use PAM
# cat /etc/passwd
account : this module type checks if the user or service has supplied valid
credentials to authenticate.
auth : this module type verifies that the user is who he / she claims to be and
grants any needed privileges.
password : this module type allows the user or service to update their password.
session : this module type indicates what should be done before and/or after the
authentication succeeds.
The second column (called control ) indicates what should happen if the
authentication with this module fails:
requisite : if the authentication via this module fails, overall authentication will
be denied immediately.
required is similar to requisite, although all other listed modules for this service
will be called before denying authentication.
sufficient : if the authentication via this module fails, PAM will still grant
authentication even if a previous marked as required failed.
optional : if the authentication via this module fails or succeeds, nothing
happens unless this is the only module of its type defined for this service.
include means that the lines of the given type should be read from another file.
substack is similar to includes but authentication failures or successes do not
cause the exit of the complete module, but only of the substack.
The fourth column, if it exists, shows the arguments to be passed to the
module.
The first three lines in /etc/pam.d/passwd (shown above), load the system-
auth module to check that the user has supplied valid credentials (account). If
so, it allows him / her to change the authentication token (password) by giving
permission to use passwd (auth).
For example, if you append
remember=2
to the following line
in /etc/pam.d/system-auth:
Summary
Effective user and file management skills are essential tools for any system
administrator. In this article we have covered the basics and hope you can
use it as a good starting to point to build upon. Feel free to leave your
comments or questions below, and we’ll respond quickly.