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

Linux - Part 4

The document provides an overview of file ownership, permissions, and management in Linux, detailing how files are owned by users and groups, and how to change ownership and permissions using commands like chown, chgrp, and chmod. It also explains file types, compression methods, and archiving techniques, including the use of gzip, bzip2, and tar. Additionally, it covers user account management, including the structure of the /etc/passwd and /etc/shadow files for user and password information.

Uploaded by

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

Linux - Part 4

The document provides an overview of file ownership, permissions, and management in Linux, detailing how files are owned by users and groups, and how to change ownership and permissions using commands like chown, chgrp, and chmod. It also explains file types, compression methods, and archiving techniques, including the use of gzip, bzip2, and tar. Additionally, it covers user account management, including the structure of the /etc/passwd and /etc/shadow files for user and password information.

Uploaded by

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

Ownership

Permissions
Ownership
● In Linux, everything is stored in files.

Files are used to store data such as text, graphics, and programs.

Directories are special files that are used to store other files.
● Every file and folder in Linux is owned by a user and a group account.

By default, users own the files that they create.

By default, primary group of user who creates the file will be the group owner
of tha file.

The administrator can change the ownership of a file to another user or a
group.
● The id command can be used to view user and group IDs.
Ownership
● In the example below, the f1.txt file was created by the user pedram and the
primary group was users.
● As a result, the f1.txt is owned by the pedram account and the users group.

pedram@vm1:~$ id
uid=21666(pedram) gid=100(users) groups=100(users)
pedram@vm1 :~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt
Ownership
● The chgrp and chown commands can be used to change the ownership of files
and directories in linux.
● Syntax:

chgrp [options]... group file...

chown [options]... user:group file...

pedram@vm1:~$ id
uid=21666(pedram) gid=100(users) groups=100(users)
pedram@vm1:~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt
pedram@vm1:~$ sudo chgrp specialgroup f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram specialgroup 0 May 25 12:17 f1.txt
pedram@vm1:~$ sudo chown anotheruser:anothergroup
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 anotheruser anothergroup 0 May 25 12:17 f1.txt
File Types
● The ls with -l option can be used to display the file type and permissions.
pedram@vm1:~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt

● The first character in the output of the ls -l command shows the file type.

– : regular file (hard link)

d : directory

l : symbolic link

c : character device file

b : block device file

s : local socket file

p : named pipe
File Types
● Regular file (-): It represents all different files such us text files, images, binary
files, shared libraries, etc.
● Directory (d): special type of file that represent directories – files that can store
other files.
● Symbolic link (l): a file that points to another file by using the file name.
● Character device file (c) and Block device file (b): these files allow users and
programs to communicate with hardware peripheral devices.
● Local socket file (s): special file that serve as the communication end-points for
processes running on that device.
● Named pipe (p): special files that allow communication between two local
processes.
Permissions
● The next nine characters in the output of the ls -l command are permissions.
pedram@vm1:~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt

● There are three groups of permissions (three permissions in each group)



Group 1 – user owner permissions: rw-r—r--

rw- are the pedram permissions (user owner)

Group 2 – group owner permissions: rw-r--r--

r-- are the users permisions (group owner)

Group 3 – others permissions (accounts that are not the user owner and not
in the group of the owner): rw-r--r--

r-- are the other permissions
Permission Types
● Three permissions are given to each group:
● Read permission

Allows to read the content of a file or to view the content of a directory.
● Write permission

Allows to change the content of a file

Write permission requires the read permission to work.

Allows to create, rename, delete files and subdirectories in a directory.

Write permission requires the execute permission to work.
● Execute permission

Allows to run the file as a script (executable file) or to move (cd) to a directory.
Changing Permissions
● The chmod command is used to modify the permissions.

The user owner of the file or the administrator can modify the permissions.
● Symbolic mode

Using characters to change the permissions.
● Numeric mode

Using octal numbers to change the permissions.
chmod – symbolic method
● User types:

u – user owner, g – group owner, o – others, a – all (u + g + o)

Types can be combined: ugo is the same as a, ug, uo, og, and etc.
● Permissions:

r – read, w – write, x – execute

Permissions can be combined: rwx, rx, rw, and etc.
● Operators:

+ is add new permissions on top of existing permissions

– is to remove permissions from existing permissions

= is to replace existing permissions with new permissions
chmod – symbolic method - syntax
● chmod [option]... mode,[mode]... file...
pedram@vm1:~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt
pedram@vm1:~$ chmod ug+x-w,o=x f1.txt
pedram@vm1:~$ ls -l f1.txt
-r-xr-x--x 1 pedram users 0 May 25 12:17 f1.txt

