0% found this document useful (0 votes)
5 views

lecture 4

The document covers Linux system accounts and groups, detailing the types of accounts (root, system, and user accounts) and their management. It includes commands for creating, modifying, and deleting user and group accounts, as well as an introduction to globbing and text utilities. Additionally, it explains standard text streams and redirection in Linux, including how to manage input and output streams effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

lecture 4

The document covers Linux system accounts and groups, detailing the types of accounts (root, system, and user accounts) and their management. It includes commands for creating, modifying, and deleting user and group accounts, as well as an introduction to globbing and text utilities. Additionally, it explains standard text streams and redirection in Linux, including how to manage input and output streams effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Linux and shell programming

Lecture 4:
system accounts and groups

© spring 2025 – Dr. Dina Awny


Table of Contents

● System Accounts
● Managing users and groups
● Globbing Introduction
● Text Utilities
● Redirection
System Accounts
● There are three types of accounts on a lunix system :
❑ Root account
• This is also called superuser and would have complete and unfettered control of the system.

• A superuser can run any commands without any restriction. This user should be assumed as a

system administrator. Root's UID is always 0


❑ System accounts (services account)
• System accounts are those needed for the operation of system-specific components for

example mail accounts (mail services) and the appache accounts (http service).
• These accounts are usually needed for some specific function on your system, and any

modifications to them could adversely affect the system.


• Id Used for OS and services 0 – 999.
System Accounts
❑ User accounts
• User accounts provide interactive access to the system for users and groups of users.

• General users are typically assigned to these accounts and usually have limited access

to critical system files and directories.


• ID > 1000

• lunix supports a concept of Group Account which logically groups a number of accounts.

Page - 4
View all user accounts

Page - 5
System’s username
● whoami: It tells you about the system's username.
● who command gives the information about the users logged on to the system.
● w command tells about the users who are logged in and what are they doing.
● Id command tells about your user id, primary group id, and a list of groups that belongs to you.

Page - 6
Managing Users and Groups
● There are four main user administration files −
• /etc/passwd − Keeps the user account and password information. This file holds the
majority of information about accounts on the lunix system.
• /etc/shadow − Holds the encrypted password of the corresponding account. Not all
the systems support this file.
• /etc/group − This file contains the group information for each account.
• /etc/gshadow − This file contains secure group account information.
• /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.

Page - 7 Check all the above files using the cat command.
Commands to create and manage accounts and groups

Sr.No. Command & Description


1 useradd Adds accounts to the system
2 usermod Modifies account attributes
3 Userdel Deletes accounts from the system
4 groupadd Adds groups to the system
5 groupmod Modifies group attributes
6 groupdel Removes groups from the system

Page - 8
Create a Group

• We will now understand how to create a group.


• For this, we need to create groups before creating any account otherwise, we can make
use of the existing groups in our system.
• We have all the groups listed in /etc/groups file.
• All the default groups are system account specific groups and it is not recommended to
use them for ordinary accounts.
• So, following is the syntax to create a new group account −
groupadd [-g gid [-o]] [-r] [-f] groupname

Page - 9
The following table lists out the parameters
Sr.No. Option & Description
1 -g gid The numerical value of the group's ID
2 -o This option permits to add group with non-unique GID
3 -r This flag instructs groupadd to add a system account
4 -f This option causes to just exit with success status, if the specified group
already exists. With -g, if the specified GID already exists, other (unique) GID is
chosen
5 Groupname Actual group name to be created

• following example creates a developers group with default values, which is very much
acceptable for most of the administrators. $ groupadd developers

Page - 10
Page - 11
Modify a Group

● To modify a group, use the groupmod syntax


● To change the developers_2 group name to developer: $ groupmod -n developer developer_2
● Here is how you will change the financial GID to 45 − $ groupmod -g 45 developer
• Delete a Group
● To delete an existing group, all you need is the groupdel command and the group name.
To delete the financial group, the command is − $ groupdel developer
● This removes only the group, not the files associated with that group. The files are still
accessible by their owners

Page - 12
Create an Account
● Following is the syntax to create a user's account −
useradd -d homedir -g groupname -s shell -u userid accountname
● The following table lists out the parameters −

Sr.No. Option & Description

1 -d homedir Specifies home directory for the account


2 -g groupname Specifies a group account for this account
3 -m Creates the home directory if it doesn't exist
4 -s shell Specifies the default shell for this account
5 -u userid You can specify a user id for this account
6 accountname Actual account name to be created
Page - 13
Create an account
• The useradd command modifies the /etc/passwd, /etc/shadow, and /etc/group files and creates a
home directory.
• Following is the example that creates an account lunix_2, setting its home directory
to /home/lunix_2 and the group as developers.
$ useradd -d /home/lunix_2 -g developers -s /bin/bash lunix_2
• Before issuing the above command, make sure you already have the developers group created using
the groupadd command.
• Once an account is created you can set its password using the passwd command $ passwd lunix_2
• When you type passwd accountname, it gives you an option to change the password, provided you
are a superuser. Otherwise, you can change just your password using the same command but
without specifying your account name.
Page - 14
Create a user with an expiry date
● To create a user with an expiry date that means after a particular date, it will be auto-deleted.

