100% found this document useful (4 votes)
1K views

Linux Essentials Full Course

This document outlines an agenda and objectives for a three-day Linux Essentials training course. Day 1 will cover introductions, storage, and processes. Day 2 will cover users, shells, and shell scripts. Day 3 will cover regular expressions, system services, package management, hardware devices, booting, and a quiz. The course aims to teach fundamental OS concepts like files and processes, shell usage, writing scripts, and basic system administration. The target audience includes those new to Linux as well as those wanting to refresh or expand their knowledge. The course takes a practical hands-on approach with examples.

Uploaded by

Cezar Emanuel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
1K views

Linux Essentials Full Course

This document outlines an agenda and objectives for a three-day Linux Essentials training course. Day 1 will cover introductions, storage, and processes. Day 2 will cover users, shells, and shell scripts. Day 3 will cover regular expressions, system services, package management, hardware devices, booting, and a quiz. The course aims to teach fundamental OS concepts like files and processes, shell usage, writing scripts, and basic system administration. The target audience includes those new to Linux as well as those wanting to refresh or expand their knowledge. The course takes a practical hands-on approach with examples.

Uploaded by

Cezar Emanuel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 210

Linux Essentials

Luxoft Romania Training Center

February 9, 2016
Florin Simion
[email protected]

Agenda

Day 1

2. Storage
3. Processes

Day 2

1. Introduction

4. Users
5. Using the shell
6. Shell scripts

Day 3

February 7, 2016

7. Regular expressions
8. Essential system services
9. Package management
10. Hardware devices
11. Booting the system
Quiz
2

Course objectives

Understand the basic abstractions within an OS

Files: information (passive entity)


Processes: actions (active entity)

Be able to interact with a Linux OS using the shell

Understand commands and parameters


Alter command execution via shell operators
Write simple to medium one-liners
Use regular expressions

Write bash scripts for performing simple tasks

Perform simple package management tasks

Understand system services and the way a Linux system boots

February 7, 2016

Target audience

By level of Linux-related knowledge

If you are new to Linux


this course will help you gain practical skills and understanding of Linux
concepts

If you are already familiar with some Linux aspects


this course will help you refresh your knowledge and integrate it into a
consistent view

By job duties

February 7, 2016

QA Engineers performing manual testing in Unix-like environments


Test Automation Engineers requiring scripting skills
Developers interested in Linux from an administrative point of view

Approach

Most of the concepts are presented using a practical approach

The point of view of a system administrator


Hands-on examples

Some important OS concepts are given a more theoretical approach

February 7, 2016

The point of view of a system developer

Introduction

February 7, 2016

What is an operating system?

A collection of software that

manages hardware resources


act as an abstraction layer for other applications

http://en.wikipedia.org/wiki/Operating_system

February 7, 2016

Unix-like operating systems

Aim to behave like a Unix system

not necessarily based on Unix code


Linux is an example of Unix-like OS

http://en.wikipedia.org/wiki/Unix
February 7, 2016

GNU / Linux

A Linux operating systems contains 2 components:

The Linux Kernel


managed and developed by the Linux Kernel Project

User space tools


mostly managed and developed by the GNU Project
GNU = GNUs Not Unix

www.ibm.com

February 7, 2016

Linux distributions

A distribution packages together:

the Linux kernel


a (large) collection of applications

A distribution usually provides:

installation discs
a repository of packages (usually in binary form)
a package management system
regular updates of the applications and kernel, for including
new features
bug fixes
security patches

February 7, 2016

Distribution families and derivatives

Most distributions are based on a parent distribution

this way, they can be grouped in families

Debian: Ubuntu, Linux Mint


RedHat: Fedora, CentOS
SUSE: openSUSE
Mandriva
Gentoo
Slackware

February 7, 2016

Storage

February 7, 2016

Topics

February 7, 2016

Introduction

February 7, 2016

What is storage about?

Storage: management of persistent information

Persistent information abstracted by:

files fundamental abstraction within an OS


non-volatile media: hard-disks, optical media, memory cards etc.

Management abstracted by actions:

February 7, 2016

retaining information
retrieving information
organizing information
protecting information

Storage Layered approach

software
hardware

February 7, 2016

Block devices

February 7, 2016

Linux devices

Devices are abstracted as files (device files)

software interacts with hardware devices using standard I/O operations

Implementations

block devices: data is transferred in blocks


character devices: data is transferred one character at a time

Device files are found under /dev

Note: not all device files correspond to real, hardware devices


(pseudo)random number generators /dev/urandom

February 7, 2016

data sinks
virtual terminals

/dev/null
/dev/tty0

Block devices

Data is transferred in blocks

block smallest unit of (physically) transferable data


512 bytes in most implementations

Transfer characteristics:

high speed
random access (vs. sequential access in char devices)

Transfer operations:

buffered

read
write
seek / skip

Note: most storage solutions directly rely on block devices

February 7, 2016

exception: network-based storage (NFS, SMB, iSCSI etc.)


8

Partitions

Partition: a contiguous slice of a storage device (usually hard-disk)

treated by the OS as a block device

Goal: a single (physical) disk will be seen as multiple (logical) disks

Use cases:

dual-boot: each OS gets its own partition


data separation:
allocate a separate partition for OS data, user data, temporary data etc.

www.howtogeek.com
February 7, 2016

Partition tables

The list of partitions is kept in a


partition table

located at the beginning of the


hard-disk

Types of partition tables

MBR (master boot record)


the most popular solution
maximum 4 primary partitions
(or 3 primary + 1 extended)
additional partitions can be
created inside the extended
partition

GPT (GUID partition table)


designed to overcome the
limitations of MBR

technet.microsoft.com

February 7, 2016

10

Device names - example

/dev/fd0 first floppy disk


/dev/fd1 second floppy disk

/dev/sda first mass-storage device

/dev/sda1 first primary partition on /dev/sda


/dev/sda2 second primary partition on /dev/sda
/dev/sda3 extended partition on /dev/sda
/dev/sda4 - third primary partition on /dev/sda

/dev/sda5 first extended partition on /dev/sda

/dev/sdb second mass-storage device

/dev/sr0 first optical device


/dev/sr1 second optical device

February 7, 2016

11

Partitioning tools

fdisk (CLI)

cfdisk (text user interface)

GParted, KDE partition


manager (GUI)

www.hal-pc.com

en.wikipedia.org

gparted.sourceforge.net
February 7, 2016

docs.kde.org

12

fdisk

Example: fdisk [-l] /dev/sda

-l just lists the partitions and exits

Operations are executed through some simple menus:


Command action
a
toggle a bootable flag
b
edit bsd disklabel
c
toggle the dos compatibility flag
d
delete a partition
l
list known partition types
m
print this menu
n
add a new partition
o
create a new empty DOS partition table
p
print the partition table
q
quit without saving changes
s
create a new empty Sun disklabel
t
change a partition's system id
u
change display/entry units
v
verify the partition table
w
write table to disk and exit
x
extra functionality (experts only)

February 7, 2016

13

Swap partitions

A dedicated partition for holding process data that does not fit in the physical memory (RAM)

useful for systems with low physical memory


copying of data between RAM and swap is transparently handled by the OS

Formatting an existing partition as swap mkswap

Usage: mkswap device_file


Example: mkswap /dev/sda4

Activating swapping on a swap partition swapon

Usage: swapon device_file


Example: swapon /dev/sda4

Deactivating swapping swapoff

Usage: swapoff [device_file]


device_file is not needed if currently swapping on a single partition

February 7, 2016

14

File systems

February 7, 2016

15

File systems

File system

provides a way of organizing a set of files on a storage medium


aspects:
logical how the files are addressed by users
physical how the files are stored at binary level by the OS

Most file systems are hierarchical

files are contained in folders and sub-folders

codeidol.com
February 7, 2016

16

Types of file systems

We can classify file systems based on where the files reside

File systems with physical support:

files reside on the local machine


data is kept on a physical medium (hard disk, CD-ROM etc.)
persistent

Virtual file systems

files reside on the local machine


data is kept in RAM
volatile
examples: procfs (/proc), sysfs (/sys), devfs (/dev), tmpfs (/tmp)

Network file systems

February 7, 2016

file reside on a remote machine


data is accessed remotely, over a network (NFS, SMB)
17

File systems - some examples

FAT32

File Allocation Table


still the most popular USB sticks, mobile phones, digital cameras etc.

NTFS

New Technology File System


Recommended on Windows NT and >= 2000
available in Linux via an user-space utility (ntfs-3g)

ext2 / ext3 / ext4

the oldest (and most stable) Linux native filesystem


ext3 added journaling support

btrfs B-tree file system

February 7, 2016

copy-on write
advanced features: snapshots, online resizing

18

File systems operations

Creating / formatting

makes the file system usable, exposing it via a system path

Un-mounting

previous data (if any) is discarded

Mounting

initializes the data structures of the file systems (to default values)

the opposite of mounting


the file system can no longer be accessed via a path

Verifying

check the file system for consistency


usually, if errors are found, there is the option to correct them

Tuning

February 7, 2016

altering file system parameters


19

Creating a filesystem - mkfs

Usage: mkfs -t fs_type device_file


Example: mkfs t ext4 /dev/sda3

For each file system type, there is a mkfs.fs_type executable

mkfs t fs_type just calls mkfs.fs_type

# ls /sbin/mkfs*
/sbin/mkfs
/sbin/mkfs.bfs
/sbin/mkfs.cramfs
/sbin/mkfs.ext2
/sbin/mkfs.ext3
/sbin/mkfs.ext4
/sbin/mkfs.minix