● In the above example, the chmod command:



Added the executable permission to user and group

Removed the write permission from user and group

Replaced the others permissions with executable permission
chmod – numeric method
● A three digit number can be used to provide permissions.
r w x

The first digit – user owner permissions
0 - - -

The second digit – group permissions
1 - - x

The third digit – others permissions
2 - w -
● Each octal number is in the range from 0 to 7.
3 - w x

Read permission – 4
4 r - -

Write permission – 2
5 r - x

Execute permission – 1
6 r w -

No permissions – 0
7 r w x
chmod – numeric method
● Examples: ● Examples:

777 
644

User – 7 (read, write, execute) 
User – 6 (read, write)

Group – 7 (read, write, execute) 
Group – 4 (read)

Others – 7 (read, write, execute) 
Others – 4 (read)

751 
000

User – 7 (read, write, execute) 
User – 0 (no permissions)

Group – 5 (read, execute) 
Group – 0 (no permissions)

Others – 1 (execute) 
Others – 0 (no permissions)
chmod – symbolic method - syntax
● chmod [option]... octal-mode file...
pedram@vm1:~$ touch f1.txt
pedram@vm1:~$ ls -l f1.txt
-rw-r--r-- 1 pedram users 0 May 25 12:17 f1.txt
pedram@vm1:~$ chmod 551 f1.txt
pedram@vm1:~$ ls -l f1.txt
-r-xr-x--x 1 pedram users 0 May 25 12:17 f1.txt

● In the above example, the chmod command replaced the file permissions with:

User and group – 5 (read, execute)

Others – 1 (execute)
Compression and Creating
Archives
Archiving and Compression
● Compression

Makes files smaller.
● There are two types of compression:

Lossless – no information is removed from the file.

Lossy – some information (metadata) is removed from the file.
● Archiving

Combines multiple files into one.

We can chose to compress an archive or to leave it uncompressed.
Compression – gzip and bzip2
● Linux provides several tools for compressing files.
● Gzip:

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77).

Whenever possible, each file is replaced by one with the extension .gz, while keeping
the same ownership modes, access and modification times.

Gzip will only attempt to compress regular files. In particular, it will ignore symbolic
links.
● Bzip2:

bzip2 compresses files using the Burrows-Wheeler block sorting text compression
algorithm, and Huffman coding.

Compression is generally considerably better than that achieved by more conventional
LZ77/LZ78-based compressors, and approaches the performance of the PPM family of
statistical compressors.
Compression – gzip and bzip2
● Two most common tools are:

gzip (gzip to compress and gunzip uncompress)

bzip2 (bzip2 to compress and bunzip2 to uncompress)
pedram@vm :~$ man ls > ls.txt
pedram@vm :~$ ls -l ls.txt
-rw-r----- 1 pedram users 7934 May 25 16:34 ls.txt
pedram@vm :~$ gzip ls.txt
pedram@vm :~$ ls -l ls.txt*
-rw-r----- 1 pedram users 3078 May 25 16:34 ls.txt.gz
pedram@vm :~$ gunzip ls.txt.gz
pedram@vm :~$ ls -l ls.txt
-rw-r----- 1 pedram users 7934 May 25 16:34 ls.txt
pedram@vm :~$ bzip2 ls.txt
pedram@vm :~$ ls -l ls.txt*
-rw-r----- 1 pedram users 3024 May 25 16:34 ls.txt.bz2
pedram@vm :~$ bunzip2 ls.txt.gz
pedram@vm :~$ ls -l ls.txt
-rw-r----- 1 pedram users 7934 May 25 16:34 ls.txt
Archiving – tar and zip/unzip
● The traditional utility to archive files is called tar: TApe aRchive
● Tar has three modes:

Create: create a new archive out of a series of files

Extract: extract one or more files out of an archive

List: Show the contents of the archive without extracting
● Tar syntax: tar -c [-f ARCHIVE] [OPTIONS] [FILE...]
● NOTE: The tar command will recurse into subdirectories by default
Archiving – tar and zip/unzip
● Most frequently used tar command options:

-f – name of the archive

-c – create a new archive

-x – extract files from an archive

-t – view the content of an archive without extracting

-v – verbose output

-z and -j – compress the archive (z – gzip and j – bzip2)

-C – extract to a specifed directory instead of the current directory
User and Group
Management
User Accounts

The /etc directory contains files which contain account data of users and groups defined on the
system.

