Linux(Chapter 5)
Linux(Chapter 5)
SYSTEM
ADMINISTRATION IN
LINUX
1. When you bring the system up in single-user mode, you are Superuser.
2. Once the system is up and running in multiuser mode, you can log in as
root. When you supply the proper password, you will be Superuser. In
Linux system we should remember that ‘root’ is the superuser. We can log
in to the root by giving following information:
3. You can give an su (substitute user) command while you are logged in as
yourself and, with the proper password, you will have Superuser
privileges.
$su
Password: ********
o Use the id utility to display the changes in your user and group ID
and in the groups you are associated with:
$id
$su
Password: ********
#id
o Consider that you are logged in as root and your working directory will
be root and you want to switch to user ‘student1’. Then you have to use
the following command:
#su – student1
o Now you are logged in as ‘student1’ and your current working directory
will be home (~) folder of ‘student1’.
o Note: Since you are switching from ‘root’ to ‘student1’ the shell prompt
will not ask for password to log in since root will be having full
permission to access other users.
o However if you want to switch from subuser to other subuser or root
(eg student1 to student2) shell prompt will be asking for password to
log in.
o Consider that you are logged in as root and your working directory will
be root and you want to switch to user ‘student1’. Then you have to use
the following command
#su student1
4. You can use sudo selectively to give users Superuser privileges for a limited
amount of time on a per-user and per-command basis.
o The sudo utility is controlled by the /etc/sudoers file, which must be set
up by root.
o sudo (Super User DO) command in Linux is generally used as a prefix
of some command that only superuser are allowed to run.
o If you prefix “sudo” with any command, it will run that command with
elevated privileges or in other words allow a user with proper
permissions to execute a command as another user, such as the
superuser.
o This is the equivalent of “run as administrator” option in Windows.
o The option of sudo lets us have multiple administrators.
o These users who can use the sudo command need to have an entry in
the sudoers file located at “/etc/sudoers”. Remember that to edit or
view the sudoers file you have to use sudo command.
o By default, sudo requires that users authenticate themselves with a
password which is the user’s password, not the root password itself.
sudoers file:
The easiest way to manage users and groups is through the graphical
application, User Manager.
The following command line tools can also be used to manage users and
groups:
o useradd, usermod, and userdel – Industry-standard methods of
adding, deleting and modifying user accounts.
o groupadd, groupmod, and groupdel – Industry-standard methods
of adding, deleting, and modifying user groups.
Linux/Unix is multi user and multi-tasking OS. There are two types of
users in Linux operating system- system users and normal users.
o User always get created with primary group
o One Primary Group per User
Each user is identified by a unique integer, the user id or uid, because
numbers are faster and easier for a computer to process than textual
names.
A separate database outside the kernel assigns a textual name, the
username, to each user id.
The database contains additional information as well. To create a user,
you need to add information about the user to the user database and
create a home directory for him. It may also be necessary to educate the
user and set up a suitable initial environment for him.
useradd COMMAND
useradd is a command in Linux that is used to add user accounts to
your system.
It is just a symbolic link to adduser command in Linux and the
difference between both of them is that useradd is a native binary
compiled with system whereas adduser is a Perl script which uses
useradd binary in the background.
It make changes to the following files:
o /etc/passwd
o /etc/shadow
o /etc/group
o creates a directory for new user in /home
Syntax:
useradd [options] name_of_the_user
This will create a new user with the user-id “1234” and the name “test_user”.
This will create a new user with the group id “1000” and the name
“test_user”.
This will create the user with the name “test_user” and that too without a
home directory.
This will create the user named “test_user” with the expiry date of 30th May
2020.
This will create a user with a short comment or description of the user.
This will create a user named “test_user” with the default shell /bin/sh.
User name
Encrypted password
User ID number (UID)
User's group ID number (GID)
Full name of the user (GECOS)
User home directory
Login shell
This will create a new user with the name “test_user” and an unencrypted
password “test_password”.
As with the /etc/passwd, each field in the shadow file is also separated
with “:” colon characters as follows:
1. Username : A valid account name, which exist on the system.
2. Password : Your encrypted password is in hash format. The
password should be minimum 15-20 characters long including
special characters, digits, lower case alphabetic and more.
3. Last password change (lastchanged) : The date of the last
password change, expressed as the number of days since Jan 1,
1970 (Unix time). The value 0 has a special meaning, which is that
the user should change her password the next time she will log in
the system. An empty field means that password aging features
are disabled.
4. Minimum : The minimum number of days required between
password changes i.e. the number of days left before the user is
allowed to change her password again. An empty field and value 0
mean that there are no minimum password age.
This command will display the help section of the useradd command.
userdel COMMAND
The userdel command in Linux system is used to delete a user account
and related files.
This command basically modifies the system account files, deleting all
the entries which refer to the username LOGIN. It is a low-level utility
for removing the users.
Syntax:
userdel [options] LOGIN
To remove a user, you first remove all his files, mailboxes, mail aliases,
print jobs, cron and at jobs, and all other references to the user. Then
you remove the relevant vlines from /etc/passwd and /etc/group
(remember to remove the username from all groups it’s been added
to). It may be a good idea to first disable the account (see below),
before you start removing stuff, to prevent the user from using the
account while it is being removed. Remember that users may have files
outside their home directory. The find command can find them:
However, note that the above command will take a long time, if you
have large disks. If you mount network disks, you need to be careful so
that you won’t trash the network or the server.
remove the user’s home directory and mail spool, even if another user is
using the same home directory or even if the mail spool is not owned by
the specified user.
Mail spool shows emails that are waiting to be delivered or have
returned an error. You can apply filters and take action on these emails.
Mail logs. Mail logs show the list of all emails. You can filter these by the
action taken and scanning outcome.
Example:
sudo userdel -f neuser
userdel -r: Whenever we are deleting a user using this option then the
files in the user’s home directory will be removed along with the home
directory itself and the user’s mail spool. All the files located in other file
systems will have to be searched for and deleted manually.
Example:
sudo userdel -r newuser2
usermod COMMAND
usermod command or modify user is a command in Linux that is used
to change the properties of a user in Linux through the command line.
After creating a user we have to sometimes change their attributes like
password or login directory etc. so in order to do that we use the
usermod command. The information of a user is stored in the following
files:
o /etc/passwd
o /etc/group
o /etc/shadow
o /etc/login.defs
o /etc/gshadow
This will add a comment about the user or a short description related to the
user.
This command will now change the group of test user from test_user to
manav
This will now change the login name of the user “test_user”.
6. To lock a user
sudo usermod -L test_user
This will lock the “test_user” account and will display a! sign in shadow file
before the username
7. To unlock a user
sudo usermod -U test_user
This will unlock the “test_user” which was locked by the previous command
This will set the password “test_password” in the unencrypted form for the
user “test_user”
It stores group information or defines the user groups i.e. it defines the
groups to which users belong. There is one entry per line, and each line
has the following format (all fields are separated by a colon (:)
Users on Linux and UNIX systems are assigned to one or more groups
for the following reasons:
groupadd COMMAND
groupadd command creates a new group account using the values
specified on the command line and the default values from the system.
The new group will be entered into the system files as needed.
Options
Tag Description
-p, --
The encrypted password, as returned by crypt. The default is
password
to disable the password.
PASSWORD
Example-1:
# groupadd newgroup
output:
Example-2:
output:
Example-3:
To create group in user defined range ( not range from system i.e
/etc/login.defs)
output:
groupdel COMMAND
groupdel command is used to delete a existing group. It will delete all
entry that refers to the group, modifies the system account files, and it
is handled by superuser or root user.
Syntax:
groupdel [options] GROUP
Files:
/etc/group : It contains the account information of the Group.
/etc/gshadow : It contains the secure group account information.
Exit values: This command exits with the following values.
0: Success
2: Invalid Command Syntax.
6: Specified group doesn’t exist.
8: Can’t remove users primary group.
10: Can’t update group file.
Options:
-f –force: It used to delete a group even if it is the primary group of a user.
-h –help: It displays the help message and exit.
groupmod COMMAND
groupmod command in Linux is used to modify or change the existing group
on Linux system. It can be handled by superuser or root user. Basically, it
modifies a group definition on the system by modifying the right entry in the
database of the group.
Syntax:
groupmod [option] GROUP
chown COMMAND
Different users in the operating system have ownership and
permission to ensure that the files are secure and put restrictions on
who can modify the contents of the files. In Linux there are different
users who use the system:
where the master is another user in the system. Assume that if you are
user named user1 and you want to change ownership to root (where
your current directory is user1). use “su do” before syntax.
sudo chown root file1.txt
You can see that the group permissions changed to group1 from root, if you
use -v option it will report that. We just need to add a “:” to change group.
Here, the output of the above command is, the group of greek1 is changed to
root.
chgrp COMMAND
chgrp command in Linux is used to change the group ownership of a
file or directory.
All files in Linux belong to an owner and a group. You can set the
owner by using “chown” command, and the group by the “chgrp”
command.
Syntax:
chgrp [OPTION]… GROUP FILE…
Here the group name of the file abc.txt was changed from kcVirtual
to geeksforgeeks. Note that when files are created the groupname of the file is
same as the owner under which the file was created.
As we can see the group of the folder GFG and its contents F1, F2 was all
kcvirtual initially and they were changed to geeksforgeeks with the single
command.
The groupname of the reference file abc.txt was used to recursively change
the group of the folder GFG and all its contents using the –reference option.
In Linux there is concept of users and groups which can be given certain
level of permissions that will enable them to share the data.
Below are the steps how to create the shared folders where users can and
update the files individually.
Now we assign the new group to become owner of the shared folder and give
them appropriate permissions which can also be inherited by the
subdirectories.
passwd COMMAND
Any user can change their password at any time. It is good practice to
frequently change your password, and to not reuse or rotate passwords.
1. Open a terminal.
2. Type in the passwd command to start the password change process.
Passwd may look like a spelling mistake, but it is the command to work with
passwords on the terminal.
$ passwd
3. Type in your current password and press Enter.
If you are an administrator of many users, there will come a time where
a user forgets their password and it will fall to you, or someone with
sudo privileges to reset their password.
1. Open a terminal.
2. Type in the passwd command along with the user name. To use this
command you will either need to be root, or be part of the “sudo” group. In the
code example we assume that you are in the sudo group.
3. Change the user’s password, and confirm the change.
Open a terminal and run the command with the -S switch. This will
show the status of the account.
The username
Password status Locked (L), No Password (NP), Password (P)
Date of last password change
Minimum password age
Maximum password age
Warning period (the number of days given to the user to change their
password before it expires)
Inactivity period (number of days after a password expires before it is
locked)