0% found this document useful (0 votes)
34 views21 pages

Unit Iii Linux

The document discusses how to create multiple user accounts in Linux using the newusers command. It explains that newusers allows creating multiple user accounts simultaneously by providing user details in a file in a specific format. The file is then passed to newusers along with permissions. The command tries to create the accounts and update system databases.

Uploaded by

Rohan Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views21 pages

Unit Iii Linux

The document discusses how to create multiple user accounts in Linux using the newusers command. It explains that newusers allows creating multiple user accounts simultaneously by providing user details in a file in a specific format. The file is then passed to newusers along with permissions. The command tries to create the accounts and update system databases.

Uploaded by

Rohan Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

How to Create Multiple User Accounts in Linux

The two utilities for adding or creating user accounts in Unix/Linux systems
are adduser and useradd. These commands are designed to add a single user
account in the system at a time. What if you have multiple users accounts to be
created? That’s when you need a program such as newusers.
Newusers is a useful command line utility used to update and create new user
accounts at a single time. It is intended to be used in IT environments with large
systems where a system administrator needed to update or create multiple user
accounts in batch.
To create users in a batch, you can provide their information in a file in the
following format, same as the standard password file:
pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
where:
 pw_name: username
 pw_passwd: user’s password
 pw_uid: user’s ID
 pw_gid: user’s group ID
 pw_gecos: defines comments sections.
 pw_dir: defines the home directory of the user.
 pw_shell: defines user’s default shell.
For example, to add the user accounts ravi and tecmint, you can create a files
called users.txt as shown.
$ sudo vim users.txt
Next, add the user accounts details in the file in the following format.
ravi:213254lost:1002:1002:Tecmint Admin:/home/ravi:/bin/bash
tecmint:@!#@%$Most:1003:1003:Tecmint:/home/tecmint:/bin/bash

Create Multiple User Accounts in Linux


Save the file and set the required permissions on it.
$ sudo chmod 0600 users.txt
Now run the newusers command with the input file to add the above user
accounts at once.
$ sudo newusers users.txt
First, newusers program tries to create or update the specified accounts, and
then write these changes to the user or group databases. In case of any errors
except in the final writes to the databases, no changes are committed to the
databases. This is simply how the newusers command works.
If the previous command is successful, check
the /etc/passwd and /etc/groups files to confirm that the user accounts have
been added as shown.
$ cat /etc/passwd | grep -E "ravi|tecmint"

Verify User Accounts in Linux


For more information, see the newuser man page.
$ man newuser
usermod
The command usermod is used to modify the properties of an existing user.
Syntax:
1. usermod -c <'newName'> <oldName>
usermod -c 'jhonny' john
Look at the above snapshot, user name john is replaced by the new user
name jhonny.
userdel
To delete a user account userdel command is used.
Syntax:
1. userdel -r <userName>

How to create, delete, and modify groups in Linux


In Linux, groups are collections of users. Creating and managing groups is one of
the simplest ways to deal with multiple users simultaneously, especially when
dealing with permissions. The /etc/group file stores group information and is the
default configuration file.
There are 2 categories of groups in the Linux operating system
i.e. Primary and Secondary groups.
The Primary Group is a group that is automatically generated while creating a
user with a unique user ID simultaneously a group with an ID the same as the
user ID is created and the user gets added to the group and becomes the first
and only member of the group. This group is called the primary group.
A secondary group is a group that can be created separately with the help of
commands and we can then add users to it by changing the group ID of users.
The below command created a group with the name provided. The group while
creating gets a group ID and we can get to know everything about the group as
its name, ID, and the users present in it in the file “/etc/group”.
groupadd group_name
Example:
groupadd Group1

Setting the Password for the Group


Below command is used to set the password of the group. After executing the
command, we have to enter the new password which we want to assign to the
group. The password has to be given twice for confirmation purposes.
gpasswd group_name

Example:
gpasswd Group1

Command to Display the Group Password File:


To access information about groups and their passwords, you can view the
password file, /etc/gshadow. However, keep in mind that this file is not
intended for regular viewing. To gather more comprehensive information about
groups.
cat /etc/gshadow
Adding a User to an Existing Group
To add a user to an existing group, you can utilize the usermod command. By
specifying the group name, you can add a user to the desired group. However,
note that when a user is added to a new group, they are automatically removed
from their previous groups.
usermod -G group_name username

usermod -G group1 John_Doe