The /etc/passwd file defines some account information for user accounts.
 Each line contains information about a single user:

root:x:0:0:root:/root:/bin/bash

Contains: Name, Password Placeholder, User ID, Primary Group ID, Comment, Home Directory,
Shell (fields are separated by a colon)

Use grep or getent commands to check if user is defined on system:
 grep -i ‘root’ /etc/passwd
 getent passwd root
root:
$6$HHJ0w8Vo$qBGAV3LywgVypyiDuxePjrjBr1rkZuFC60oRbX4Rq0:18666:0:99999:
7:::
sername: root - This is the name of the user. In this case, it's the root user.
Encrypted Password: $6$HHJ0w8Vo$qBGAV3LywgVypyiDuxePjrjBr1rkZuFC60oRbX4Rq0
Date of Last Password Change: 18666 .This is the date the password was last changed, expressed in days since the Unix epoch
(January 1, 1970).Given the value 18666, this means the password was changed 18,666 days after 1 January 1970.
Minimum Password Age: 0 - A value of 0 means there's no enforced wait time between password changes. In other words, after
changing their password, the user is immediately allowed to change it again if they wish.The number of days the user must wait
before they can change their password again. A value of 0 means there's no wait time.
Maximum Password Age: 99999 -The number of days after which the user is forced to change their password. A value of 99999
typically means the password never expires.
Warning Period: 7 - The number of days before the password expires during which the user is warned.The number of days before
the password expires during which the user is warned.
Inactivity Period: (empty in your example)-The number of days after the password expires that the account is disabled. If this
field is empty, there's no inactivity period set.
Account Expiry Date: (empty in your example) -The date on which the account will be disabled, expressed in days since the Unix
epoch. If empty, the account does not expire.
Reserved: (empty in your example)This field is reserved for future use.
User Passwords

The etc/shadow file contains user password information (must be logged in as root).
 sudo getent shadow root
 root:$6$HHJ0w8Vo$qBGAV3LywgVypyiDuxePjrjBr1rkZuFC60oRbX4Rq0:18666:0:99999:7:::


Fields include:
 Username: Username of the account (matches username in /etc/passwd)
 Password: Encrypted password for the account
 Last Change: Last time password was changed
 Min: Minimum # of days between password changes
 Max: Max # of days password is valid
 Warn: Number of days before password expiry in the system warns the user
 Inactive: Grace period in which user’s password can be changed
 Expire: Number of days when user accounts will expire (from January 1, 1970)
 Reserved: Currently not used, this field is reserved for future use
System Accounts


Users log in using regular accounts (UID > 1000).


System administrator, root account (UID = 0).


System accounts are used to run services on the system (UID 1-499)

System accounts in /etc/passwd and /etc/shadow have some different field
values:
 Home directory - typically do not have
 Shell: Uses nologin
 Password: Uses *
john:x:1001:1001:John Doe:/home/john:/bin/bash

• Username: john
• Password: x (indicating the actual encrypted password is in /etc/shadow)
• UID: 1001
• GID: 1001
• User Info/Comment: John Doe
• Home Directory: /home/john
• Shell: /bin/bash
Group Accounts

Each user can be a member of one or more groups.

The /etc/passwd file defines the primary group membership for a user.

The /etc/group file defines supplemental (or secondary) group membership.

Fields include:
 Group Name: Field contains the group name
 Password Holder: The x means password is not stored in this file
 GID: Unique group ID associated with group
 User List: Lists members in the group

getent group root
 This command shows the primary group information for the root user

id root or groups root
 These commands show the group membership for the root account
Viewing User Information

The id command is used to print user and group information of the current user:

Output:

uid=1001(pedram ) gid=1001(pedram ) groups=1001(pedram ),4(adm),27(sudo)


 Lists user account information first (UID (uid=1001) and username (nivanov))
 After username, the primary group is listed (group ID and group name)
 Other information includes other groups (secondary) user belongs to (group IDs and group names)

To display information for a specific account, use the username as an argument.
 id root


To print only group IDs, use the -G option.
Viewing Current Users - who

The who command lists users who are currently logged in, as well as where and when they logged in.

Sample Output:

pedram pts/0 2022-03-08 19:26 (97.xxx.76.xx) (:0)



Username: Indicates user who is logged in and has an open session.

Terminal: Indicates which terminal window the user is working in. tty indicates a local login whereas pts
indicates a pseudo terminal. (ssh /telnet)

Date: Indicates when user logged in.

A hostname means user logged in remotely.

A colon and number means a graphical local login.
 No location info means user logged in via local command line.
