0% found this document useful (0 votes)
5 views16 pages

Linux File

The document covers practical exercises on Linux system administration, focusing on single-user mode, user and group management, ownership, permissions, and quotas. It details the characteristics and use cases of single-user mode, along with commands for managing user accounts and groups, including useradd, usermod, and groupadd. Additionally, it explains file ownership and permissions management, emphasizing the use of commands like chown, chgrp, and chmod.

Uploaded by

amit
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)
5 views16 pages

Linux File

The document covers practical exercises on Linux system administration, focusing on single-user mode, user and group management, ownership, permissions, and quotas. It details the characteristics and use cases of single-user mode, along with commands for managing user accounts and groups, including useradd, usermod, and groupadd. Additionally, it explains file ownership and permissions management, emphasizing the use of commands like chown, chgrp, and chmod.

Uploaded by

amit
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/ 16

Practical 6 – Single-User Mode

Single-user mode in a Linux system is a specialized operational state that


allows only one user, typically the root user, to access the system for
maintenance and recovery purposes. This mode is particularly useful for
performing critical tasks when the system is unable to operate normally or when
administrative access is required without interference from other users or
processes.
Key Characteristics of Single-User Mode
Minimal Environment:
In single-user mode, the system boots with a minimal set of services and
processes. This means that many non-essential services, including network
services, are not started, reducing security vulnerabilities during maintenance
tasks.
Root Access:
The user operating in this mode has full administrative privileges, allowing
them to execute commands that can modify system configurations, repair
filesystems, reset passwords, and perform other critical maintenance tasks.
Use Cases:
Common scenarios for entering single-user mode include:
● Recovering from boot failures.
● Resetting lost root or user passwords.
● Running filesystem checks (fsck) on damaged partitions.
● Modifying configuration files that may prevent normal booting.
Important Considerations
Data Safety: When operating in single-user mode, there is a risk of data loss if
local filesystems cannot be mounted successfully. Therefore, caution is advised
when making changes.
Security Risks: Since single-user mode is not password-protected by default,
anyone with physical access to the machine can gain root access, which poses a
security risk
Practical 7 – Working with users and Groups

1. Manage users and Groups


2. Manage ownership, permissions, and quotas

1) MANAGE USERS AND GROUPS


❖ Linux user accounts
❖ Linux groups

1. LINUX USER ACCOUNTS


✔ How Linux user accounts work
✔ Where Linux user accounts are stored
✔ Creating and managing user accounts from the command line
● To view information about the user_name account on my Linux
system, you would enter finger user_name
● The following information about the user account:
⮚ Login This is the username that is used to authenticate to the system.
⮚ Name This is the user’s full name.
⮚ Directory This is the user’s home directory.
⮚ Shell This is the default shell that will be provided to the user.
⮚ Last Login This displays the last time the user logged in and where
from.

● In addition to having a home directory and default shell assigned, each


user account is also assigned a unique user ID (UID) number when
they are created.
● No two user accounts on the system will have the same UID.
● To view the UID for a given user account, you can use the id
username command from the shell prompt. For example, to view
information about our vmk user account, we can enter
● id vmk at the shell prompt
● On a SUSE Linux system, the first regular user account created on the
system is always assigned a UID of 1000. The next user account will
be assigned a UID of 1001...
● Other distributions may use a different numbering scheme for the
UID, however. For example, UIDs on a Fedora system start at 500
instead of 1000.
● The root user account is always assigned a UID of 0 on most Linux
distributions.
● It’s this UID that the operating system actually uses to control access
to files and directories in the file system.

Creating and Managing User Accounts from the Command Line


▪ Using useradd
▪ Using passwd
▪ Using usermod
▪ Using userdel

i. Using useradd
Syntax: useradd options username
examplle : useradd ncth
ncth account is created using the default parameters contained in the following
configuration files: /etc/default/useradd/etc/login.defs. This file contains values
that can be used for the GID and UID parameters when creating an account with
useradd. It also contains defaults for creating passwords in /etc/shadow.
You can also view these default values by entering useradd –D at the shell
prompt.