Note:
If we add a user to a group then it automatically gets removed from the previous
groups, we can prevent this by the command given below.
Command to Add User to Group Without Removing from Existing Groups
This command is used to add a user to a new group while preventing him from
getting removed from his existing groups.
usermod -aG *group_name *username

Example:
usermod -aG group1 John_Doe
Command to Add Multiple Users to a Group at once
To add multiple users to a group simultaneously, you can utilize the gpasswd
command with the -M option. This command allows you to specify a list of
usernames separated by commas.
gpasswd -M *username1, *username2, *username3 ...., *usernamen
*group_name

Example:
gpasswd -M Person1, Person2, Person3 Group1

Deleting a User from a Group


Below command is used to delete a user from a group. The user is then removed
from the group though it is still a valid user in the system but it is no longer a
part of the group. The user remains part of the groups which it was in and if it
was part of no other group then it will be part of its primary group.
gpasswd -d *username1 *group_name
Example:
gpasswd -d Person1 Group1

Command to Delete a Group


To delete a group from the system, use the groupdel command. This action
removes the group while retaining the users who were members of the group.
They will revert to their primary groups if they are not part of any other groups.
groupdel *group_name

Example:
groupdel Group1

Permissions in Linux
Linux is a multi-user operating system, so it has security to prevent people from
accessing each other’s confidential files. When you execute a “ls” command, you
are not given any information about the security of the files, because by default
“ls” only lists the names of files. You can get more information by using an
“option” with the “ls” command. All options start with a ‘-‘. For example, to
execute “ls” with the “long listing” option, you would type ls -l . When you do so,
each file will be listed on a separate line in a long format. There is an example in
the window below.

How to Check the Permission of Files in Linux


ls -l
There’s a lot of information in those lines.

The first character = ‘-‘, which means it’s a file ‘d’, which means it’s a directory.
The next nine characters = (rw-r–r–) show the security
The next column shows the owner of the file. (Here it is `root`)
The next column shows the group owner of the file. (Here it is `root` which has
special access to these files)
The next column shows the size of the file in bytes.
The next column shows the date and time the file was last modified.
Last Column = File_name or Directory_name. (For example, here are: prac, snap,
test, example)
What are the three permission groups in Linux?
First, you must think of those nine characters as three sets of three characters
(see the box at the bottom). Each of the three “rwx” characters refers to a
different operation you can perform on the file.

Owners: These permissions apply exclusively to the individuals who own the
files or directories.
Groups: Permissions can be assigned to a specific group of users, impacting only
those within that particular group.
All Users: These permissions apply universally to all users on the system,
presenting the highest security risk. Assigning permissions to all users should be
done cautiously to prevent potential security vulnerabilities.
--- --- ---
rwx rwx rwx
user group other
What are the three kinds of file permissions in Linux?
There are three kinds of file permissions in Linux Read, write, and execute.
Letters Definition

‘r’ “read” the file’s contents.

“write”, or modify, the file’s


‘w’ contents.

“execute” the file. This permission is


‘x’ given only if the file is a program.

Symbols: `+`, `-` and `=`Option in Linux File Permission


Operators Definition

`+` Add permissions

`-` Remove permissions

Set the permissions to the specified


`=` values

User, group, and others Option in Linux File Permission


Reference Class Description

The user permissions


apply only to the owner
of the file or directory,
they will not impact the
`u` user actions of other users.

The group permissions


apply only to the group
that has been assigned
to the file or directory,
they will not affect the
`g` group actions of other users.
Reference Class Description

The other permissions


apply to all other users
on the system, this is
the permission group
that you want to watch
`o` others the most.

All three (owner,


`a` All three groups, others)

Reading the Security Permissions in Linux


For example: “rw- r-x r–“
 “rw-“: the first three characters `rw-`. This means that the owner of the
file can “read” it (look at its contents) and “write” it (modify its contents).
we cannot execute it because it is not a program but a text file.
 “r-x”: the second set of three characters “r-x”. This means that the
members of the group can only read and execute the files.
 “r–“: The final three characters “r–” show the permissions allowed to
other users who have a UserID on this Linux system. This means anyone in
our Linux world can read but cannot modify or execute the files’
contents.
How to Change Permissions in Linux
The command you use to change the security permissions on files is called
“chmod“, which stands for “change mode” because the nine security characters
are collectively called the security “mode” of the file.
An example will make this clearer.