Page - 15
Delete an Account
• The userdel command can be used to delete an existing user. This is a very
dangerous command if not used with caution.
• There is only one argument or option available for the command -r, for removing
the account's home directory and mail file.
● For example, to remove account lunix_2, issue the following command −
$ userdel -r lunix_2
● If you want to keep the home directory for backup purposes, omit the -r option.
You can remove the home directory as needed at a later time

Page - 16
Globbing Introduction
Globbing

• Globs, also called “wildcards”, are special characters to the shell designed to match

filenames used for manipulating (listing, copying, moving, etc.) groups of files

• Three types:

○ * = match zero or more of any character

○ ? = match exactly one character

○ [ ] = match exactly one character from a range of characters.

Page - 18
“*” Wildcard Examples

• Display all files in the current directory:


○ $echo * or $ls *
• Display all files in the current directory that
begin with the letter D:
○ $echo D* or $ls D*
• Display all files in the current directory that
begin with "D" and have an "n":
○ $echo D*n* or $ls D*n*

Page - 19
“?” Wildcard Examples
• Display all files in the current directory that have exactly one character in file name:
○ $echo ? or $ls ?
• Display all files in the current directory that begin with the letter D and have three more
characters:
○ $echo D??? or $ls D???

Page - 20
“[ ]” Wildcard Examples
• Display all files in the current directory that begin with "a", "b" or "c":
○ $echo [abc]* or $ls [abc]*
○ $echo [a-c]* or $ls [a-c]*
• When using a range ([a-c]), the range is based on the ASCII text table
• Display all files in the current directory that don't begin with "a", "b" or "c":
○ $echo [^a-c]* or $ls [^a-c]*

Page - 21
More complex Examples

• Display all files in the current directory that begin with "a", "b" or "c" and are at least 5
characters long:
○ $echo[abc]????* or $ls [abc]????*
• Display all files in the current directory that begin with don't end with "x", "y" or "z":
○ $echo*[^xyz] or $ls*[^xyz]

Page - 22
Text Utilities
Display file content
• The cat command merges two or more files into a single file:
$cat file1 file2 > file3
• Can also be used to display the contents of a file to the terminal:
$cat file1
• The fmt command does very simple text formatting
• The most common purpose is to format a text file with a maximum line width
• Use -w option to specify the width of each line

Page - 24
pr command

• The pr command prepares a file for printing by


● - breaking the file into "pages“
● - displaying header information at the top of each page
• Use -l to specify lines per page
• Use -d to double space
• Use -o to specify a margin

Page - 25
The file command

• Only text files should be displayed, not binary files


• Displaying binary file can cause terminal corruption
• Either logout or run the reset command to fix terminal corruption
• To view file contents type, use the file command:
● $file filename
type of file
[root@localhost username]# file lab_note
lab_note: ASCII text
[root@localhost username]# file myscript.sh
myscript.sh: Bourne-Again shell script, ASCII text executable

Page - 26
The split command
• The split command will break large files into smaller files
• Useful for file transfer when large files create problems
• Syntax:
● split [OPTION]...[INPUT [PREFIX]]
• INPUT is a file or stdin
• By default the new files will be named with a prefix of x and a suffix of aa, ab, etc.
• For example, the first file would be called xaa, the second file would be called xab, etc
• The -d option splits files to have a numeric suffix instead of a default alphabetic suffix
• The file names will start with the PREFIX, if specified; if not specified, then "x" is used

Page - 27
The nl command

• The nl command will number the lines of its output


• By default will only number lines that are not blank
• To number every line use: $nl -ba

Page - 28
tr command

• The tr command can be used to translate from one set of characters to another set

Page - 29
The sed command
● The sed (Stream Editor) command is a powerful tool used for text manipulation, search, find-and-replace,
insertion, and deletion in Linux.
● It processes text line by line and can modify files or input streams without opening them in an editor.
sed [OPTIONS] 'COMMAND' file

Page - 30
Redirection
Standard Text Streams and Redirection
● Data is entered into the computer via stdin (usually the keyboard), and the resulting
output goes to stdout (usually the shell). These pathways are called streams. However,
it's possible to alter these input and output locations, causing the computer to get
information from somewhere other than stdin or send the results somewhere other
than stdout. This functionality is referred to as redirection.

Page - 32
Standard Text Streams

• stdin − This is referred to as the standard input and the associated file descriptor
is 0. This is also represented as STDIN. The lunix program will read the default
input from STDIN.
• stdout − This is referred to as the standard output and the associated file
descriptor is 1. This is also represented as STDOUT. The lunix program will
write the default output at STDOUT
• stderr − This is referred to as the standard error and the associated file
descriptor is 2. This is also represented as STDERR. The Unix program will
write all the error messages at STDERR.

