Linux - Part 4
Linux - Part 4
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
● 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:
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:
The last command reads the /var/log/wtmp file all login records.
Shows previous login sessions as well as current login information.
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
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)