Unit Iii Linux
Unit Iii 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
Example:
gpasswd Group1
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
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.
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
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.
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.
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:
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’.
Both of them provide full read write and execute permission (code=7) to all the
group.
The same is the case with this.
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…
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.
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 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);
}
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;
}
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