February 7, 2016

20

Mounting a file system

For accessing a file system, it must first be mounted

Mounting binding a file system to a mount point

mount point a directory where the root of the file system will be found
the file system will appear to extend downwards from the mount point
Note: the original content of the mount point will no longer be accessible

Mounting methods

manual: using the mount command


automatic: via the /etc/fstab file

etutorials.org
February 7, 2016

21

mount

Usage: mount t fs_type [-o options] device_file mount_point

Example: mount t ext4 /dev/sda3 /mnt/data

Note: the mount point must already exist

mount without any parameters shows the currently mounted filesystems:


# mount
/dev/sda2 on / type ext4 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/sda1 on /boot type ext4 (rw)
/dev/sda3 on /home type ext4 (rw)
/dev/sda5 on /var type ext4 (rw)
/dev/sda7 on /media/storage type ext4 (rw)

February 7, 2016

22

/etc/fstab

Each line of the file describes a mount operation


# <file system> <mount point>

<type>

<options>

<dump>

<pass>

proc

/proc

proc

defaults

/dev/sda1

/boot

ext4

defaults

/dev/sda2

ext4

errors=remount-ro 0

/dev/sda3

/home

ext4

defaults

/dev/sda5

/var

ext4

defaults

/dev/sda7

/media/storage

ext4

defaults

/dev/scd0

/media/cdrom0

udf,iso9660 user,noauto

/dev/fd0

/media/floppy0

auto

rw,user,noauto

The file is usually interpreted during system startup


Re-interpreting the file can be forced using the mount a command

February 7, 2016

23

/etc/fstab

Each line of the file describes a mount operation


# <file system> <mount point>

<type>

<options>

<dump>

<pass>

proc

/proc

proc

defaults

/dev/sda1

/boot

ext4

defaults

/dev/sda2

ext4

errors=remount-ro 0

/dev/sda3

/home

ext4

defaults

/dev/sda5

/var

ext4

defaults

/dev/sda7

/media/storage

ext4

defaults

/dev/scd0

/media/cdrom0

udf,iso9660 user,noauto

/dev/fd0

/media/floppy0

auto

rw,user,noauto

The file is usually interpreted during system startup


Re-interpreting the file can be forced using the mount a command

February 7, 2016

24

Specifying swap in /etc/fstab

For activating swapping at boot-time, the swap partition must be specified in


/etc/fstab:
/dev/sda4 swap swap defaults 0 0

For manually activating the swap partitions from /etc/fstab:

February 7, 2016

swapon -a

25

Un-mounting a file system

Un-mounting un-binding the file system from the mount point

Using the umount command

Usage:
umount device_file

umount mount_point

Examples:
umount /dev/sda3

February 7, 2016

umount /mnt/data

26

Displaying free space - df

df disk free
Usage: df [-h] [-k | -m] [-T]

-h display space in human-readable form


-k / -m display space in KB / MB
-T also display the type of the filesystems

Example:
# df -hT
Filesystem

Type

Size

/dev/sda2

ext4

16G

707M

16G

tmpfs

tmpfs

3.9G

3.9G

0% /lib/init/rw

udev

tmpfs

3.9G

164K

3.9G

1% /dev

tmpfs

tmpfs

3.9G

3.9G

0% /dev/shm

/dev/sda1

ext4

248M

31M

217M

13% /boot

/dev/sda3

ext4

252G

128G

125G

51% /home

/dev/sda5

ext4

126G

2.3G

124G

2% /var

/dev/sda7

ext4

3.2T

353G

2.9T

February 7, 2016

Used Avail Use% Mounted on


5% /

11% /media/storage
27

Displaying used space - du

du disk usage
Usage: du [-h] [--max-depth=N] [directory]

-h display disk space in human readable form


--max-depth=N number of levels of recursion
--max-depth=0 is the same as --summarize

directory where to start


if omitted, it starts in the current directory

Example:
# du -h --summarize /home/
127G

February 7, 2016

/home/

28

Checking for errors - fsck

fsck file system check


Usage: fsck [options] device_file

-n do not attempt to repair, only report errors


-y attempt to repair all detected error
-r interactively repair prompt the user for all errors

Example: fsck y /dev/sda3

Note: for checking a file system for errors, it must first be un-mounted!

otherwise, severe data corruption will occur

dumpe2fs dump file system metadata


Usage: dumpe2fs [options] device_file

February 7, 2016

useful for verifying file system parameters (like last mount time, last check time,
creation time etc.)
29

Tuning file system parameters tune2fs

Usage: tune2fs option value device_file

sets the desired file system option to the specified value

Example:
tune2fs c 10 /dev/sda1
sets the maximum number of mounts before a check is performed
tune2fs C 99 /dev/sda1
modifies the actual number of mounts (to force a check at next reboot)

Note: tune2fs l /dev/sda1 is similar to dumpe2fs /dev/sda1

February 7, 2016

30

Network file systems

Network file systems allow a system to access files stored on a remote machine

the remote machine exports a part of its file system


the local machine mount the remote network share

The most common network file systems are:

NFS (Network File System)


originally developed by Sun (version 1 was proprietary)
now it is an open standard

CIFS (Common Internet File System)


also known as SMB (Server Message Block)
developed by Microsoft
Samba is an open source implementation for Unix-like systems

February 7, 2016

31

NFS and CIFS mounting in /etc/fstab

NFS:

Usage:

remote_server:remote_share

mount_point

nfs

options

0 0

172.31.203.13:/space/usrglobal /usr/global

nfs

defaults

0 0

Example:

CIFS:

Usage:

//remote_server:remote_share/

cifs

options

0 0

/media/eng

cifs

defaults

0 0

Example:

//172.31.203.16/eng/

February 7, 2016

mount_point

32

File system paths

File path a string that uniquely identifies the location of a file or directory within a file system

consists of the file or directory name, preceded by all parent directories, separated by /

Examples:

/var/log/syslog the path of syslog file, found under /var/log/


/etc/apache2/ - the path of apache2 directory, found under /etc
directory paths should be ended with /, to distinguish them from file paths (not
mandatory)

A path can be:

absolute contains all the directories up to the root (as in previous examples)
relative contains only the directories up to the current directory
Example: if the current directory is /var, the relative path of /var/log/syslog is
log/syslog

Note: The current directory can be displayed with pwd

February 7, 2016

33

Special paths

/ - the file system root

only one per machine

. the current directory

appending . to a path does not change it:


/bin/././././. is the same as /bin

.. the parent directory

~ - the home directory of the current user

/var/log/../ is the same as /var/ and /var/../var/

~username the home directory of username

Note: any number of /s can be used as separators:

February 7, 2016

/var///log//////syslog is the same as /var/log/syslog


34

File system hierarchy standard (FHS)

A standard for the structure and content of directories in Linux


Directory

Description

/bin

essential utilities for booting, running and debugging the system

/boot

boot-related files, like the kernel image and bootloader configuration

/dev

devfs - special files used for accessing hardware devices

/etc

configuration files for system and services

/home

contains home directories for non-root users

/media

mount-points for removable media

/mnt

mount-points for other filesystems

/opt

3rd party applications

/proc

procfs

/root

home directory for root

/sbin

essential utilities, only accessible by root

/tmp

temporary files

/usr

user-accessible application (non-critical)

February 7, 2016

35

Partitioning best practices

The number of partitions depend on the role of the machine

for a desktop system, even a single partition is sufficient


for a business-critical server, the partitioning scheme must be carefully designed

It is recommended that partitions to be separated according to:

February 7, 2016

I/O patterns
typical use cases
Partition

Use case / pattern

/boot

only accessed at boot time

/home

many reads

/usr/local

user customizations

/var

frequent writes

/var/log

very frequent writes


36

Files

February 7, 2016

37

File and directory operations

Operations available for both files and directories:


ls list

cp copy
mv move or rename
rm delete (remove)

Operations defined only for directories:


cd change current directory

February 7, 2016

mkdir create directory


rmdir remove directory

38

List - ls

Usage: ls [-a] [-l] [-h] name

-a also list hidden files or directories (that begin with .)


-l long listing format display additional details, not just names
-h human-readable form for sizes
name
directory list its content
file list information about it
# ls -l /etc/fstab
-rw-r--r-- 1 root root 947 Oct 27 19:47 /etc/fstab
# ls -l /boot
total 13236
-rw-r--r-- 1 root
drwxr-xr-x 3 root
-rw-r--r-- 1 root
drwx------ 2 root
-rw-r--r-- 1 root
-rw-r--r-- 1 root

February 7, 2016

root 106193 Feb 25 05:02


root
4096 Feb 27 12:04
root 9324077 Feb 27 12:04
root
16384 Aug 27 2012
root 1667905 Feb 25 05:02
root 2426880 Feb 25 04:59

config-2.6.32-5-amd64
grub
initrd.img-2.6.32-5-amd64
lost+found
System.map-2.6.32-5-amd64
vmlinuz-2.6.32-5-amd64

39

Copy - cp

Usage: cp [options] source destination

cp /path/to/file1 /path/to/file2

copies file1 to file2


if file2 exists, it is overwritten
if file2 does not exist, it is created

cp /path/to/file1 /path/to/dir2/

copies file1 inside dir2/ (to /path/to/dir2/file1)


if dir2/ contains a file named file1, it will be overwritten

cp r /path/to/dir1/ /path/to/dir2/

recursively copies dir1/ inside dir2/ (to /path/to/dir2/dir1/)