Options:
o –c Includes the user’s full name.
o –e Specifies the date when the user account will be disabled.
Format the date as yyyy-mm-dd.
o –f Specifies the number of days after password expiration before
the account is disabled. Use a value of –1 to disable this
functionality, e.g., useradd –f –1 jmcarthur.
o –g Specifies the user’s default group.
o –G Specifies additional groups that the user is to be made a
member of.
o –M Specifies that the user account be created without a home
directory.
o –m Specifies the user’s home directory.
o –n Used only on Red Hat or Fedora systems. By default, these
systems create a new group with the same name as the user every
time an account is created. Using this option will turn off this
functionality.
o –p Specifies the user’s password.
o –r Specifies that the user being created is a system user.
o –s Specifies the default shell for the user.
o –u Manually specifies a UID for the user.
o EX: useradd –c “Tommy” ncth1
o useradd –c “Truong Khac Tung” –m –p “tung123” –s “/bin/bash”
tktung

ii. Using passwd


o The passwd utility is used to change an existing user’s password
o You can find out this information using the –S option with passwd.
For example, we could enter passwd –S vmk at the shell promp
o Syntax: passwd username
iii. Using usermod
o From time to time, you will need to modify an existing user account. The
syntax for usermod is very similar to that used by useradd.
o Syntax:
usermod options username
Options:
o –c Edits the user’s full name.
o –e Sets the date when the user account will be disabled. Format the
date as yyyy-mm-dd.
o –f Sets the number of days after password expiration before the
account is disabled. Use a value of –1 to disable this functionality.
o –g Sets the user’s default group.
o –G Specifies additional groups that the user is to be made a member of.
o –l Changes the username.
o –L Locks the user’s account. This option invalidates the user’s password.
o –m Sets the user’s home directory.
o –p Sets the user’s password.
o –s Specifies the default shell for the user.
o –u Sets the UID for the user.
o –U Unlocks a user’s account that has been locked.
● useradd –c “your_full_name” –m –p “your_password” –s “/bin/bash”
your_username.
● tail /etc/passwd
● Create a user account using your system’s default settings by entering
useradd abc
● Passwd abc -> enter password

iv. Using userdel


o Syntax:
Userdel username
o ex: userdel ncth
o It’s important to note that, by default, userdel will not remove the user’s
home directory from the file system. If you do want to remove the home
directory when you delete the user, you need to use the –r option in the
command line. For example, entering userdel –r ncth will remove the
account and delete her home directory.
2. LINUX GROUPS
✔ How Linux groups work.
✔ Managing groups from the command line.
i. How Linux Groups Work
● If your Linux system has been configured to use local authentication,
your groups are defined in the /etc/group file. Each record is composed
of the following four fields:
Group:Password:GID:Users
● Group Specifies the name of the group.
● Password Specifies the group password.
● GID Specifies the group ID (GID) number of the group.
● Users Lists the members of the group.
● As with /etc/shadow, each line in /etc/gshadow represents a record for a
single group. Each record is composed of the following fields:
Group_Name:Password:Group_Admins:Group_Members
ii. Managing groups from the command line
● Using groupadd
● Using groupmod
● Using groupdel

i. Using groupadd
● Syntax:
groupadd options groupname
● Options:
o –g Specifies a GID for the new group.
o –p Specifies a password for the group.
o –r Specifies that the group being created is a system group.
ii. Using groupmod
● To modify a group, including adding users to the group membership, you
use the groupmod utility.
● Syntax:
groupmod options group
● Options:
o –g Changes the group’s GID number.
o –p Changes the group’s password.
o –A Adds a user account to the group.
o –R Removes a user account from the group.
● If we wanted to add ncth to the group, we would enter
groupmod –A “ncth” student at the shell prompt.
iii. Using groupdel
● Syntax:
groupdel group_name
● ex: groupdel student

2) MANAGE OWNERSHIP, PERMISSIONS, AND QUOTAS


✔ Managing ownership
✔ Managing permissions
✔ Implementing disk quotas

