Linux Unit III
Linux Unit III
Linux Unit III
SYSTEM ADMINISTRATION
1. User: A user is the one who created the file. By default, whosoever, creates the file
becomes the owner of the file. A user can create, delete, or modify the file.
2. Group: A group can contain multiple users. All the users belonging to a group have
same access permission for a file.
3. Other: Any one who has access to the file other than user and group comes in the
category of other. Other has neither created the file nor is a group member.
Syntax:
1. ls -lh
Listing User Accounts
To know the local users account, following command can be used. It list out all the local
users from the system.
Syntax:
The chgrp command can be abbreviated as change group. You can change the group owner
of the file using chgrp command.
Syntax:
Example:
Look at the above snapshot, earlier 'file' group was sssit. But after passing the command
"chgrp php file" , we have changed the group to php (we have highlighted php just to show
you).
Syntax:
Example:
Command chown can also be used to change both user owner and group.
Syntax:
Example:
Look at the above snapshot, both user owner and group are changed to jtp and php
respectively.
- Normal file
d Directory
l Symbolic link
p Named pipe
b Blocked device
c Character device
s Socket
Look at the above snapshot, first letter c denotes the character device and b denotes the
blocked device.
Look at the above snapshot, first letter (-) denotes the normal file and d denotes the directory.
Linux chgrp command is used to change the group ownership of a file or directory. In the
Linux file system
, each file is related to corresponding owner and group, and has read, write, and execute
permission.
Syntax:
Options:
-c, --changes: It is like 'verbose' option but, it is reported only when a change is made.
--dereference: It is used to affect each symbolic link's significance, rather than the symbolic
link itself.
-h, --no-dereference: It is used to alter the symbolic links instead of any referenced file
--preserve-root: It is used when the default command is failed to operate recursively on '/.'
-R, --recursive: It is used to operate on files and directories recursively. The option specifies
the traversing of the '-R' option.
-P: If this option is specified, it will not traverse any symbolic links (default)
--help: It is used to display the help manual having the brief information of supported
command-line options.
We can change the group of any specific file. To change the group ownership of a file,
execute the command as follows:
is used to list the available groups in the system. And, the 'chgrp' command executed with the
sudo because it needs sudo privilege.
To change the group ownership of a folder, execute the 'chgrp' command with the folder
name as follows:
The above command will change the group ownership of the 'Newdirectory' folder. Consider
the below output:
If the chgrp command is successfully executed, it will not produce any output. To confirm the
changes, execute the "ls -l" command.
We can change the group ownership of a file or folder recursively by applying some set of
rules. To recursively change the group ownership of a folder and its content, execute the
command as follows:
We can change the group of a file by using the group name of a reference file. To change the
group of another file using the reference of any other file or directory, execute the command
as follows:
The above command will change the group of 'ref.txt' by using the reference of 'Nwdirectory.'
Consider the below output:
File Permissions
All the three owners (user owner, group, others) in the Linux system have three types of
permissions defined. Nine characters denotes the three types of permissions.
1. Read (r) : The read permission allows you to open and read the content of a file. But
you can't do any editing or modification in the file.
2. Write (w) : The write permission allows you to edit, remove or rename a file. For
instance, if a file is present in a directory, and write permission is set on the file but
not on the directory, then you can edit the content of the file but can't remove, or
rename it.
3. Execute (x): In Unix type system, you can't run or execute a program unless execute
permission is set.But in Windows, there is no such permission available.
Permission Set
Look at the above snapshot, there are ten characters (-rw-rw-r--) before the user owner. We'll
describe these ten characters here.
When you are the User owner, then the user owner permission applies to you. Other
permissions are not relevant to you.
When you are the Group then the group permission applies to you. Other permissions are not
relevant to you.
When you are the Other, then the other permission applies to you. User and group
permissions are not relevant to you.
Permission Example
Now we'll show some examples how permissions can be seen for a file or directory.
Look at the above snapshot, different directories and files have different permissions.
Now, from remaining nine letters, first triplet represents the permission for user owner.
Second triplet represents the permission for group owner. Third triplet represents the
permission for other
You can change the permissions with chmod command accordingly to your need. Below are
some examples to change the permissions for different groups.
Syntax:
Example:
Syntax:
Example:
Look at the above snapshot, permission to execute is removed from the group and permission
to write is removed from the user owner.
Syntax:
Example:
Syntax:
Example:
1. chmod +w file
Look at the above snapshot, this example is same as the earlier one only difference is that we
haven't typed a in this.
Syntax:
Example:
Syntax:
Example:
Look at the above snapshot, we have set permissions for all the three groups.
For example, to set r octal will be 4, to set w octal will be 2, to set x octal will be 1.
Octal Table:
000 0 ---
001 1 --x
010 2 -w-
011 3 -wx
100 4 r--
101 5 r-x
110 6 rw-
111 7 rwx
1. 777 = rwxrwxrwx
2. 765 = rwxrw-r-x
3. 654 = rw-r-xr--
Look at the above snapshot, we have shown some random octal examples with the numbers
777, 274 and 111.
umask
While creating a file or directory, by default a set of permissions are applied. These default
permissions are viewed by umask command.
For safety reasons all Unix systems doesn't provide execution permission to newly created
files.
mkdir -m
Syntax:
Example:
Look at the above snapshot, we have created two files new1 and new2 with mode 777 and
000 respectively.
cp -p
The 'cp -p' command preserves the permissions and time stamps from source files.
Syntax:
1. cp -p <sourceFile> <destinationFile>
Example:
1. cp -p list dupli.txt
COMMUNICATION COMMANDS
Sending messages to other users on the Linux command line can be very easy, but there are a
number of commands that you might want to consider. In this post, we’ll look at four
commands and see how each of them works.
wall
The wall command (as in "write all") allows you to send a message to all users who are
currently logged into the system. This implies that the system is likely a server and that users
are working on the command line. While the wall command is generally used by sysadmins
to send out notices to users to let send out information (e.g., that the server is going down for
maintenance), it can be used by any user.
$ wall The system will be going down in 15 minutes to address a serious problem
Everyone logged into the system will see something like this:
0 seconds of 30 secondsVolume 0
If you want to use single quote marks in your message, enclose the message in double quote
marks like this:
mesg
If, for some reason, you don’t want to accept messages from another user, you can stop them
from arriving with the mesg command. This command can be used with a “n” argument to
refuse mail from the user or a “y” argument to allow the messages to arrive.
[ Learn how IT can harness the power and promise of 5G in this FREE CIO Roadmap
Report. Download now! ]
$ mesg n doug
$ mesg y doug
The blocked user will not be notified that their messages have been blocked. You can also
block or allow all messages with a mesg command like one of these:
$ mesg y
$ mesg n
write
Another command for sending text without reverting to email is write. This command can be
used to communicate with a specific user.
$ write nemo
Are you still at your desk?
I need to talk with you right away.
^C
Enter your text and use ^C to exit when you’re done. The command allows you to send text,
but doesn’t start a two-way conversation. It just sends the text. If the user is logged in on
more than one terminal, you can specify which terminal you want to send the message to or
you can rely on the system to choose the one with the shortest idle time.
$ write nemo#1
If the user you are trying to write to has messages blocked, you should see something like
this:
$ write nemo
write: nemo has messages disabled
talk/ytalk
The talk or ytalk command gives you a chance to have an interactive chat with one or more
other users. The command will bring up a double-pane (top and bottom) window. Each
individual will type into the top portion of the display on their screen and see the responses in
the bottom section(s). The respondents can respond to a talk request by typing "talk"
followed by the username of the person addressing them.
$ talk dory
The window can involve more than two participants if ytalk is used. As you can see in the
example below (the result of the "talk dory" command shown above), talk is often ytalk.
As explained above, on the other side of the conversation, the talk session window panes are
reversed:
To talk with someone on another system, you just need to add a -h option and the hostname
or IP address with a command like this:
Wrap-Up
There are a number of basic commands for sending messages to other logged-in users on
Linux systems, and they can be especially useful when you need to send out a quick message
to all of the users, prefer a quick exchange to a phone call or want to easily involve more than
two people in a quick messaging session.
Some commands, like wall, allow a message to be broadcast, but are not interactive. Others,
like talk, allow both lengthy and multi-user chats, avoiding the need to set up a conference
call when a fairly quick exchange of information is all that's required.
This tutorial will tell you how to identify a system's user account with commands like who,
who am i, etc.
If more than one person use a single system, then everyone may have their own user account.
Here, it will be helpful to know the user account details.
It also tells how to create a second user account and run program on that with the help of su
and sudo command.
whoami
Syntax:
1. whoami
The who command gives the information about the users logged on to the system.
Syntax:
1. who
who am i
This command displays the information about the current user only.
Syntax:
1. who am i
Look at the above snapshot, in our system current logged in user is sssit.
This command tells about the users who are logged in and what are they doing.
Syntax:
1. w
id
This command tells about your user id, primary group id, and a list of groups that belongs to
you.
Syntax:
1. id
Linux Create User | Linux Add user | Linux user add command
The Linux server allows us to create more than one user after installation. Linux is a Multi-
user system, which means more than one user can work in the same system at the same time.
We are allowed to do so through the Setup agent.
We must have to create an account in order to work with Linux as we cannot keep working
with the root account. We have one administrative account; a system administrator account is
responsible for managing the user accounts and groups of the system.
Adding or removing a user is one of the most basic tasks of a new Linux server.
Linux su Commands
Syntax:
1. su <username>
Example:
1. su jtp
su to root
You can change the user to root when you know the root password.
Syntax:
1. su root
su as root
The root user can become any existing user without knowing that user's password. Otherwise,
password is needed.
Example:
1. su - sssit
Look at the above snapshot, it is asking for password while switching from user jtp to sssit.
Example:
1. su - jtp
su - $username
By default, the su command maintains the same shell environment. To access the target user's
shell environment use the su command with (-) followed by the target user name.
Example:
1. su - jtp
2. su jtp
Look at the above snapshot, with the command "su - jtp" current shell environment is
/home/jtp and user is also jtp. With the command "su jtp" current shell environment is
/home/sssit and user is sssit.
su -
If any user name is not mentioned then by default, it will assume root as the target user.
Example:
1. su -
Look at the above snapshot, command "su -" has assumed root as the target.
Linux User Management
User management includes everything from creating a user to deleting a user on your system.
User management can be done in three ways on a Linux system.
Graphical tools are easy and suitable for new users, as it makes sure you'll not run into any
trouble.
Command line tools includes commands like useradd, userdel, passwd, etc. These are
mostly used by the server administrators.
root
The root user is the superuser and have all the powers for creating a user, deleting a user and
can even login with the other user's account. The root user always has userid 0.
useradd
Syntax:
Example:
Look at the above snapshot, we have created a user xyz along with creating a home directory
(-m), setting the name of home directory (-d), and a description (-c).
/etc/default/useradd
File /etc/default/useradd contains some user default options. The command useradd -D can
be used to display this file.
Syntax:
1. useradd -D
userdel
Syntax:
1. userdel -r <userName>
Example:
1. userdel -r xyz
Look at the above snapshot, first we have shown the xyz user account with 'tail' command.
To delete it, command "userdel -r xyz" is passed.
To recheck, again 'tail' command is passed and as you can see no xyz user account is
displayed.
Hence, it is deleted.
usermod
Example:
Look at the above snapshot, user name john is replaced by the new user name jhonny
/etc/skel/
The /etc/skel/ contains some hidden files which have profile settings and default values for
applications. Hence, it serves as a default home directory and user profile. While using
useradd -m option, the /etc/skel/ is copied to the newly created directory.
By using userdel -r option, you can delete home directory along with user account.
Syntax:
1. userdel -r <userName>
Example:
1. userdel -r john
Look at the above snapshot, both home directory as well as user account john is deleted.
Login Shell
The /etc/passwd file also tells about the login shell for the user.
Look at the above snapshot, user guest will log in with /bin/bash shell and user jtp will log in
with /bin/ksh shell.
You can change the shell mode with usermod command for a user.
Syntax:
Example:
Look at the above snapshot, shell of jtp is changed to /bin/bash from /bin/ksh.
chsh
Both the command chsh and chsh -s will work to change the shell.
Syntax:
1. chsh
Look at the above snapshot, command chsh has changed the sssit login shell from /bin/sh to
/bin/bash.
Syntax:
1. chsh -s <newShell>
Example:
1. chsh -s /bin/sh
This chapter tells you about the local users password. You will learn here to change the
password, set the password using different methods.
passwd
A user can set the password with the command passwd. Old password has to be typed twice
before entering the new one.
Syntax:
1. passwd
Look at the above snapshot, shell warns the user from creating a simple password.
Ultimately, after two or three attempts if password is not changed then the command passwd
fails and you have to pass the command again.
Although, these rules are not applied on the root user neither they need to type the old
password. They can change the password directly.
Syntax:
1. passwd <userName>
Example:
1. passwd jtp
Look at the above snapshot, password is changed successfully without any warning.
Shadow File
Shadow files are the encrypted user passwords which are kept in /etc/shadow. This file is
read-only directory and can be read only by root.
Syntax:
1. /etc/shadow
Look at the above snapshot, the /etc/shadow file contains nine columns separeted by colons.
Starting from left to right, these nine columns contain username, encrypted password, last
changed password day, number of days password must be left unchanged, password expiry
day, warning number of days before password expiry, number of days after expiry before
disabling the account, and the day account was disabled. Last column has no meaning yet.
Passwords are always stored in encrypted format. Encryption is done with crypt function. The
simplest way to add a user with a password is to add the user with the command useradd -m
and then set the user's password with command passwd.
Syntax:
1. useradd -m <userName>
Example:
1. useradd -m akki
Syntax:
1. passwd <typePassword>
Example:
1. passwd ****
Look at the above snapshot, user name akki is created with a password successfully.
To create a user with a password -p option is also used, but that requires an encrypted
password.
openssl passwd command can genereate several distinct hashes for the same pssword. To do
this, it uses salt.
This salt can be chosen and is visible as the first two characters of the hash as shown below.
Look at the above snapshot, the first two characters start from the defined sale '32'.
To create a user with password using openssl command, following syntax is used.
Syntax:
Example:
Look at the above snapshot, user aaa is created and its password is kept into command
history.
/etc/login.defs
The /etc/login.defs file contains some default settings like password aging and length
settings.,
Syntax:
1. grep PASS /etc/login.defs
chage
The chage command can be used by a user to know the information about their password.
The -l option is used to list the information.
Syntax:
1. chage -l <userName>
Example:
1. chage -l abc
Disabling A Password
Passwords in /etc/shadow are not saved starting with exclamation mark (!). If exclamation
mark is present in starting then password can not be used.
This feature can be used to disable a password and the process is called locking, disabling
and suspending a user account. It can be done in vi or with usermod command.
Here, we'll disable the password of akki with usermod command.
Syntax:
1. usermod -L <userName>
Example:
1. usermod -L akki
Look at the above snapshot, first command shows hashed password of akki, and command
"usermod -L akki" disables the password of akki. Now user akki can't authenticate using
this password.
Look at the above snapshot, hashed password is preceded with !, which means it is disabled.
Please note that root user will be able to open the akki account as password is not needed
here. And if user akki wouldn't have set password, then akki can also login.
Syntax:
1. usermod -U <userName>
Example:
1. usermod -U akki
Look at the above snapshot, hashed password of akki is unlocked now as there is no (!) mark
in starting.
Linux Groups
Users can be listed in different groups. Group allow us to set permission on the group level
instead of setting the permission on individual level.
Every Linux distribution have a graphical tool to manage groups. Groups can be managed by
graphical tools, command line tools and by vi or vigr depending upon the user's experience.
Only experienced users should use vi or vigr to manage groups, since it will do proper
locking or changes in the file.
groupadd
HTML Tutorial
Syntax:
1. groupadd <groupName>
Example:
1. groupadd php
2. groupadd java
3. groupadd android
4. groupadd spring
Look at the above snapshot, groups php, java, android and spring are created with groupadd
command.
Group File
The /etc/group file defines the group membership. A user can be a member of more than one
group.
Syntax:
1. /etc/group
Look at the above snapshot, first column indicates group name, second is the group's
encrypted password which may remain empty also, third is group identification (GID) and
fourth is the list of members. Fourth list is empty as these groups do not have members.
Groups
The group command tells about the group where current user belongs to.
Syntax:
1. groups
Look at the above snapshot, user jtp and sssit belongs to the different groups.
usermod
The group members can be edited with usermod or useradd command. If a group is not
listed then by default, usermod command will remove the user from every group of which he
is a member. Here, -a (append) option is used to prevent this from happening.
Syntax:
Example:
Look at the above snapshot, we have displayed the list of /etc/group. User akki and abc are
added into the group php, user jtp is added into java.
groupmod
With the help of groupmod command you can change the name of an already existing group.
Syntax:
Example:
gruopdel
The command groupdel will delete a group permanently from the system.
Syntax:
1. groupdel <group>
Example:
1. groupdel sql
Look at the above snapshot, group sql is deleted from the system.
gpasswd
Control of group membership can be passed on to another user with gpasswd command.
Syntax:
Example:
Group administrators need not to be a member of the group. They can add or remove a
member without being a member of that group.
File /etc/gshadow keeps the information about the group administrators as shown in below
snapshot.
To remove all the administrators from a group, set an empty administrator list.
Syntax:
Example:
Look at the above snapshot, administrator jtp is removed from the group java.