if dir2/ already contains a sub-directory named dir1/, the copy will merge
the two directories

February 7, 2016

40

Move / rename - mv

Usage: mv source destination

mv file1 file2 (or mv dir1 dir2)

mv /path/to/file1 /path/to/dir2/

moves file1 inside dir2/ (to /path/to/dir2/file1)


if dir2/ contains a file named file1, it will be overwritten

mv /path/to/file1 /path/to/dir2/file2

renames file1 to file2 (or dir1 to dir2)

moves file1 inside dir2 and renames it to file2 (to /path/to/dir2/file2)


if dir2/ contains a file named file2, it will be overwritten

mv /path/to/dir1/ /path/to/dir2/

moves dir1/ inside dir2/ (to /path/to/dir2/dir1/)


if dir2/ already contains a sub-directory named dir1/, the move will merge the two
directories

February 7, 2016

41

Remove - rm

Usage: rm [-f] file

-f force the deletion (do not ask for confirmation)

Usage: rm r [-f] directory

Deletes file

Recursively deletes directory (including itself )


-f force the deletion (do not ask for confirmation)

Note: rm directory (without r is not valid)

February 7, 2016

42

Directory operations

Change directory - cd
Usage: cd path
path can be absolute or relative

Create directory - mkdir


Usage: mkdir [-p] /path/to/dir
-p create the required directories along the path (if required)

Remove directory - rmdir


Usage: rmdir /path/to/dir
dir must be empty for being able to delete it with rmdir
alternative: rm rf /path/to/dir

February 7, 2016

43

File types

Regular files
Directories

Symbolic link

a directory is actually a file with a directory flag


a special file that contains the name of the file it points to
similar to shortcuts in Windows

Named pipe

a special file used for data transfer between two processes


one process opens the writable side
another process opens the readable side

Device file

February 7, 2016

block device
character device

44

Determining the file type - ls

From the output of ls l

the first flag gives information about the file type:


- regular file (or hard link)
d directory
l - symbolic link
p named pipe
b block device
c char device

# ls -l /etc/
drwxr-xr-x 3 root root

4096 Aug 27

2012 acpi

-rw-r--r-- 1 root root

2981 Aug 27

2012 adduser.conf

lrwxrwxrwx 1 root root

13 Aug 27

2012 motd -> /var/run/motd

# ls -l /dev/
crw------- 1 root root

254,

brw-rw---- 1 root disk

8,

February 7, 2016

0 Jan 14 21:41 rtc0


0 Feb 17 14:10 sda
45

File

Usage: file /path/to/file

can give information about the content of the file, too

# file /dev/sda
/dev/sda: block special
# file /dev/rtc0
/dev/rtc0: character special
# file /boot/
/boot/: directory
# file /boot/vmlinuz-2.6.32-5-amd64
/boot/vmlinuz-2.6.32-5-amd64: Linux kernel x86 boot executable bzImage,
version 2.6.32-5-amd64 (unknown@Debian), RO-rootFS, swap_dev 0x2, Normal
VGA
# file /etc/fstab
/etc/fstab: ASCII English text
# file /usr/local/bin/networking.sh
/usr/local/bin/networking.sh: Bourne-Again shell script text executable
February 7, 2016

46

Stat

Usage: stat /path/to/file

Displays metadata related to file (or directory)

# stat /etc/
File: `/etc/'
Size: 4096
Blocks: 8
IO Block: 4096
Device: 802h/2050d
Inode: 655361
Links: 77
Access: (0755/drwxr-xr-x) Uid: (
0/
root)
Gid: (
Access: 2013-03-03 18:27:20.903396964 +0200
Modify: 2013-02-27 12:04:42.331433296 +0200
Change: 2013-02-27 12:04:42.331433296 +0200

# stat /etc/fstab
File: `/etc/fstab'
Size: 947
Blocks: 8
IO Block: 4096
Device: 802h/2050d
Inode: 655491
Links: 1
Access: (0644/-rw-r--r--) Uid: (
0/
root)
Gid: (
Access: 2013-03-03 15:04:11.959642599 +0200
Modify: 2012-10-27 19:47:08.262392353 +0300
Change: 2012-10-27 19:47:08.297379208 +0300

February 7, 2016

directory
0/

root)

regular file
0/

root)

47

Hard and symbolic links

Methods to have more than one name for a single file

Hard link

another name that points to the same data content


has nothing to do with the original file name

Symbolic link

another name that points to the original file name


if the original file is deleted, the symbolic link becomes broken

msdn.microsoft.com
February 7, 2016

48

Creating links - ln

Creating a hard link


Usage: ln source destination
Example: ln /var/log/syslog syslog2

Creating a symbolic link


Usage: ln s source destination
Example: ln /var/log/syslog syslog3
# ls l
-rw-r--r-- 2 root root 947 Oct 27 19:47 syslog2
lrwxrwxrwx 1 root root

February 7, 2016

10 Mar

3 19:41 syslog3 -> /var/log/syslog

49

File security

February 7, 2016

50

Owners

Every file is owned by a user from the system

Additionally, every file is associated with a group of users

These parameters are displayed by ls l


# ls -l /etc/fstab
-rw-r--r-- 1 root root 947 Oct 27 19:47 /etc/fstab

Changing the user and group of a file is done with the chown command:

Usage:
chown user:group file
chown [-R] user:group directory/
-R makes the operation recursive

February 7, 2016

51

Permissions

Each file has 3 sets of permissions:

group
others

Each set of permissions grants 3 different rights:

user

Read
Write
eXecute

Permissions are displayed with ls l


-rwxr-xr--

February 7, 2016

1 root root

125 Feb 18 12:30 test.sh

52

Permissions representations

The permissions of a file can be interpreted as 3 octal numbers:


literal: rwxr-xr-binary:
octal:

111101100
7

The permissions can also be enumerated, in literal form:

February 7, 2016

literal:

rwxr-xr--

enumeration: u=rwx,g=rx,o=r

53

Changing permissions - chmod

Usage: chmod permissions file

permissions can be specified in


octal mode
enumeration mode

Example:

we have test.sh with 644 (or u=rw,g=r,o=r) permissions

we want to add execution right for all entities (user, group, others):
the new permissions will be 755 (or u=rwx,g=rx,o=rx)

we can run chmod in the following forms:


chmod 755 test.sh

(octal form)

chmod u=rwx,g=rx,o=rx test.sh (enumeration form)

February 7, 2016

chmod u+x,g+x,o+x test.sh

(specify only what changes)

chmod +x test.sh

(apply the same change to all entities)

54

Default permissions umask

Usage: umask [mask]

sets the file-creation mask to mask


if mask is ommitted, displays the current value

File-creation mask:
when creating a new file, its permissions are set to (0666 & ~mask)

when creating a new directory, its permissions are set to (0777 & ~mask)

in many distributions, the file-creation mask is 0022:


files are created with permissions:
644 (0666 & 0022): rw-r--r- directories are created with permissions:
755 (0777 & 0022): rwxr-xr-x

February 7, 2016

55

Processes

February 7, 2016

Topics

February 7, 2016

General concepts

February 7, 2016

What is a process?

Fundamental abstraction within an OS

February 7, 2016

Abstracts a task that is executing

Process vs program attributes and resources

Process

Process identifier (PID)


Image (executable)
State (running, waiting etc.)
Memory space
Open files
Security attributes (owner etc.)

# ps -p 19024 u
USER

PID %CPU %MEM

root 19024

0.0

VSZ RSS TTY

STAT START TIME COMMAND

0.0 10264 828 pts/2 S+

17:04 0:00 ping google.com

Program

File name
Disk space

# ls -al /bin/ping
-rwsr-xr-x 1 root root 34248 Oct 14
February 7, 2016

2010 /bin/ping
5

Linux process tree

Every process has a parent process (except the init process)


A process can have: 0, 1 or more child processes

# pstree -p
init(1)acpid(1447)
apache2(1734)apache2(904)

apache2(1666)
cron(1841)
dhcpd(1849)
getty(2127)
getty(2128)
mysqld_safe(1491)logger(1603)

mysqld(1602){mysqld}(1605)

{mysqld}(1606)
named(27572){named}(27573)

{named}(27574)
ntpd(1467)
rsyslogd(1378)
sshd(1822)sshd(13837)bash(13843)pstree(24134)

sshd(19004)bash(19007)
udevd(479)
February 7, 2016

Process parallelism

How many processes can be executed at the same time?

equal to the number of physical CPUs / cores

Each process gets a time slice for running on the CPU(s)

A process scheduler decides:

what process should run next


how long will the process be allowed to run

Scheduling algorithms: a large number, ranging from:

February 7, 2016

very simple (e.g. FCFS first come, first served)


quite common (e.g. RR round robin)
pretty complex (e.g. CFS completely fair scheduler)

Process states

http://elf.cs.pub.ro/so/wiki/cursuri/curs-03

new process just created


ready process ready for execution
running process actively executing on the CPU
waiting process is waiting for an I/O operation to execute
terminated process has completed execution

February 7, 2016

Listing processes

February 7, 2016

Listing and monitoring processes

Inspecting raw information:


procfs

Non-interactive CLI commands:


ps

pstree
pgrep

Interactive CLI tools


top

every process has an entry under /proc/PID

htop

GUI tools

February 7, 2016

most desktop environments (GNOME, KDE etc.) have their own graphical task
manager
10

procfs

procfs is a virtual file system

only resides in memory


is not stored on the hard disk
accesible via /proc

Every process has an entry under /proc, named with its PID:

# ls /proc/
1
10
1016
1017
1022
1023
1028
1029
12
12341
14
14098
15
16
17
1734
...
...

1012
1018
1024
1030
13
1447
1602
18
...

1013
1019
1025
10403
1378
1459
1603
1822
...

1014
1020
1026
10497
13837
1467
1666
1824
...

1015
1021
1027
11
13843
1491
1693
1825
...

/proc also contains information not-related to processes

February 7, 2016

11

procfs process information


# ls /proc/1849/
...
cmdline

- complete command line

...
cwd

- current working directory for the process

environ

- environment variables of the process

exe

- symbolic link to the executable

fd /

- contains links to the opened files

...
maps

- information about memory maps

...
stat
status

- status information (raw)


- status information (human readable)

...

February 7, 2016

12

ps

Has two types of command-line options, for historical reasons:

UNIX-style options, preceded by a dash:


-e display all processes

-p pidlist only display processes with PIDs from the provided list
-u userlist only display processes owned by users from the provided list
-f full format output (verbose)

BSD-style options, not preceded by a dash:


a display all processes

February 7, 2016

x display both foreground and background processes


U userlist same as -u userlist
u human-readable format similar to f

13

ps - examples

List all processes:


ps ef
# ps -ef
UID
root
root
...

PID
1
2

PPID
0
0

C STIME TTY
0 Jan14 ?
0 Jan14 ?

ps aux

# ps aux | head
USER
PID %CPU %MEM
root
1 0.0 0.0
root
2 0.0 0.0
...

TIME CMD
00:00:18 init [2]
00:00:00 [kthreadd]

VSZ
8356
0

RSS TTY
732 ?
0 ?

STAT START
Ss
Jan14
S
Jan14

TIME COMMAND
0:18 init [2]
0:00 [kthreadd]

List all processes belonging to user root:


ps u root f
ps U root ux

The most popular use case:


ps ef | grep name

February 7, 2016

ps aux | grep name


14

pstree

Lists processes in a hierarchical format

Usage: pstree [-a] [-p] [pid]

-a also display command-line arguments


-p also display PIDs
pid display sub-tree starting from PID process

Example: show sub-tree starting from PID 27572:


# pstree -a -p 27572
named,27572 -u bind
{named},27573
{named},27574
{named},27575
{named},27576

February 7, 2016

15

pgrep

Process grep

list processes that match a certain pattern

Usage: pgrep [options] [pattern]

pattern what to match in process names


commonly used options:
-f also match in the full command line, not only in the process name
-l also list the process names, not only the PIDs
-v negates the matching
-c only display a count of matching processes

Example:
# pgrep -f -l apache2
1734 /usr/sbin/apache2 -k start
4546 /usr/sbin/apache2 -k start
6938 /usr/sbin/apache2 -k start

February 7, 2016

16

top

interactively displays the processes in the system

default refresh interval: 3 seconds

the interface has 4 distinct area:

summary area: general information about the system (load, uptime, number of
processes etc.)

prompt line: below the summary area, used to write specific commands
columns header: displays the table header and highlights the sort key
task area: displays information about all processes

htop a more eye candy alternative to top

February 7, 2016

17

top - example
top - 13:48:32 up 12 days, 16:06, 1 user, load average: 0.04, 0.10, 0.04
Tasks: 119 total,
1 running, 118 sleeping,
0 stopped,
0 zombie
Cpu(s): 1.3%us, 0.6%sy, 0.9%ni, 96.5%id, 0.7%wa, 0.0%hi, 0.1%si, 0.0%st
Mem:
8066492k total, 8015136k used,
51356k free,
25492k buffers
Swap:
0k total,
0k used,
0k free, 7530592k cached
PID
8234
7864
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
February 7, 2016

USER
carpalex
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root
root

PR
20
20
20
20
RT
20
RT
RT
20
RT
20
20
20
20
20
20
20
20

NI VIRT RES SHR S %CPU %MEM


TIME+ COMMAND
0 106m 16m 5676 S
6 0.2 116:10.24 rtorrent
0 19068 1248 904 R
4 0.0
0:00.02 top
0 8356 728 596 S
0 0.0
0:19.20 init
0
0
0
0 S
0 0.0
0:00.00 kthreadd
0
0
0
0 S
0 0.0
0:04.82 migration/0
0
0
0
0 S
0 0.0
0:02.24 ksoftirqd/0
0
0
0
0 S
0 0.0
0:00.00 watchdog/0
0
0
0
0 S
0 0.0
0:03.91 migration/1
0
0
0
0 S
0 0.0
0:02.22 ksoftirqd/1
0
0
0
0 S
0 0.0
0:00.00 watchdog/1
0
0
0
0 S
0 0.0
0:05.97 events/0
0
0
0
0 S
0 0.0
0:34.98 events/1
0
0
0
0 S
0 0.0
0:00.00 cpuset
0
0
0
0 S
0 0.0
0:00.00 khelper
0
0
0
0 S
0 0.0
0:00.00 netns
0
0
0
0 S
0 0.0
0:00.00 async/mgr
0
0
0
0 S
0 0.0
0:00.00 pm
0
0
0
0 S
0 0.0
0:01.27 sync_supers
18

Signals

February 7, 2016

19

Killing a process

Processes can be killed (forcefully terminated) by sending them a signal.

The command is kill, followed by the PID of the process, for example:

kill 1234

The signal that kill uses by default is SIGTERM, and has the number 15

The following commands are synonymous with kill 1234:


kill s TERM 1234
kill SIGTERM 1234
kill -15 1234

The SIGTERM signal can be ignored / blocked by a process.

A more powerful signal is SIGKILL (9). This signal cannot be ignored / blocked.

February 7, 2016

kill s KILL 1234


kill SIGKILL 1234
kill -9 1234

20

Killing a set of processes

killall kills all processes with the specified name

Usage: killall [-s signal] name


Examples:
killall apache2
killall -9 apache2
killall s KILL apache2

pkill works like pgrep, but instead of displaying processes, it kills them

February 7, 2016

example:
pkill -9 u root ping

21

Signals

Signals are asynchronous notification mechanisms

can occur at anytime during the execution of a process


can be sent by:
the kernel (e.g. SIGSEGV segmentation fault)
a user (e.g. SIGSTOP suspends the current process)

There are 64 signals


kill l lists all the signals

see man signal for more details

Signals can be manually triggered using the kill commands:

February 7, 2016

kill, killall, pkill


this applies to all signals, not only to those used for terminating processes

22

Signals most used

SIGHUP (1) signal hang up: used for restarting a process

SIGTERM (15) signal termination: sent by the kernel to terminate a process

Some signals can be sent using keyboard shortcuts:

SIGINT (2) signal interrupt: used by the user to terminate a process


SIGQUIT (3) signal quit: used by the user to terminate a process
SIGKILL (9) signal kill: used by the kernel to immediately terminate a process
SIGSEGV (11) segmentation fault: sent by the kernel when a process is trying to access an
invalid memory area
SIGSTOP (19) signal stop: sent by the user to suspend the current process
SIGCONT (18) signal continue: sent by the user to continue the execution of a suspended
process

February 7, 2016

CTRL+Z SIGSTOP
CTRL+C SIGINT
CTRL+\ - SIGQUIT

23

Foreground and
background

February 7, 2016

24

Foreground and background processes

A foreground process is attached to the current terminal

can read from standard input


can write to standard output
blocks the current terminal until the process finishes its execution
by default, when a user enters a command in a terminal, a foreground process is
created

A background process detaches from the current terminal

February 7, 2016

the terminal can be used for entering commands, launching other processes etc.
also called a job

25

Running a process in background

To run a process in background, append & after the command line:

example:

# md5sum big-file.bin &


[1] 27986
#

After the process is launched:

a line with the job number (in brackets) and the PID is displayed
the command prompt appears
the process runs in the background

When the process finished its execution:

a line with the job number and Done is displayed

e07a107c8e6019aa4ce82d68370b7527
[1]+

Done

big-file.bin

md5sum big-file.bin

Note: the output of the process is still displayed at the current terminal

Note: job numbers are only relevant to the current terminal

February 7, 2016

26

Suspending a foreground process

While running a foreground process, entering CTRL+Z has the following effects:

The process is suspended (stops its execution, but does not terminate)
Is forced into background (becomes a job)

# ping google.com
PING google.com (173.194.44.33) 56(84) bytes of data.
64 bytes from 173.194.44.33: icmp_req=1 ttl=56 time=24.4 ms
64 bytes from 173.194.44.33: icmp_req=2 ttl=56 time=23.5 ms
^Z
[1]+

Stopped

ping google.com

Afterwards, the job can be:


restored in the foreground (using fg)

February 7, 2016

restored, but kept in the background (using bg)


killed, using the PID (found using jobs)

27

Job control: jobs, bg and fg

jobs list all jobs from the current terminal

jobs l also displays the PIDs


jobs p only displays the PIDs

# jobs -l
[1]+

1238 Stopped

ping google.com

[2]-

1976 Running

md5sum big-file.bin &

fg [job_number]

if job_number is omitted, the action is taken on the last created job

bg [job_number]

resumes and brings to background the job identified by job_number

resumes and keeps in background the job identified by job_number


if job_number is omitted, the action is taken on the last created job

Note: job numbers are only relevant to the current terminal

February 7, 2016

28

Daemons

Daemons are processes that:

run in background
do not have an associated terminal
Note: background jobs do have an associated terminal, but are detached

Properties

a daemon cannot communicate with the user via standard input and standard
output
the user usually controls a daemon via configuration files and startup / shutdown
scripts
a daemon usually writes messages to log files