For example, if you want to give “execute” permission to the world (“other”) for
file “xyz.txt”, you will start by typing.
chmod o
Now you would type a ‘+’ to say that you are “adding” permission.
chmod o+
Then you would type an ‘x’ to say that you are adding “execute” permission.
chmod o+x
Finally, specify which file you are changing.
chmod o+x xyz.txt
You can see the change in the picture below.
chmod o+x xyz.txt

You can also change multiple permissions at once. For example, if you want to
take all permissions away from everyone, you would type.
chmod ugo-rwx xyz.txt
The code above revokes all the read(r), write(w), and execute(x) permission
from all user(u), group(g), and others(o) for the file xyz.txt which results in this.

Another example can be this:


chmod ug+rw,o-x abc.mp4
The code above adds read(r) and write(w) permission to both user(u) and
group(g) and revoke execute(x) permission from others(o) for the file abc.mp4.
Something like this:
chmod ug=rx,o+r abc.c

assigns read(r) and execute(x) permission to both user(u) and group(g) and add
read permission to others for the file abc.c.
There can be numerous combinations of file permissions you can invoke revoke
and assign. You can try some on your Linux system.
The octal notations in Permissions in Linux
chmod o
Now you would type a ‘+’ to say that you are “adding” permission.
chmod o+
Then you would type an ‘x’ to say that you are adding “execute” permission.
chmod o+x
Finally, specify which file you are changing.
chmod o+x xyz.txt
You can see the change in the picture below.

You can also change multiple permissions at once. For example, if you want to
take all permissions away from everyone, you would type.
chmod ugo-rwx xyz.txt
The code above revokes all the read(r), write(w), and execute(x) permission
from all user(u), group(g), and others(o) for the file xyz.txt which results in this.

Another example can be this:

chmod ug+rw,o-x abc.mp4

The code above adds read(r) and write(w) permission to both user(u) and
group(g) and revoke execute(x) permission from others(o) for the file abc.mp4.
Something like this:

chmod ug=rx,o+r abc.c

assigns read(r) and execute(x) permission to both user(u) and group(g) and add
read permission to others for the file abc.c.
There can be numerous combinations of file permissions you can invoke revoke
and assign. You can try some on your Linux system.
You can also use octal notations like this.

octal notations
Using the octal notations table instead of ‘r’, ‘w’, and ‘x’. Each digit octal
notation can be used for either of the group ‘u’, ‘g’, or’o’.

So, the following work is the same.

chmod ugo+rwx [file_name]


chmod 777 [file_name]

Both of them provide full read write and execute permission (code=7) to all the
group.
The same is the case with this.

chmod u=r,g=wx,o=rx [file_name]


chmod 435 [file_name]

Both the codes give read (code=4) user permission, write and execute (code=3)
for the group and read and execute (code=5) for others.
And even this…

chmod 775 [file_name]


chmod ug+rwx,o=rx [file_name]

Both the commands give all permissions (code=7) to the user and group, read
and execute (code=5) for others.
Frequently Asked Questions
How do I change file permissions in Linux using the command line?
To change file permissions in Linux, you can use the `chmod` command followed
by the desired permission settings.
For example: If we want to grants read, write, and execute permissions to the
owner, and read and execute permissions to the group and others.
chmod 755 filename
Can I change file permissions for multiple files at once?
Yes, you can change file permissions for multiple files simultaneously using
wildcards with the `chmod` command.
For instance to sets read and write permissions for the owner and read-only
permissions for the group and others for all text files in the directory.
chmod 644 *.txt
How do I change the owner of a file in Linux?
To change the owner of a file, you can use the `chown` command.
For example : If we want to changes the owner to “newowner” and the group to
“newsgroup.”
chown newowner:newgroup filename
What are the symbolic and octal representations in file permissions?
File permissions can be expressed in both symbolic (e.g., u=rw, g=r, o=r) and
octal (e.g., 644) representations. Symbolic representations offer a more intuitive
way to specify permissions, while octal representations provide a concise
numerical format.

Changing ownership in Linux


Another helpful command is changing ownerships of files and directories in
Linux:
 chown name filename
 chown name foldername

These commands will give ownership to someone, but all sub files and
directories still belong to the original owner.
You can also combine the group and ownership command by using:
 chown -R name:filename /home/name/directoryname