Viewing Current Users - w

The w command lists users who are currently logged in, as well as where and when they logged in and
what they are doing.

w displays information about the users currently on the machine, and their processes.

The header shows, in this order, the current time, how long the system has been running, how many users
are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

Sample Output:
16:53:33 up 11 days, 14:53, 20 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
pedram pts/0 10.48.44.63 13:51 2:47m 0.02s 0.02s -bash
Viewing Login History - last


The last command reads the /var/log/wtmp file all login records.

Shows previous login sessions as well as current login information.

pedram pts/0 97.xxx.176.xx Tue Mar 8 19:26 still logged in


pedram pts/0 97.xxx.176.xx Tue Mar 8 11:47 - 11:47 (00:00)
Creating Groups

Execute the groupadd command using root user account to create a new group.
 sudo groupadd -g 777 newgroup

Use the -g option to specify a group ID (GID) for the group. If specified GID must not be an
existing GID and the user range > 1000 should be used when assigning GIDs.

If -g option is not used, groupadd will automatically assign a first available group id - one
value higher than the last group id added in /etc/group file.

Check if the group was created:
 getent group newgroup
 Output: newgroup:x:777:
Modifying Groups

The groupmod command can be used to change the name of the group (-n option) or change the GID (-
g option).

Changing the group name won’t cause any problems with accessing files
 sudo groupmod -n newgroup1 newgroup

Changing the GID will cause files that were owned by the group to no longer be associated with that group –
orphaned files.
 sudo groupmod -g 888 newgroup1

To search for orphaned files, use the find command with the -nogroup option: find . -nogroup
Deleting Groups

The groupdel command can be used to delete a group.

Files that belong to the deleted group will become orphaned.

Only supplementary groups can be deleted.

sudo groupdel newgroup1
Creating User

Once you know what values to use, you can create a user by using the useradd command:
 sudo useradd -u 1000 -g students -G sudo,adm -c ‘New Student’ mark
 -g – primary group
 -G – supplementary groups
 -c – comment

Information about new user mark is added to /etc/passwd and /etc/shadow and information about supplementary
groups is added to /etc/group and /etc/gshadow files.

Please refer to the man pages for the list of options that can be used with the useradd command.
Modifying User

The user must not be logged in for the changes to take effect:
 Use who, w, and last commands to verify if user is currently logged in.

Some of usermod options for modifying users:
 -c – comment
 -d – home directory
 -e – expiration date
 -s – shell
 -g – primary group
 -G – supplementary groups
 -a – append user to new group
 -L – lock user password

Please view the man page for the usermod command for a complete list of options
Deleting User

When you delete a user account, you also need to decide whether to delete the user's home
directory.

To delete a user without deleting the user’s home directory execute:
 sudo userdel user1

To delete a user and their home directory execute:
 sudo userdel -r user1
Add Users and Groups – more commands

Linux also has
 adduser
 addgroup

commands for adding user and groups accounts.

Read man pages for the adduser and addgroup command to learn how to use these
commands.
Changing password - passwd

The passwd command changes passwords for user accounts.

A normal user (not root) may only change the password for their own account.

The superuser (root) may change the password for any account.

passwd also changes the account or associated password validity period.


When the passwd command is used, the user is first prompted for their old password, if one is present. This password is then

encrypted and compared against the stored password. The user has only one chance to enter the correct password. The

superuser is permitted to bypass this step so that forgotten passwords may be changed.

Example – changing your own password: passwd

Example – changing user1 account password: sudo passwd user1
The ssh command
The ssh command will allow you to connect to another machine across the network, log in and
then perform tasks on the remote machine:
root@localhost:~# ssh ssh [email protected] The authenticity of host
‘atlas.sheridanc.on.ca’ can’t be established.
RSA key fingerprint is c2:0d:ff:27:4c:f8:69:a9:c6:3e:13:da:2f:47:e4:c9.
Are you sure you want to continue connection (yes/no)? yes
Warning: Permanently added ‘test’ (RSA) to the list of known hosts.
[email protected]’s password:
The ssh command – RSA fingerprint

The ssh command: RSA key fingerprint


If you answer yes at the prompt (asking to verify the machine’s identity), the RSA key fingerprint
of the remote machine will be stored on your local system.
When you attempt to ssh to the same machine in the future, the RSA key fingerprint provided by
the remote machine is compared to the copy stored on the local machine.
If they don't match, you will see an error message.
The ssh – executing commands on remote machine

The ssh command can be used to execute commands on a remote machine.