Most daemons have names that end with a d:


sshd, inetd, ntpd, udevd, named, dhcpd, mysqld etc.

February 7, 2016

but there are exceptions: apache2, cron etc.


29

Background jobs and daemons

When a terminal is closed, it sends a SIGHUP signal to all its child processes, forcing them to
terminate:

foreground process (if any)


background jobs

By launching a background job with the nohup command, it will ignore any SIGHUP signal

it will continue to run even after the terminal is closed


it will behave similarly to a daemon

# nohup ping google.com &


[1] 7032
# nohup: ignoring input and appending output to `nohup.out'

Notes:

February 7, 2016

the job cannot be brought back into foreground


its output will be written to a file (nohup.out)
the user will not be able to communicate with the job via standard input

30

Priorities

February 7, 2016

31

Process priorities

In Linux, the priority is called niceness

maximum priority: niceness = -20


minimum priority: niceness = 19

The nicer a process is, the less likely to be chosen by the scheduler

By default, a process starts with niceness 0

the OS can change the niceness automatically


the user can change the niceness manually
only the superuser (root) can assign negative niceness values

Changing the niceness (also called renice):

interactively, via top


via the nice command

There are few situations is which an user really needs to renice a process

February 7, 2016

32

User management

February 7, 2016

What is a user?

Non-technically

Technically

An entity which can execute actions within an operating system


The attributes of the entity are comprised in an user account

Most importantly, an user can:

A person who uses a computer

Run processes
Own files

Other actions for an user:

February 7, 2016

Login / logout
Change password
Switch to a different user

Users in Linux

In Linux, each user account is identified by a number

UID (User ID)


UID ranges from 0 to 32768 / 65535 (depends by distribution)

UID 0 is reserved for the superuser (root)

Has full rights


Can run / kill any process
Can create / modify / delete any file
Can create / modify / delete any user account

The mappings between UIDs and usernames are stored in a file

February 7, 2016

/etc/passwd

/etc/passwd

Every line contains information about a single user

Fields are separated by :

Fields:

Username
Password is shadowed?
UID
GID
User alias
Home directory
Default shell

# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
sqa:x:1000:1000:sqa,,,:/home/sqa:/bin/bash

February 7, 2016

Passwords

In modern distributions, passwords are stored encrypted, in a separate file:

/etc/shadow

# cat /etc/shadow
root:$6$kIwQNw.e$2KdkqHiORpAvLqX.ggLLGOFgR6vddce7neUOQOnNWpl/66y/NMnIg1ZCgCyfDpi
YXYlLAwyjqQPvHVwVHdkgC/:15098:0:99999:7:::
sqa:$6$/x9znDgL$10DetEzMVQbLLTOp5w2AtleTf2OJt5BmVUX/3SF2OLPWoXNJNZD3jEFHIWf2OsQV
iSE8Um5WVgqoA.vDydbje0:15098:0:99999:7:::

Passwords can be assigned / changed using the passwd command:

passwd [username]
If username is ommitted, the command takes effect for the current user

February 7, 2016

Each user may change its own password


Only root can change passwords for other accounts

Creating a user

Manually, by editing /etc/passwd

Create the home directory


Set password using passwd

Manually, using useradd:

Create the appropriate line in /etc/passwd

useradd d HOME_DIR -m s SHELL g GROUP USERNAME


Set password using passwd

Using adduser

February 7, 2016

Adduser USERNAME
Interactive script which prompts for most account parameters
Also prompt for password
Exists in Debian-based distributions

Modifying user accounts

Usermod

The syntax is very similar to useradd


example:

# cat /etc/passwd
...
sqa:x:1000:1000:sqa,,,:/home/sqa:/bin/bash
# usermod d /localhome/sqa sqa
# cat /etc/passwd
...
sqa:x:1000:1000:sqa,,,:/localhome/sqa:/bin/bash

February 7, 2016

Deleting user accounts

Manually delete the entry from /etc/passwd

Userdel username

Deluser username

February 7, 2016

Only on Debian-based distributions

Groups

A group consists of a collection of users

A group can contain 1 or more users


A user can belong to 1 or more groups

In certain situations, policies can be applied per group

Groups facilitate applying a policy to a large number of users


Examples:
File ownership
File access permissions

In Linux, groups are identified by a number:

GID (group ID)

The mappings between GIDs and group names are stored in a file

February 7, 2016

/etc/group
9

/etc/group

Fields:

Group name
GID
List of users belonging to the group

# cat /etc/group
root:x:0:
...
cdrom:x:24:sqa
floppy:x:25:sqa, sqa2
...

Notes:

The list of users belonging to the group is useful only when an user belongs to more
than one group

Otherwise, it is enough to assign the group membership in /etc/passwd

February 7, 2016

10

Operations with groups

Creating a group

Groupadd
Addgroup

Modifying a group

Via /etc/group

Via /etc/group
Groupmod

Deleting a group

February 7, 2016

Via /etc/group
Groupdel
Delgroup

11

Switching users

An user can continue its session as a different user without logging in and logging out

Syntax: su [ - ] [ username ]

If username is ommited, root is assumed


If is present, the environment of the target user replaces the current
environment
The working directory is changed to the home directory of the target user
Any initialization scripts are re-executed
It simulates a clean login

Root can su to any user account, without knowing the password

February 7, 2016

12

Running a command with full privileges

It is possible to run a single command with root privileges:

sudo command

The users who have the right to use sudo must be configured by root, in /etc/sudoers

In Ubuntu-based distributions, members of the admin group have the right to use
sudo:

# Members of the admin group may gain root privileges


%admin ALL=(ALL) ALL

It is also possible to limit the usage of sudo to a set of commands.

February 7, 2016

13

Centralized authentication

/etc/passwd is also known as the local user database

User accounts are only relevant to the local system


If a user has to exist on more than one system, it has to be created on every system
Any modification to the account must be operated on every system

A scalable solution: centralized authentication

User accounts are stored on a remote server

The authentication server can also push user attributes to the local system

Credentials are captured on the local system and sent to the authentication server for
validation

Implementations:

February 7, 2016

NIS (network information service) a distributed /etc/passwd database


LDAP (lightweight directory access protocol) a more complex implementations, also
compatible with Windows Active Directory

14

Using the shell

February 7, 2016

What is a shell?

An application (or suite) that provides an interface between:

users
resources in an OS

Shells can be:

graphical (GUI):
GNOME Shell, KDE, Unity etc.
Windows Explorer (explorer.exe)

text-based (command line interface):


sh, bash, dash etc.
Windows PowerShell
DOS Shell

Every shell is basically a command interpreter

February 7, 2016

GUI shells interpret mouse gestures and keystrokes


CLI shells interpret text commands and their parameters
2

Text shell vs CLI

A (text) shell is a particular type of CLI

Not every CLI is a shell

examples:
GRUB console
CLI running on the console of an embedded device
MATLAB console
text consoles in games (Quake, Half-Life etc.)
Python, TCL console

February 7, 2016

Accessing a shell

In Linux, an user can interact with a shell using:

a physical terminal
only found in museums nowadays

a virtual terminal
CTRL+ALT+F1, CTRL+ALT+F2,

a terminal emulator
gnome-terminal, konsole, xterm etc.

a remote connection
over a serial line (using a modem) back to the museum
over TCP/IP: SSH, Telnet

February 7, 2016

Terminal vs shell

A terminal is a device (physical or virtual), which:

captures user input and directs it to a process


captures process output and presents it to the user
passive entity

A shell runs inside a terminal:

users type commands


the shell presents the output of the commands
active entity

http://en.wikipedia.org/wiki/Computer_terminal

February 7, 2016

The shell prompt

The prompt is a text displayed by the shell at the beginning of every line

The cursor is placed immediately after

It has an orientation role. In most distributions, it displays:

the current user


the hostname
the current path
the last character is:
# - for the root user
$ - for a non-root user

Examples (from a Debian distribution):

cristi@Router:/usr/bin $
Router /usr/bin #

The prompt is highly customizable!

February 7, 2016

Shell commands and arguments

Syntax of a command

command argument1 argument2

Arguments can be (informally) classified into:

parameters representing objects that the command operates upon


options modifies the behavior of the command
short options beginning with -
long options beginning with --

Examples (forcefully deleting the file test.txt):

command rm, short option -f, parameter test.txt


rm f test.txt

command rm, long option --force, parameter test.txt


rm --force test.txt

February 7, 2016

Not all commands follow this recipe!

Internal and external commands

Internal commands

implemented in the shell executable (e.g. /bin/bash)


interpreted and executed by the shell itself
examples: cd, pwd, bg, fg, set, wait, echo etc.

External commands

also called shell builtins

not implemented in the shell


executed by calling an external executable
examples: ping, ifconfig, cat, mkdir etc.

Finding out if a command is internal or external: type command


# type mkdir
mkdir is /bin/mkdir
# type pwd
pwd is a shell builtin

February 7, 2016

Shell facilities

Command completion

by default, only completes command-names and paths


can be augmented by bash-completion

Command history

triggered by pressing TAB

navigation though previous commands: ARROW-UP, ARROW-DOWN


reverse-search: CTRL+R
repeat last argument of last command: ALT+.
rerun last command: !!

Screen control:

February 7, 2016

Clear screen: CTRL+L


Lock input / output: CTRL+S
Unlock input / output: CTRL+Q
9

Variables

Defining a variable

name=value

Referencing a variable

$name
also known as expansion

Mostly used in shell scripts