Page - 33
File Descriptors

• In simple words, file descriptors are integers (numbers) that act as unique identifiers
for an open file (or other I/O resource) in a Linux system.
• Remember, in lunix, "everything is a file descriptor or a process" or even
"everything can have a file descriptor“.
• It's important and useful to understand how the so-called three standard file
descriptors, or standard streams, work because all processes use these channels for
input and output operation.

Page - 34
Page - 35
Standard Out
• When a command executes without any errors, the output produced is known as
standard out (also called stdout or STDOUT)
• By default stdout is sent to the terminal
• You can redirect stdout from a command into a file
• For example, when you type the date command, the resulting time and date output is
displayed on the screen.
• It is possible, however, to redirect this output from stdout to somewhere else. In this
case, I'm going to redirect the results to a file named specifications.txt. I'll confirm it
worked by using the cat command to view the file contents.

Page - 36
Standard Out
• To redirect stdout of a command to a file, use the “>”:
○ $ls > /tmp/ls.txt
• A single > will overwrite existing file contents
• Use >> to append to the end of a file
• Stdout is assign a numeric value of 1, so it could also be used like this:
○ $ls 1> /tmp/ls.txt
• The >> redirector even works on an empty file. That means that you could
conceivably ignore the regular > redirector to alleviate the potential to overwrite data,
and always rely on the >> redirector instead.

Page - 37
Page - 38
Standard Error

• When command encounters an error, it will produce output that is known as standard error
(also called stderr or STDERR).
• The stderr output is sent to the terminal
• The number associated with the standard error file descriptor is 2.
• To redirect stderr, use the following syntax:
• $ls /junk 2> /tmp/output.err
• A single 2> will override existing file contents
• Use 2>> to append to the end of a file
• To discard output, redirect output to the /dev/null file (trash can)

Page - 39
Standard Error

• To redirect both stdout and stderr to different files:


○ $ls –la mm > output.txt 2> error
• To redirect both stdout and stderr to the same file, use one of the following:
○ $ls –l /root/.bash_profile mm > output.txt 2>&1
○ $ls &> output

Page - 40
Standard Input
• Standard in (also called stdin or STDIN) normally comes from the keyboard
• Some commands expect input from STDIN rather than a filename argument
• Sometimes you may want to redirect standard input so the input comes from a file instead of
they keyboard
• User interactions with the system are input through standard input (stdin), which is
channel/stream 0, usually by using a keyboard.

Page - 41
• Then, any command executed through an interactive shell connects to a text
terminal on which the shell is running and sends the output through either
standard output (stdout), which is channel/stream 1, if it is OK, or through
standard error (stderr), which is channel/stream 2 if it is not OK.
• The stdout is usually the terminal displayed by the monitor. There are other
channels and streams (3 and up) that any process can use and don't have a
default input or output.

Page - 42
Redirection with >

• command > file: Sends standard output to <file>


• command 2> file: Sends error output to <file>
• command 2>&1: Sends error output to standard output
• command > file 2>&1: Sends standard output and the error output to a file
( 2>&1: This means "redirect stderr (file descriptor 2) to wherever stdout (file descriptor 1) is currently pointing.“
This ensures that stderr is redirected after stdout has already been redirected to the file.)
• command &> file: Sends standard output and the error output to a file
• command 2>&1 > file: Redirects error output (stderr) to the current destination of standard output (stdout), and then
redirects standard output to a file. As a result, only standard output goes to the file, while error output still goes to the
terminal.

Page - 43
Append with >>

• command >> file: Appends standard output to a file


• command 2>> file: Appends error output to a file
• command >> file 2>&1: Appends standard output and error output to a file
• command &>> file: Appends standard output and error output to a file
• command 2>&1 >> file: Sends error output to standard input and appends standard input
to a file

Page - 44
tee command

• The tee command duplicates output to be placed in a file and to the terminal
simultaneously:
the diff between tee and redirection:
● $date | tee timer.txt
in tee :
● Fri Nov 7 02:21:24 UTC 2014 You see the output on the screen AND it is saved in timer.txt.
in redirection :
● $cat timer.txt The date is saved in timer.txt, but you don’t see it on the screen.

● Fri Nov 7 02:21:24 UTC 2014


• The –a option is used to append output to an existing file

Page - 45
Prepare for the Next Lecture!
After solving, take screenshots and be ready for discussion.
1. Write a command to remove all white spaces from a file named input.txt.
2. write a command to replace all occurrences of the word "error" with "warning" in a file called
log.txt.
3. How to print only the first and last columns from a space-separated file named data.txt?
4. Write a command to delete all digits (0-9) from a file called numbers.txt.
5. Write a command to delete all lines that contain the word "debug" from logfile.txt.
6. Using redirection, how would you save both the output and error messages of a script called
run.sh into a file named output.log?

Page - 46
orl

Thank you !

Get ready for Linux process and runlevels next week!

You might also like