For example, the following command will run the “ls -l” command on atlas server:
The scp command

SCP is the classic tool for making encrypted copies between two Linux computers on a
network.
SCP stands for “secure copy” – with “secure” referring to the encryption of the data
transfer.
The following command will transfer the local f.txt file to ivanovn home directory on atlas
server.

Verify that the file has been uploaded to the home directory on atlas server:
The sftp command

The sftp is a file transfer program, similar to ftp, which performs all operations over an
encrypted ssh transport.
It may also use many features of ssh, such as public key authentication and compression.
Process Management
What is a Process?
• A process is a set of instructions loaded into memory
• Numeric ID (PID) used to identify a running process
• A process also runs with an associated UID and GID that
determines filesystem access
Program is a process
• A process is a running program such as
• Open Terminal Window (tty)
• Shell (bash)
• Active editor (gedit, vi)
• Active file manager
• Daemons
• System services (email, web, ftp, printer)
Process Architecture
• Each process has a unique PID (process ID)
• Processes have parent-child relationship;
e.g.
• On CentOS, bash is a child of gnome-terminal
• When bash runs cat command, cat is child of
bash
• Child process can terminate when parent dies
• Parent knows PID of all children and vice
versa, making a known process tree
• OS knows the how to map a PID to its
program
Process Tree
.
Process Information Commands
Get process information
• ps – show list of running processes
• pstree – show process hierarchy
• top – monitor processes resources
Get PID information
• pidof – list PIDs of a specific program
• pgrep – list PIDs of programs matching a
pattern
ps
Show Process information
• Usage: ps [options]
• -e (show all processes)
• -a (includes processes on all terminals)
• -f (show process percentage)
• -x (includes processes not attached to
terminals)
• -u (prints process owner information)
$ ps // show info for current process
$ ps –eaf // show info for all processes
$ ps –eax // show info for all processes
How to Use Unix PS Command (0:55)
ps example
pstree
Show process trees
• Usage: pstree [options] program
• -h (show full process tree, current is highlighted)
• -p (show PID)
• -a (show command line arguments)

// show PIDs for full process tree


// show command line arguments
$ pstree –pha

Show Process Tree – pstree (2:31)


pstree –p example
.
top
• Similar to Windows Task Manager
• Displays real-time process information
• %CPU, %MEM, etc
• Can interactively sort information, kill processes,
change nice scheduling priority
• -u <username> (filter by user)
• -n (number of iterations)
• Type q to exit
• Type h for help

Process Management Part 3 ( top ) (6:06)


top example
pidof
List PIDs of processes with the exact program name
• Usage: pidof [options] name
• -s (return just one PID)

$ pidof bash // get PIDs of all bash


processes
$ pidof –s bash // get PID of just the
current bash
pidof example
pgrep
Find PIDs of program names that match a pattern
• Usage: pgrep [options] <pattern>
• -U <user>
• -G <group>
• -l (show program name)
• Similar to ps –a | grep <pattern>

$ pgrep –U labuser sh // show PIDs for labuser


that match sh
$ pgrep –l –U labuser sh // show PIDs with
program names
$ pgrep –G root // show PIDs for group root
pgrep example
Signals
• OS mechanism Inter Process Communication (IPC)
• Signals are sent directly to processes
• Signals are specified by name or number when sent:
• Signal 15, TERM (default) - Terminate cleanly
• Signal 9, KILL - Terminate immediately
• Signal 1, HUP - Re-read configuration files
• Private program-specific signals can be defined too
Process Termination Commands
Terminating a process commands
• kill – kill process by PID
• pkill – kill process by pattern
• killall – kill process by program
kill
Terminate a process by its PID
• Usage: kill [-<signal>] <pid>

$ kill 1001 // kill process with PID=1001


$ kill –15 2002 // kill process 2002
gracefully
$ kill –9 3003 // kill process 3003
immediately

How to kill Process in Linux (0:35)


pkill
Terminate process(es) if program name
matches pattern
• Usage: pkill [-<signal>] <pattern>
e.g. pattern sh would match bash, sh, csh
$ pkill sh // kill all processes with sh in
program name
$ kill –15 sh // gracefully kill all matching
processes
$ kill –9 sh // immediately kill all
matching processes
pkill things when killall isn't enough (1:19)
killall
Terminate process(es) with same exact
program name
• Usage: killall [-<signal>] <pattern>

$ killall sh // kill all processes with sh in


program name
$ killall –15 sh // gracefully kill all matching
processes
$ killall –9 sh // immediately kill all
matching processes

You might also like