Environment variables

February 7, 2016

defined by the shell


used to control key aspects within the shell
use env to display all environment variables

10

Return values

After execution, every process returns a numeric value

stored in the $? variable

# ping -c 1 google.com
PING google.com (173.194.39.160) 56(84) bytes of data.
64 bytes from bud02s04-in-f0.1e100.net (173.194.39.160): icmp_req=1 ttl=57
time=20.9 ms
--- google.com ping statistics --1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 20.969/20.969/20.969/0.000 ms
# echo $?
0

The command is considered to be:

February 7, 2016

successful return value is 0


unsuccessful return value is non-zero

11

Bash operators

Command chaining

Sequence: ;
Parallel execution: &
Conditional execution: &&, ||

Pipelines: |, |&

Input / output redirection

February 7, 2016

Redirecting input: <, <<, <<<


Redirecting output: >, >>, 2>, 2>>, &>, &>>

12

Command chaining

Sequence:

command1 ; command 2 ; command3 ;


command1 is executed first
command2 is executed after command1 finishes

Conditional execution: AND

command1 && command2


command2 is executed only if command1 is successful

Conditional execution: OR

command1 || command2
command2 is executed only if command1 is unsuccessful

February 7, 2016

13

Pipelines

command1 | command2

output of command1 (stdout) is redirected to input of command2 (stdin)


example:
ls | sort

can be used multiple time, for creating a more complex command (one-liner)

command1 |& command2

February 7, 2016

error output of command1 (stderr) is also redirected to input of command2 (stdin)

14

Redirecting input

Used for commands that read their input from the terminal (stdin)

command < file

input is read from file, instead of standard input


has the same effect as: cat file | command
what are the differences?

Example:

February 7, 2016

sort < list.txt

15

Redirecting input here documents

Used when the input of a command should be taken directly from the terminal, rather
that from a file

command <<DELIMITER
first line of text
second line of text

DELIMITER

the text that will be redirected to the command input is between the two
DELIMITER marks

DELIMITER can be any word, but must not exist in the text itself

# sort <<STOP
> bob
> alice
> trudy
> STOP
alice
bob
trudy
February 7, 2016

16

Redirecting input here strings

Used when a single string should be redirected to the input of the command

command <<< STRING

has the same effect as: echo STRING | command


what are the differences?

Example:
# wc <<< "ana are mere"
1

February 7, 2016

3 13

17

Redirecting output - stdout

Used for commands that write output to the terminal (stdout / stderr)

command > file

standard output of command is written into file


if file
does not exist it is created
already exists it is truncated

command >> file

same as above, but


if file already exists, output is appended

Note: standard error (stderr) is still written to the terminal

February 7, 2016

18

Redirecting output - stderr

command 2> file

standard error of command is written into file


if file
does not exist it is created
already exists it is truncated

command 2>> file

same as above, but


if file already exists, output is appended

Note: standard output (stdout) is still written to the terminal

February 7, 2016

19

Redirecting output stdout and stderr

Both stdout and stderr can be redirected for the same command:

command > out.txt 2> err.txt

stdout and stderr can be redirected in the same file

command > outerr.txt 2> outerr.txt


command &> outerr.txt
works in bash, but not in more basic shells (e.g. sh)

February 7, 2016

20

Suppressing output

If stdout and / or stderr should be suppressed, redirect it to /dev/null

command > /dev/null

stderr is written to the terminal

command 2> /dev/null

stdout is suppressed

stderr is suppressed
stdout is written to the terminal

command > /dev/null 2> /dev/null


command &> /dev/null

February 7, 2016

both stdout and stderr are suppressed

21

Multicasting input - tee

tee file1 file2 file3

the input (stdin) of tee is written to


the terminal (stdout)
file1
file2
file3

Mostly used as:

command | tee file1 file2 file3

If output to the terminal is not desired:

February 7, 2016

command | tee file1 file2 file3 > /dev/null


command | tee file1 file2 > file3

22

Redirecting output to command arguments

command1 | xargs command2

the output (stdout) of command1 is used as the arguments list for command2

Example:

find / -name *.txt | xargs rm


finds all files with *.txt extension and deletes them

February 7, 2016

23

Bash substitutions

Substitution evaluating an expression and replacing it with the result

Bash substitutions:

variable substitution
$variable or ${variable}

command substitution
$(command) or `command`

arithmetic expansion
$((expression))

brace expansion
{expression}

February 7, 2016

24

Variable substitution

$variable

variable is the name of the variable


substitutes with the value of the variable

# a=2
# echo $a
2

For avoiding ambiguity, braces must be used: ${variable}


# a=2
# aa=5
# echo $aaa
# echo ${a}aa
2aa
# echo ${aa}a
5a

February 7, 2016

25

Command substitution

$(command) or `command`

substitutes with the output of the command

# echo "The current date and time is $(date)"


The current date and time is Mon Feb

4 15:31:45 EET 2013

$(command) is preferred over `command` for its greater legibility

February 7, 2016

26

Arithmetic expansion

$((expression))

expression is an arithmetic formula


it can contain variables
limited to integer calculations

Examples:

computing a sum
# echo $((1+2))
3

incrementing a variable
# a=10
# a=$(($a+1))
# echo $a
11

February 7, 2016

27

Brace expansion

Enumeration:

{elem1, elem2, elem3, }


expands to a list of the enumerated elements (separated by spaces)
# echo {a,b,c}
a b c

Numeric range:

{min..max}
expands to a list with all integers between min and max
# echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
an increment can be optionally specified: {min..max..incr}

ASCII range:

{char1..char2}
expands to a list with all ASCII characters between char1 and char2
# echo {X..d}
X Y Z [

] ^ _ ` a b c d

an increment can be optionally specified: {char1..char2..incr}


February 7, 2016

28

Brace expansion prefix and suffix

If a prefix is concatenated to a brace expansion, the prefix will be concatenated with


every element of the list:
# echo host{1..5}
host1 host2 host3 host4 host5

The same goes for a suffix concatenation:


# echo {1..5}-linux
1-linux 2-linux 3-linux 4-linux 5-linux

The two situations can be combined:


# echo host{1..5}-linux
host1-linux host2-linux host3-linux host4-linux host5-linux

February 7, 2016

29

Brace expansion concatenation and nesting

By concatenating two brace expansions, a Cartesian product will result:


# echo {host,user}{1..3}
host1 host2 host3 user1 user2 user3

Concatenation can be done multiple times:


# echo {host,user}_{1..3}-{linux,windows}
host_1-linux host_1-windows host_2-linux host_2-windows host_3-linux
host_3-windows user_1-linux user_1-windows user_2-linux user_2-windows
user_3-linux user_3-windows

Brace expansions can also be nested, resulting an union:


# echo host_{{1..5},{a,b}}
host_1 host_2 host_3 host_4 host_5 host_a host_b

February 7, 2016

30

Text filters

Colloquial name for commands that process an input text, resulting a modified output
text

Typically used in pipelines, for applying multiple transformations to an input text

Do one thing, do one thing well

February 7, 2016

31

cat, tac, nl

cat [-n] file1 file2

if only one file parameter in present, it just displays that file


-n also displays line numbers
it is the first command in many one-liners

tac [-n] file1 file2

concatenates multiple files

inverted cat
output the lines in reverse order

nl file1 file2

February 7, 2016

like cat n, but does not consider empty lines

32

sort, uniq

sort [-u] [-r] [-n] [file]

sorts the lines read from standard input of from file (if present)
-u unique (suppresses duplicates)
-r reverse sort
-n numeric sort (default is alpha-numeric)
also can perform advanced sorts (by fields, by multiple keys etc.)

uniq

February 7, 2016

behaves like sort u

33

head, tail

head [-n N] [file]

displays only the first N lines from standard output or file (if present)
if N is omitted, 10 is used

tail [-n N] [file]

February 7, 2016

displays only the last N line from standard output or file (if present)
if N is omitted, 10 is used

34

cut

Select columns from an input text


cut d delimiters f fields

delimiters a list of characters to be treated like column delimiters


fields what column numbers to select

Example: displaying only the username and home directory from /etc/passwd

use : as delimiter
select columns 1 and 6

# cat /etc/passwd | cut -d ":" -f 1,6


root:/root
daemon:/usr/sbin
bin:/bin
sys:/dev
sync:/bin
sqa:/home/sqa
...
February 7, 2016

35

tr

Transliterate

used for applying character-level transformations on the input text

Character replace:

tr char1 char2
replaces all occurrences of char1 with char2
# echo "root:/root" | tr ":" " "
root /root

Squeeze (remove repeating characters)

tr s char
useful for processing white-spaces

Character delete

February 7, 2016

tr d char
36

wc

Word count

can count lines, words and characters

wc l

counts the number of lines

# cat /etc/passwd | wc -l
28

counts the number of words

wc c

wc w

counts the number of characters

When used without any options, it displays all the numbers above

February 7, 2016

37

Getting help

Built-in help

Most command display a quick usage guide by running them with


-h
--help

Manual pages

Info pages

man command

info command
sometimes they are more detailed that manual pages

Help (for shell builtins)

February 7, 2016

help command

38

Shell scripts

February 7, 2016

Shell scripts

A text file containing shell commands

The file is interpreted by the shell, executing the commands one by one
Used for automating complex tasks
More complex than an one-liner

The most simple bash script:


# cat hello.sh
echo "Hello, World!

Running the bash script:


# bash hello.sh
Hello, World!

February 7, 2016

If the script is written for a different shell than bash, use the appropriate interpreter

Running the script as an executable

The path to the interpreter must be present on the first line of the script, preceded by
#!:
# cat hello.sh
#!/bin/bash
echo "Hello, World!

The script must have execution rights:

chmod +x hello.sh

Run the script just by specifying its name (like when running a command)

./hello.sh
Hello, World!

./ is necessary because the full path to the script is required

February 7, 2016

Return value

By default, a script returns 0

For returning a different value, use the exit command:


# cat hello.sh
#!/bin/bash
echo "Hello, World!
exit 1
# ./hello.sh
Hello, World!
# echo $?
1

February 7, 2016

Comments

In bash, comments start with #

# can occur anywhere within a line

a line beginning with # is entirely ignored


if # occurs anywhere else, the text following it is ignored

# cat hello.sh
#!/bin/bash
# This is my first script
echo "Hello, World! # This line prints a message
# Nothing more to do

February 7, 2016

Conditionals - if
if condition1;
then
commands
[ elif condition2;
then
commands ]
[ else
commands ]
fi

elif and else are not mandatory

condition a command, evaluated by its return value

elif can be replaced by an else containing an if

February 7, 2016

0 condition is fulfilled (true)


non-zero condition is not fulfilled (false)

Test conditions

Used for assessing a logical expression


Syntax

[ expression ]

Operands contained in expressions can be:

test expression

files
strings
numbers

Examples:

test if a file exists:


[ -e /root/file.txt ]

test if the value of variable a is less than 10:


[ $a lt 10 ]

test if two strings are equal:


[ $str1 = $str2 ]

February 7, 2016

The for loop

Executes commands for every member in a list

similar to for each in many programming languages

for member in list;


do
commands
done

Example:

sum of first 10 natural numbers

sum=0
for i in 1 2 3 4 5 6 7 8 9 10
do
sum=$(($sum+$i))
done
echo $sum
February 7, 2016

The while loop


while condition;
do
commands
done

Example:

sum of first 10 natural numbers

i=1
sum=0
while [ $i le 10 ];
do
sum=$(($sum+Si))
i=$(($i+1))
done
echo $sum

February 7, 2016

Script parameters

Like every command, a script can be executed with parameters:

./test.sh param1 param2 param3

The parameters can be accessed from within the script, via special variables:

February 7, 2016

$0 the name of the script itself


$1 the value of the first parameter
$2 the value of the second parameter

$@ - a list containing all parameters (excluding $0)


$# - the number of parameters (excluding $0)

10

Functions

Defining a function:
function name()
{
commands
}

Calling a function:

the function keyword can be omitted

name;

Function parameters:

February 7, 2016

can be accessed using $1, $2 etc. (just like accessing script parameters)
are not declared in the function header (bash functions do not have formal
parameters)

11

Regular
expressions

February 7, 2016

Regular expressions

Regular expressions

Strings that use a special syntax for performing pattern-matching on an input text

Syntax:

[] used to define a set of characters to match:


[abcd] enumeration
[a-d] range
[^abcd] negated match

. match any character


^ - match at the beginning of line
$ - match at the end of line
? match the previous character / set of characters at most once
* - match the previous character / set of characters at least 0 times
+ - match the previous character / set of characters at least once

By concatenating the above elements, complex regular expressions can be generated

February 7, 2016

Using regular expressions

Regular expressions are used in many commands for performing various tasks, based
on pattern matching

grep

sed

select lines of text that match a regular expression

perform search and replace tasks


an advanced version of tr

awk

February 7, 2016

select fields based on pattern matching


an advanced version of cut

grep

Basic usage:

grep regular_expression
reads lines from standard input
writes to standard output only lines matching regular_expression

Typically used to filter the output of shell commands:

command | grep regular_expression

Reading from a file:

grep regular_expression file


preferred over cat file | grep regular_expression

February 7, 2016

grep options

grep i

grep v

display line numbers

grep w

inverted search (negates the regular expression)

grep n

ignore case (case insensitive search)

match whole words

grep r

February 7, 2016

recursive search
grep r regular_expression starting_dir
5

sed

Stream editor

can perform very complex transformations on input streams


mostly used for string substitution (search and replace)

Syntax

sed /pattern/s/to_be_replaced/to_replace/options
pattern
optional
if present, only apply transformation to lines matching pattern
s the transformation to apply (in this case substitute)
to_be_replaced a pattern to search for
to_replace what to replace the searched pattern with
options
various flags that alter the default behavior
the most used: g do not stop after the first match

February 7, 2016

awk

Can be viewed as an advanced version of cut


Can perform complex transformations with the fields

has the power of a programming language

Examples:

print the first field, delimited by :


cut d : f 1
awk F : {print $1;}

print the 3rd field, delimited by a variable number of tabs and/or spaces:
awk F [ \t]+ {print $3;}

February 7, 2016

Essential system
services

February 7, 2016

System services

System service

a long running process


a daemon (running in background)

performs specific tasks, relevant to:


the system itself (maintenance, sanity checks, logging etc.)
users (content serving, remote connection provisioning etc.)

typically started at boot-time

Interacting with a system service:

configuration
via configuration files

starting / stopping / restarting


via scripts

status checking
via log files

February 7, 2016

Important system services

Maintaining accurate time

syslog

Task scheduling

implements the NTP (network time protocol)

Logging

ntpd

cron

Providing remote connections

February 7, 2016

sshd
implements the SSH protocol

NTP

Used for maintaining accurate system time

Can act as:

server provide time to other NTP clients

ntpd

client receive time from an NTP server and adjusting the local time

package: ntp
configuration file: /etc/ntp.conf
script: /etc/init.d/ntp

default configuration

February 7, 2016

act as NTP client, using some predefined servers


also act as NTP server

/etc/ntp.conf
...
# pool.ntp.org maps to about 1000 low-stratum NTP servers.
# pick a different set every time it starts up.

Your server will

Please consider joining the

# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst
...
# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
...

February 7, 2016

date

Without any parameters, displays the system time:


# date
Sun Feb 24 14:56:24 EET 2013

A display format can be specified:

date +format

# date +%Y-%m-%d
2013-02-24

Can also be used for manually setting the system time:

February 7, 2016

date s STRING

Logging

Implements the syslog standard

stores the messages in log files, according to logging rules

rsyslogd

captures log messages generated by the system

package: rsyslog
configuration files: /etc/rsyslog.conf, /etc/rsyslog.d/
script: /etc/init.d/rsyslog

Rsyslog can also act as a remote syslog server

February 7, 2016

log messages from various systems are centralized on a single syslog server
useful for scalable, centralized monitoring solutions

Facility and severity

Every syslog message has a facility and severity

Facility level

indicates where the message is coming from


24 facility levels:
auth, authpriv, daemon, cron, ftp, lpr, kern, mail, news, syslog, user, uucp, local0,
... , local7

Severity level

indicates how important the message is


8 severity levels:
Emergency, Alert, Critical, Error, Warning, Notice, Info, Debug

February 7, 2016

/etc/rsyslog.conf

Messages are mapped to files based on facility and severity:


*.*;auth,authpriv.none

-/var/log/syslog

cron.*

/var/log/cron.log

auth,authpriv.*

/var/log/auth.log

daemon.*

-/var/log/daemon.log

kern.*

-/var/log/kern.log

lpr.*

-/var/log/lpr.log

mail.*

-/var/log/mail.log

user.*

-/var/log/user.log

February 7, 2016

Task scheduling

Used for scheduling jobs to run periodically

cron

typical jobs are system administration and maintenance tasks

package: cron
configuration file: /etc/crontab, /etc/cron.*
script: /etc/init.d/cron

Configuration:

February 7, 2016

global: /etc/crontab
per-user: accessed with crontab e

10

crontab syntax
# m h dom mon dow user

command

* * *

root

/usr/local/bin/statistics.sh > /dev/null 2> /dev/null

17 *

* * *

root

cd / && run-parts --report /etc/cron.hourly

fields:

minute
hour
day of month
month
day of week
username
command

Note: in per-ser configurations, the username field is missing

February 7, 2016

11

SSH

Secure Shell protocol

current version: 2.0


used for accessing a shell on remote systems

OpenSSH - client-server implementation

February 7, 2016

package: ssh
configuration files: /etc/ssh/*
script: /etc/init.d/ssh

12

Connecting to a remote system

Connecting to the shell:

ssh username@hostname

Run a command on the remote system, then exit

ssh username@hostname command


does not provide a remote shell

Enable X forwarding

ssh X username@hostname
useful for launching remote graphical applications

February 7, 2016

13

SSH key-based authentication

If used, the password will not longer be prompted

useful for automation scripts

Steps for enabling

generate a key-pair on the local system


ssh-keygen t rsa

copy the public key to the remote system


ssh-copy-id username@hostname

log into the remote system


ssh username@hostname
no password will be needed

February 7, 2016

14

Package
management

February 7, 2016

Packages

Most distributions provide packages for installing software

similar to the installers in Windows

A package contains

the software itself (in compiled binary form)


meta-information
dependencies
what to do after installing the package (e.g. post-install configuration)
what to do for removing the package

Not all packages contain programs

some contain fonts, images, themes etc.

Most packages have dependencies

February 7, 2016

example: a complex program depends on packages that provide libraries

Package formats

Some distributions define their own package format

.deb Debian and derivatives (Ubuntu, Mint etc.)

Some package formats can be translated into each other

.rpm Red Hat and derivatives (CentOS, Fedora etc.), OpenSUSE


.apk Android
.ebuild Gentoo

February 7, 2016

.deb packages can be converted from other formats using the alien utility
in case of complex dependencies, the conversion may not work well

Installing a package

Manually

download the package file (.deb, .rpm etc.)

if a package has dependencies, they must be downloaded and installed as well

example: dpkg i firefox-19.0.deb

manually install the package with the distribution-specific command (dpkg, rpm
etc.)

From the repository

identify the package name

example: apt-get install firefox

February 7, 2016

install the package with the distribution-specific command (apt-get, yum etc.)
all dependencies are resolved and installed automatically

Configuring the repository

In Debian, the repository sources are found in /etc/apt/sources.list:


deb http://ftp.debian.org/debian/ squeeze main contrib non-free
deb-src http://ftp.debian.org/debian/ squeeze main contrib non-free
deb http://security.debian.org/ squeeze/updates main contrib non-free
deb-src http://security.debian.org/ squeeze/updates main contrib non-free

Third party repositories can also be added

February 7, 2016

useful for installing software that is not officially supported

APT operations

apt-get update

upgrades packages that have newer versions that the ones locally installed

apt-get remove [ --purge ] package

only downloads the lists of packages, not packages itself

apt-get upgrade

updates the repository

removes the package from the system


if --purge is present, also deletes the configuration files

apt-cache search word

February 7, 2016

search for a package with the name or description containing the specified word

Hardware devices

February 7, 2016

Hardware devices - tasks

Enumerate hardware devices on the system:


Using commands (recommended): lspci, lsusb, lshw

Via information in virtual file systems: /sys/, /proc/, /dev/

Device drivers
Inspecting the kernel configuration: /boot/config*

Manipulating kernel modules:


listing: lsmod
inserting: insmod, modprobe
removing: rmmod, modprobe
configuring: /etc/modules

February 7, 2016

lspci

Enumerates the devices connected to the PCI bus of the system

Usage: lspci [-v | -vv | -vvv]

without any option, displays a brief one-line description of each device


-v verbose, display detailed descriptions
useful information: the kernel drivers used by the devices
-vv even more verbose
-vvv display every possible detail, even not useful ones

Example: information about a network card displayed by lspci v


03:06.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8139/8139C/8139C+ (rev 10)
Subsystem: Allied Telesyn International Device c10f
Flags: bus master, medium devsel, latency 64, IRQ 20
I/O ports at ce00 [size=256]
Memory at fdcff000 (32-bit, non-prefetchable) [size=256]
Capabilities: [50] Power Management version 2
Kernel driver in use: 8139too

February 7, 2016

lsusb

Enumerates the devices attached to the USB ports of the system

Usage: lsusb [-v]

without any option, displays a brief one-line description of each device


-v verbose, display detailed descriptions

Example:
# lsusb
Bus 007 Device
Bus 006 Device
Bus 005 Device
Bus 004 Device
Bus 003 Device
Power Supply
Bus 003 Device
Bus 002 Device
Bus 001 Device

February 7, 2016

001:
001:
001:
001:
002:

ID
ID
ID
ID
ID

1d6b:0001
1d6b:0001
1d6b:0001
1d6b:0001
051d:0002

Linux Foundation 1.1 root


Linux Foundation 1.1 root
Linux Foundation 1.1 root
Linux Foundation 1.1 root
American Power Conversion

hub
hub
hub
hub
Uninterruptible

001: ID 1d6b:0001 Linux Foundation 1.1 root hub


001: ID 1d6b:0002 Linux Foundation 2.0 root hub
001: ID 1d6b:0002 Linux Foundation 2.0 root hub

lshw

Displays a list of all hardware devices in the system (not just PCI or USB devices)

the list is presented in a hierarchical format

Usage: lshw [-xml | -html | -short]

February 7, 2016

without any option, it displays information in a text format


-short text format, only displays brief information
-xml displays output as XML
-html displays output as HTML

/sys, /proc, /dev

Raw information about the hardware devices can also be found in some virtual file
systems

/sys/

contains information used by lspci, lsusb, lshw


example: /sys/bus/pci/devices/

/proc/

contains information about essential devices


example: /proc/cpuinfo, /proc/meminfo

most information has been moved to /sys/

/dev/

block and devices have nodes in /dev/


example: hard-disks and partitions: /dev/sd*

February 7, 2016

Device drivers

Device driver software that acts as an interface between the hardware and the
operating system

runs inside the kernel

A device driver can be:

included in the kernel image


always loaded in memory (even if there is no corresponding physical device)
cannot be removed from memory

available as a kernel module


separate from the kernel image
found in /lib/modules (.ko file)
only loaded if needed by a physical device
can be unloaded and loaded into memory at any time

February 7, 2016

Kernel configuration

/boot/config-kernel_version

contains a list with all kernel features


example: /boot/config-2.6.32-5-amd64

each line refers to a feature: feature_name=status


status
y included in the kernel image
m available as module
# feature_name is not set
feature not available

Example:

CONFIG_RTL8180=m
the driver for the Realtek 8180 NIC is available as a module

February 7, 2016

lsmod

Displays all kernel modules currently loaded into memory

Example:
# lsmod
Module
btrfs
zlib_deflate
crc32c
libcrc32c
ufs
qnx4
hfsplus
hfs
minix
ntfs
vfat
msdos
fat
jfs
xfs
exportfs
reiserfs
ext3
...

February 7, 2016

Size
376157
17746
2560
1074
56522
6194
65350
37567
21213
162972
7900
6202
40070
140201
435849
3186
194300
106854

Used by
0
1 btrfs
1
1 btrfs
0
0
0
0
0
0
0
0
2 vfat,msdos
0
0
1 xfs
0
0
9

modprobe, insmod

Used for inserting a kernel module in memory

modprobe

Usage: modprobe module_name


does not need the full path to the .ko file
automatically resolves dependencies
inserts additional modules if needed
recommended

insmod

Usage: insmod /path/to/module.ko


the path to the .ko file must be known
does not resolve dependencie

February 7, 2016

10

modprobe r, rmmod

Used for removing a kernel module from memory

modprobe

Usage: modprobe r module_name


also removes the dependencies, it they are not used by anybody else
recommended

rmmod

Usage: rmmod [-f] module_name


-f forcefully removes the modules (dangerous!)

February 7, 2016

11

Loading and blacklisting modules at boot

/etc/modules

modules that should be loaded at boot time can be manually specified here
each line contains a module name

/etc/modprobe.d/blacklist.conf

February 7, 2016

modules that must not be loaded are specified here


each line contains blacklist module_name

12

Booting the
system

February 7, 2016

Boot sequence

Each step from the boot sequence prepares the system for the next step

Steps:

February 7, 2016

BIOS
MBR
bootloader
kernel
init
runlevel (services)

GRUB

GRand Unified Bootloader


The most popular Linux bootloader

versions:
GRUB 1 obsolete
GRUB 2 current stable version

Roles:

February 7, 2016

choose the OS to load


for the chosen OS, locate the kernel image and execute it

GRUB - installing

update-grub

detects the operating systems installed on the system and builds a configuration
file
the configuration file is saved in /boot/grub/grub.cfg

grub-install block_device

installs the bootloader code in the MBR of the specified hard-disk


example: grub-install /dev/sda

Note: GRUB can also be installed on a partition, but that partition has to be marked
bootable

February 7, 2016

GRUB - configuring

It is not recommended to edit /boot/grub/grub.cfg manually

Recommended way for configuring GRUB:


edit the parameters in /etc/default/grub

re-run update-grub

Examples of parameters:
GRUB_DEFAULT
which entry from the GRUB menu to boot into by default (starts from 0)

GRUB_TIMEOUT
number of seconds to wait for user input before booting into default entry

February 7, 2016

Kernel parameters

When executing the kernel image, parameters can be specified

similar to specifying parameters when running an executable

Parameters can be configured in /etc/default/grub, by editing


GRUB_CMDLINE_LINUX

Parameters can also be manually altered at boot time, by using the GRUB menu

After the system has booted, the parameters can be accessed using /proc/cmdline:
# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-2.6.32-5-amd64 root=/dev/sda2 ro console=tty0

February 7, 2016

Init and runlevels

After the init process is started, the system enters a certain runlevel

A runlevel defines which services should be started and which should remain stopped

there are 7 runlevels


each distribution has slightly different meanings for the runlevels

Runlevels for Debian systems:

February 7, 2016

0 halt (shutdown)
1 single-user mode (no daemons and no networking started)
2-5 multi-user mode (all services and networking started)
6 reboot

Default runlevel. Changing runlevels

The default runlevel is specified in /etc/inittab:


# The default runlevel.
id:2:initdefault:

For changing the runlevel, use init N, where N is the desired runlevel to switch to

examples:
init 0 shuts the system down
init 6 reboots the system
init 1 stops all services and disables networking (mostly used for debugging
purposes)

February 7, 2016

Initialization scripts

Services can be started, stopped, or restarted using initialization scripts


they are found in /etc/init.d/

the parameter taken by the script specifies the action to be taken


start start the service
stop stop the service
restart stop, then start the service (usually used after changing some
configuration parameters)

February 7, 2016

example: /etc/init.d/apache2 restart

Runlevel scripts

Each runlevel has associated scripts which specify which services to run and which to
stop
the scripts are found in /etc/rcN.d/
N is the runlevel number

the scripts are symbolic links to initialization scripts from /etc/init.d/

Syntax of a script name:


SxyName or KxyName
S specifies that the service should be started
K specified that the service should be stopped (killed)
xy is the number which imposes the order in which the scripts are ran

February 7, 2016

10

You might also like