● Managing ownership
o How ownership works
o Managing ownership from the command line
i. How ownership works
o Anytime a user creates a new file or directory, his or her user account is
assigned as that file or directory’s “owner.” For example, suppose the
“Anchal” user logs in to her Linux system and creates a file named
linux_introduction.odt using OpenOffice.org in home directory. Because
she created this file, she is automatically assigned ownership of
linux_introduction.odt. By right-clicking on this file in the system’s
graphical user interface and selecting Properties | Permissions, you can
view who owns the file.
o You can also view file ownership from the command line using the ls –l
command
ii. Managing ownership from the command line
You can specify a different user and/or group as the owner of a given file
or directory. To change the user who owns a file, you must be logged in
as root. To change the group that owns a file, you must be logged in as
root or as the user who currently owns the file.
✔ Using chown
✔ Using chgrp
● Using chown
o The chown utility can be used to change the user or group that owns a
file or directory.
o Syntax: chown user.group file or directory.
o Ex: If I wanted to change the file’s owner to the ncth1 user, I would
enter chown ncth1 /tmp/myfile.txt
● Using chgrp
o In addition to chown, you can also use chgrp to change the group that
owns a file or directory.
o Syntax: chgrp group file (or directory)
o For example: chgrp student /tmp/newfile.txt.

● Managing ownership
o How ownership works
o Managing ownership from the command line
iii. How ownership works
o Anytime a user creates a new file or directory, his or her user account is
assigned as that file or directory’s “owner.” For example, suppose the
“Anchal” user logs in to her Linux system and creates a file named
linux_introduction.odt using OpenOffice.org in home directory. Because
she created this file, she is automatically assigned ownership of
linux_introduction.odt. By right-clicking on this file in the system’s
graphical user interface and selecting Properties | Permissions, you can
view who owns the file.
o You can also view file ownership from the command line using the ls –l
command
iv. Managing ownership from the command line
You can specify a different user and/or group as the owner of a given file
or directory. To change the user who owns a file, you must be logged in
as root. To change the group that owns a file, you must be logged in as
root or as the user who currently owns the file.
✔ Using chown
✔ Using chgrp
● Using chown
o The chown utility can be used to change the user or group that owns a
file or directory.
o Syntax: chown user.group file or directory.
o Ex: If I wanted to change the file’s owner to the ncth1 user, I would
enter chown ncth1 /tmp/myfile.txt
● Using chgrp
o In addition to chown, you can also use chgrp to change the group that
owns a file or directory.
o Syntax: chgrp group file (or directory)
o For example: chgrp student /tmp/newfile.txt.
❑ Managing permissions
✔ How permissions work
✔ Managing permissions from the command line
✔ Working with default permissions
✔ Working with special permissions
- How permissions work
Each file or directory in your Linux file system stores the specific
permissions assigned to it. These permissions together constitute the
mode of the file. These permissions are assigned to each of three different
entities for each file and directory in the file system:
✔ Owner This is the user account that has been assigned to be the file or
directory’s owner. Permissions assigned to the owner apply only to that
user account.
✔ Group This is the group that has been assigned ownership of the file or
directory. Permissions assigned to the group apply to all user accounts
that are members of that group.
✔ Others This entity refers to all other users who have successful
authenticated to the system. Permissions assigned to this entity apply to
these user accounts.
ls –l
- Managing Permissions from the Command Line with chmod
Chmod entity=permissions filename

Owner, g for Group, and o for Others in the entity portion of the command.
You substitute r, w, and/or x for the permissions portion of the command. For
example, suppose I wanted to change the mode of contacts.odt to –rw–rw–r– –
chmod u=rw,g=rw,o=r contacts.odt
❑ You can also use chmod to toggle a particular permission on or
off using the + or – signs. For example, suppose I want to turn off
the write permission I just gave to Group for the contacts.odt file.
I could enter chmod g–w contacts.odt at the shell prompt.
❑ You can modify all three entities at once with only three
characters. To do this, enter
chmod numeric_permission filename
ex: chmod 660 contacts.odt
Working with default permissions
❑ By default, Linux assigns rw–rw–rw– (666) permissions to every
file whenever it is created in the file system. It also assigns
rwxrwxrwx permissions to every directory created in the file
system. It also assigns rwxrwxrwx permissions to every directory
created in the file system.
❑ To increase the overall security of the system, Linux uses a
variable called umask to automatically remove permissions from
the default mode whenever a file or directory is created in the file
system. The value of umask is a three-digit number.
Practical 8 – Script to perform arithmetic operations on
Integers
SCRIPT:

OUTPUT:
Practical 9 – Script to perform arithmetic operations on
Real Numbers
Practical 10 – Write a script to copy a file
Practical 11 – Write a script to print something using for
loop

You might also like