Linux Daemon
The programs are running in the background of your Linux system, providing
essential services like file sharing, web hosting, and email are called daemons.
What is a Daemon Process in Linux?
Daemon in Linux is a background process designed to perform specific tasks or
provide services without direct user intervention.
 Daemons operate in the background, detached from any specific user
session.
 Daemons are responsible for providing essential services to the system or
other processes. Examples include web servers (e.g., Apache), database
servers (e.g., MySQL), network services (e.g., SSH), and logging services
(e.g., rsyslogd).
 Daemon processes are launched automatically by the system's
initialization process or the init system in use (such as systemd, SysVinit,
or Upstart).
 During system startup, the init system reads its configuration files and
starts the necessary daemons as specified.
 Once a daemon process starts, it enters a loop where it waits for events or
requests, processes them, and then continues waiting for events.
Don't get confused between programs, processes and services in Linux. Refer to
the below table to understand the difference between them:
Creating a Daemon Process on Linux
Creating a daemon process in Linux involves specific steps to ensure that the
process runs independently in the background and provides the desired
services.
Let's explore the process of creating a daemon in Linux with an example. In this
example, the daemon will create a new file and write the current time to it every
five seconds.
The code for the same is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

int main() {
pid_t pid;

// Step 1: Fork the Parent Process


pid = fork();

// Step 2: If the fork fails, exit the program


if (pid < 0) {
exit(EXIT_FAILURE);
}

// Step 3: On success, the PID of the child process is returned in the parent, and
0 is returned in the child
if (pid > 0) {
exit(EXIT_SUCCESS);
}

// Step 4: Set a new session


if (setsid() < 0) {
exit(EXIT_FAILURE);
}

// Step 5: Change the current working directory to the root


chdir("/");

// Step 6: Close Standard File Descriptors


close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

// Daemon specific action


while (1) {
FILE *file = fopen("/tmp/time.txt", "a");
if (file == NULL) {
printf("Failed to open the file\n");
return 1;
}

time_t now = time(NULL);


fprintf(file, "Current time: %s", ctime(&now));
fclose(file);
sleep(5); // The daemon works every 5 seconds
}

return EXIT_SUCCESS;
}
Knowing what is daemon in Linux enables us to control and monitor vital system
services. Now, follow the steps to create a daemon in Linux:
1. Fork the Parent Proces:
o The process begins with the parent process, and to create a
daemon, a child process is forked from the parent. This is
accomplished with the fork() system call.
pid = fork();
2. Exit on Fork Failure:
o If the fork() system call fails, it returns a value of less than 0. If this
happens, the program exits.
if (pid < 0) {
exit(EXIT_FAILURE);
}
3. Terminate the Parent Process:
o If the fork() system call succeeds, the PID (Process ID) of the child
process is returned in the parent, and 0 is returned in the child. If
the process is the parent, then this code will exit.
if (pid > 0) {
exit(EXIT_SUCCESS);
}
4. Create a New Session:
o The setsid() system call is used to create a new session if the fork
was successful. The child process becomes the leader of the new
session and also becomes the process group leader of a new
process group.
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
5. Change Directory to Root:
o Since a daemon process shouldn't use the file system, it should
change its current working directory to root ("/"). This is
accomplished using the chdir() system call.
chdir("/");
6. Close Standard File Descriptors:
o A daemon process needs to close standard file descriptors to free
them for reuse. This is achieved with the close() system call.
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
7. Implement Daemon Functionality:
o The infinite while loop is where the daemon process does its work.
In this case, it writes the current time to a file every five seconds.
while (1) {
FILE *file = fopen("/tmp/time.txt", "a");
if (file == NULL) {
printf("Failed to open the file\n");
return 1;
}

time_t now = time(NULL);


fprintf(file, "Current time: %s", ctime(&now));
fclose(file);
sleep(5); // The daemon works every 5 seconds
}
Let's shed light on what is a daemon in Linux by running the provided
daemon process program written in C, you can follow these steps:
1. Save the program code in a file, for example, time_daemon.c.
2. Open a terminal and navigate to the directory where you saved the file.
3. Compile the program using a C compiler. For example, if you
have gcc installed, you can use the following command:
gcc -o time_daemon time_daemon.c
Start the daemon in Linux using the following command:
./time_daemon

You can check that the daemon is running by executing the following
command:
ps aux | grep time_daemon

You can stop the daemon process by using the following command:
killall time_daemon

You might also like