When you see slides with a white background:
When you see slides with a colored background:
Environment variables contain information about processes.
$ echo $$ $PPID
4224 4223
# $$ = current process ID
# $PPID = parent PID
You can find all process id's by name using the
pidof command.
$ pidof python
1015 847 839
Processes have a parent-child relationship. Every process has a parent process.
$ bash
$ echo $$ $PPID
4812 4224
$ exit
$ echo $$ $PPID
4224 4223
$
One of the most common tools on Linux to look at processes.
[paul@centos]$ echo $$ $PPID
117051 117049
[paul@centos]$ bash
[paul@centos]$ echo $$ $PPID
127802 117051
[paul@centos]$ bash
[paul@centos]$ echo $$ $PPID
127845 127802
[paul@centos]$ ps fx
PID TTY STAT TIME COMMAND
117048 pts/0 S 0:00 sudo su -
117049 pts/0 S 0:00 \_ su -
117051 pts/0 S 0:00 \_ -bash
127802 pts/0 S 0:00 \_ bash
127845 pts/0 S 0:00 \_ bash
127882 pts/0 R+ 0:00 \_ ps fx
Can order processes according to cpu usage or other properties.
$ top
In case of trouble,
top is often the first tool to fire up, since it also provides you memory and swap space information.
The
kill command will kill (or stop) a process.
# List signals
$ kill -l
# kill -1 (SIGHUP) <= force to re-read its configuration file.
$ kill -1 1
# kill -15 (SIGTERM) <= standard kill
$ kill 1942
$ kill -15 1942
# kill -9 (SIGKILL) <= also called a sure kill. The kernel will shoot down the process.
$ kill -9 3342
Display stuff that runs in background of current shell.
$ jobs
$
This sends a SIGSTOP signal to the Linux kernel, effectively freezing the operation of the process.
[paul@centos]$ vim test
[1]+ Stopped vim test
[paul@centos]$ jobs
[1]+ Stopped vim test
[paul@centos]$
Processes that are started in background using the
& character at the end of the command line are also visible with the
jobs command.
[paul@centos]$ find / > allfiles.txt 2> /dev/null &
[2] 129217
[paul@centos]$ jobs
[1]+ Stopped vim test
[2]- Running find / > allfiles.txt 2> /dev/null &
[paul@centos]$
Will bring a background job to the foreground.
[paul@centos]$ jobs
[2] Running sleep 1000 &
[3]- Running sleep 2000 &
[4]+ Running sleep 3000 &
[paul@centos]$ fg 3
sleep 2000
Jobs that are
suspended in background can be started in background with
bg. Will sent a
SIGCONT signal.
# Example of the sleep command being reactivated in background with bg.
[paul@centos]$ jobs
[paul@centos]$ sleep 5000 &
[1] 129641
[paul@centos]$ sleep 3000
^Z
[2]+ Stopped sleep 3000
[paul@centos]$ jobs
[1]- Running sleep 5000 &
[2]+ Stopped sleep 3000
[paul@centos]$ bg 2
[2]+ sleep 3000 &
[paul@centos]$ jobs
[1]- Running sleep 5000 &
[2]+ Running sleep 3000 &
jobs command to verify whether you have any processes running in background.
vi to create a little text file. Suspend vi in background.
jobs that
vi is suspended in background.
find / > allfiles.txt 2>/dev/null in foreground. Suspend it in background before it finished?
sleep processes in background.
jobs in background.
kill command to suspend the last
sleep process.
All ata drives on your system will start with /dev/hd followed by a unit letter. The master hdd on the first ata controller is /dev/hda, the slave is /dev/hdb. For the second controller, the names of the devices are /dev/hdc and /dev/hdd.
IDE device naming
scsi drives follow a similar scheme, but all start with /dev/sd.
A modern Linux system will use /dev/sd* for scsi and sata devices, and also for sd-cards, usb-sticks, (LEGACY) ATA/IDE devices and solid state drives.
scsi device naming
/sbin/fdisk to find out what kind of disks are seen by the kernel.
# fdisk -l | grep Disk
Disk /dev/sda: 32.2 GB, 32214351872 bytes, 62918656 sectors
Disk label type: dos
Disk identifier: 0x000a41c5
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Disk label type: dos
Disk identifier: 0x59e794fa
# fdisk -l /dev/sda
Disk /dev/sda: 32.2 GB, 32214351872 bytes, 62918656 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a41c5
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 62914559 30944256 83 Linux
Kernel boot messages can be seen after boot with
dmesg.
# dmesg | grep disk
[ 1.439543] systemd[1]: Running in initial RAM disk.
[ 3.392719] sd 2:0:0:0: [sda] Attached SCSI disk
[ 3.398665] sd 3:0:1:0: [sdb] Attached SCSI disk
dmesg to make a list of hard disk devices detected at boot-up.
fdisk to find the total size of all hard disk devices on your system.
Linux requires you to create one or more partitions.
primary, extended and logical partitions
We saw before that hard disk devices are named /dev/hdx or /dev/sdx with x depending on the hardware configuration.
Next is the partition number, starting the count at 1.
Partition naming
Example below show you can see two partitions exists on /dev/sda
[root@centos]# fdisk -l /dev/sda
Disk /dev/sda: 32.2 GB, 32214351872 bytes, 62918656 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a41c5
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 62914559 30944256 83 Linux
First, we check with fdisk -l whether Linux can see the new disk. Then :
A FS is a way of organizing files on your partition. Besides file-base storage, file systems usually include directories and access control, and contain meta information about files likes access times, modifications times and file ownership.
The Linux kernel will inform you about currently loaded file system drivers in
/proc/filesystems.
root@centos ~# cat /proc/filesystems | grep -v nodev
ext2
iso9660
ext3
[root@poc-david ~]# mkfs /dev/sdc1
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=4 blocks, Stripe width=4 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
fdisk and
df.
This example shows how to create a new mount point with mkdir.
root@ipme:~# mkdir /home/project42
When the
mount point is created, and a
file system is present on the partition, then
mount can
mount the
file system on the
mount point directory.
[root@ipme ~]# mkdir /home/project42
[root@ipme ~]# mount -t ext2 /dev/sdc1 /home/project42/
[root@ipme ~]# cd /home/
[root@ipme home]# ls -l
total 1
drwx------. 4 david david 91 Jan 22 15:16 david
drwxr-xr-x. 3 root root 1024 Jan 22 15:44 project42
[root@ipme home]# df -h
Filesystem Size Used Avail Use% Mounted on
...
/dev/sdc1 194M 1.6M 183M 1% /home/project42
One monted, the new FS is accessible to users.
You can
unmount a mounted FS using the
umount
command.
root@ipme:~# umount /home/project42
Using
mount,
/proc/mounts
and
/etc/mtab
.
root@ipme:~# mount | grep /dev/sdc
/dev/sdc1 on /home/project42 type ext2 (rw,relatime,seclabel,stripe=4)
root@ipme:~# cat /proc/mounts | grep /dev/sdc
/dev/sdc1 /home/project42 ext2 rw,seclabel,relatime,stripe=4 0 0
root@ipme:~# cat /etc/mtab | grep /dev/sdc
/dev/sdc1 /home/project42 ext2 rw,seclabel,relatime,stripe=4 0 0
[root@poc-david home]# df -h
Filesystem Size Used Avail Use% Mounted on
...
/dev/sdc1 194M 1.6M 183M 1% /home/project42
Using
blkid and
/etc/fstab
.
[root@ipme home]# blkid
...
/dev/sdc1: UUID="3d66bc42-6b8a-4884-8a25-2b92fe676be8" TYPE="ext2"
[root@ipme home]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan 18 19:00:00 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=bf383770-7408-48df-b204-d408f67e439b / xfs defaults 0 0
UUID=203575e2-7c86-408b-b2e0-59df18bba2fb /boot xfs defaults 0 0
UUID=3d66bc42-6b8a-4884-8a25-2b92fe676be8 /home/project42 ext2 defaults 0 0
Imagine you want to enlarge space available for /src/project42.
You're force to unmount the FS, take a backup of the data, remove and recreate partitions, and then restore the data and remout the FS.
Virtual layer between the mounted FS and the hardware device.
A physical volume is any block device (a disk, a partition ...).
All these devices can become a member of a volume group.
[root@ipme ~]# pv
pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan
A volume group is an abstraction layer between block devices and logical volumes.
[root@ipme ~]# vg
vgcfgbackup vgchange vgconvert vgdisplay vgextend vgimportclone
vgmknodes vgremove vgs vgsplit vgcfgrestore vgck
vgcreate vgexport vgimport vgmerge vgreduce vgrename
vgscan
A logical volume is created in a volume group. Logical volumes that contain a FS can be mounted. The use of lv is similar to the use of partitions and is accomplished with the same standard commands (mkfs, mount...).
[root@ipme ~]# lv
lvchange lvcreate lvextend lvmconf lvmdiskscan lvmetad lvmsadc
lvreduce lvrename lvs lvconvert lvdisplay lvm lvmconfig
lvmdump lvmpolld lvmsar lvremove lvresize lvscan
How can we use a device to create vg & lv:
[root@ipme00 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@ipme00 ~]# vgcreate vg /dev/sdc
Volume group "vg" successfully created
[root@ipme00 ~]# lvcreate --size 500m vg
Logical volume "lvol0" created.
[root@ipme00 ~]# mke2fs -m0 -j /dev/vg/lvol0
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
...
[root@ipme00 ~]# mkdir /home/project10
[root@ipme00 ~]# mount /dev/vg/lvol0 /home/project10/
[root@ipme00 ~]# df -h | grep proj
/dev/mapper/vg-lvol0 477M 2.3M 474M 1% /home/project10
A logical volume is very similar to a partition, it can be formatted with a file system and can be mounted so that users can access it.
Extend a logical volume
[root@ipme00 ~]# df -h | grep pro
/dev/mapper/vg-lvol0 477M 2.3M 474M 1% /home/project10
[root@ipme00 ~]# lvextend -L +100 /dev/vg/lvol0
Size of logical volume vg/lvol0 changed from 500.00 MiB (125 extents) to 600.00 MiB (150 extents).
Logical volume vg/lvol0 successfully resized.
[root@ipme00 ~]# df -h | grep pro
/dev/mapper/vg-lvol0 477M 2.3M 474M 1% /home/project10
[root@ipme00 ~]# lvdisplay /dev/vg/lvol0 | grep Size
LV Size 600.00 MiB
[root@ipme00 ~]# resize2fs /dev/vg/lvol0
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg/lvol0 is mounted on /home/project10; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/vg/lvol0 is now 614400 blocks long.
[root@ipme00 ~]# df -h | grep pro
/dev/mapper/vg-lvol0 574M 2.3M 571M 1% /home/project10
Display a quick overview of all volume groups.
[root@ipme00 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg 1 1 0 wz--n- <50.00g 49.41g
Scan all disks for existing Volume Groupes. Also update the
/etc/lvm/.cache file.
[root@ipme00 ~]# vgscan
Reading volume groups from cache.
Found volume group "vg" using metadata type lvm2
Give more detailed information about a volume group.
[root@ipme00 ~]# vgdisplay vg
--- Volume group ---
VG Name vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 150 / 600.00 MiB
Free PE / Size 12649 / 49.41 GiB
VG UUID qzcwDf-EpF6-Xjsr-dZaI-7kTv-Nbut-u2WNYb
[root@ipme00 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvol0 vg -wi-ao---- 600.00m
[root@ipme00 ~]# lvscan
ACTIVE '/dev/vg/lvol0' [600.00 MiB] inherit
[root@ipme00 ~]# lvdisplay /dev/vg/lvol0
--- Logical volume ---
LV Path /dev/vg/lvol0
LV Name lvol0
VG Name vg
LV UUID 7ZKmUs-wmY9-8gT0-P25J-fWMR-vkZO-ejs1UL
LV Write Access read/write
LV Creation host, time ipme00, 2018-02-04 11:21:42 +0000
LV Status available
# open 1
LV Size 600.00 MiB
Current LE 150
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
Use
pvcreate command to add devices to lvm.
# How to add a disk to lvm
[root@ipme00 ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
# How to add partition to lvm
[root@ipme00 ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
# How to add multiple disks or partitions to lvm
[root@ipme00 ~]# pvcreate /dev/sde /dev/sdf /dev/sdg
Physical volume "/dev/sdce" successfully created.
Physical volume "/dev/sdcf" successfully created.
Physical volume "/dev/sdcg" successfully created.
Use
pvremove command to remove physical volumes to lvm.
[root@ipme00 ~]# pvremove /dev/sde /dev/sdf /dev/sdg
Label on physical volume "/dev/sde" successfully wiped.
Label on physical volume "/dev/sdf" successfully wiped.
Label on physical volume "/dev/sdg" successfully wiped.
When you used
fdisk to resize a partition on a disk, then you must use
pvresize to make lvm recognize the new size of the physical volume that represents this partition.
[root@ipme00 ~]# pvresize /dev/sdc
Physical volume "/dev/sdc" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
Use
vgcreate
command to create a volume group.
# You can immediately name all the physical volume that span to the volume group
[root@ipme00 ~]# vgcreate vg42 /dev/sdc /dev/sdd
Volume group "vg42" successfully created
Use the
vgextend command to extend an existing volume group with a physical volume.
[root@ipme00 ~]# vgextend vg42 /dev/sdg
Volume group "vg42" successfully extended
Use the
vgremove command to remove volume groups from lvm. The volume groups may not be in use.
[root@ipme00 ~]# vgremove vg42
Volume group "vg42" successfully removed
Use the
vgreduce command to remove a Physical Volume from a Volume Group.
[root@ipme00 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdc vg1 lvm2 a-- <50.00g <50.00g
/dev/sdd lvm2 --- 10.00g 10.00g
[root@ipme00 ~]# vgextend vg1 /dev/sdd
Volume group "vg1" successfully extended
[root@ipme00 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdc vg1 lvm2 a-- <50.00g <50.00g
/dev/sdd vg1 lvm2 a-- <10.00g <10.00g
[root@ipme00 ~]# vgreduce vg1 /dev/sdc
Removed "/dev/sdc" from volume group "vg1"
[root@ipme00 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdc lvm2 --- 50.00g 50.00g
/dev/sdd vg1 lvm2 a-- <10.00g <10.00g
Use the
lvcreate command to create Logical Volumes in a VG.
# Creates a 8GB Logical Volume in Volume Group vg42
[root@ipme00 ~]# lvcreate -L8G vg42
Logical volume "lvol0" created.
# Creates a 200MB Logical Volume named MyLV in Volume Group vg42
[root@ipme00 ~]# lvcreate -L200M -nMyLV vg42
Logical volume "MyLV" created.
# Does the same thing, but with different syntax
[root@ipme00 ~]# lvcreate --size 200M -n MyLV vg42
Logical Volume "MyLV" already exists in volume group "vg42"
# Creates a LV that occupies 10 percent of the VG
[root@ipme00 ~]# lvcreate -l 10%VG -n MyLV2 vg42
Logical volume "MyLV2" created.
# Creates a LV that occupies 30 percent of the remaining free space in the VG
[root@ipme00 ~]# lvcreate -l 30%FREE -n MyLV3 vg42
Logical volume "MyLV3" created.
Use the
lvremove command to remove Logical Volume from a Volume Group.
[root@ipme00 ~]# lvremove /dev/vg42/lvol0
Do you really want to remove active logical volume vg42/lvol0? [y/n]: y
Logical volume "lvol0" successfully removed
[root@ipme00 ~]# lvremove /dev/vg42/MyLV /dev/vg42/MyLV2 /dev/vg42/MyLV3
Do you really want to remove active logical volume vg42/MyLV? [y/n]: y
Logical volume "MyLV" successfully removed
Do you really want to remove active logical volume vg42/MyLV2? [y/n]: y
Logical volume "MyLV2" successfully removed
Do you really want to remove active logical volume vg42/MyLV3? [y/n]: y
Logical volume "MyLV3" successfully removed
[root@ipme00 ~]#
To extend a Logical Volume ;-)
[root@ipme00 ~]# lvdisplay /dev/vg42/lvol0 | grep Size
LV Size 5.00 GiB
[root@ipme00 ~]# lvextend -L +100 /dev/vg42/lvol0
Size of logical volume vg42/lvol0 changed from 5.00 GiB (1280 extents) to <5.10 GiB (1305 extents).
Logical volume vg42/lvol0 successfully resized.
[root@ipme00 ~]# lvdisplay /dev/vg42/lvol0 | grep Size
LV Size <5.10 GiB
Renaming a Logical Volume is done with
lvrename.
[root@ipme00 ~]# lvrename /dev/vg42/lvol0 /dev/vg42/COUCOU
Renamed "lvol0" to "COUCOU" in volume group "vg42"
[root@ipme00 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vg42/COUCOU
LV Name COUCOU
VG Name vg42
LV UUID nbwc3d-qT2p-v6u2-D1t6-KtLF-MN98-ewAMd3
LV Write Access read/write
LV Creation host, time ipme00, 2018-02-04 12:45:10 +0000
LV Status available
# open 0
LV Size <5.10 GiB
Current LE 1305
Segments 1
...
fdisk,
mount,
pvs,
vgs,
lvs, (
pv
|
lv
|
vg
)
display and
df. Does
fdisk give you any information about lvm?
The kernel receives system control from the bootloader. After a while the kernel starts the init daemon. The init daemon (/sbin/init) is the first daemon that is started and receives process id 1 (PID 1). Init never dies.
When /etc/init is started, it will first read its configuration file /etc/inittab. In that file, it will look for the value of initdefault.
[paul@centos ~]$ grep ^id /etc/inittab
id:3:initdefault:
The value found in initdefault indicates the default runlevel.
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you don't have network)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
Runlevel 0 = shutting down ; 1 = troubleshooting ; 3 = servers ; 5 = desktop ; 6 = reboot
Independent of the selected runlevel, init will run the /etc/rc.d/rc.sysinit script to initialize hardware, populate /etc/mtab while mounting file systems, [...]
[paul@centos ~]$ egrep -e"^# Ini" -e"^# Sta" -e"^# Che" /etc/rc.d/rc.sysinit
# Check SELinux status
# Initialize hardware
# Start the graphical boot, if necessary; /usr may not be mounted yet...
# Initialiaze ACPI bits
# Check filesystems
# Start the graphical boot, if necessary and not done yet.
# Check to see if SELinux requires a relabel
# Initialize pseudo-random number generator
# Start up swapping.
# Initialize the serial ports.
Init will start the rc script with the runlevel as the only parameter.
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
/etc/rcX.d/ directories contain links to scripts in /etc/init.d/.
[root@centos rc3.d]# ls -l | tail -4
lrwxrwxrwx 1 root root 19 Oct 11 2008 S98haldaemon -> ../init.d/haldaemon
lrwxrwxrwx 1 root root 19 Oct 11 2008 S99firstboot -> ../init.d/firstboot
lrwxrwxrwx 1 root root 11 Jan 21 04:16 S99local -> ../rc.local
lrwxrwxrwx 1 root root 16 Jan 21 04:17 S99smartd -> ../init.d/smartd
Scripts in /etc/init.d/ can be used when the system is running to start and stop daemons (services).
root@centos:~# /etc/init.d/samba restart
* Stopping Samba daemons... [ OK ]
* Starting Samba daemons... [ OK ]
[root@centos ~]# service smb restart
Shutting down SMB services:
Shutting down NMB services:
Starting SMB services:
Starting NMB services:
[ OK ]
[ OK ]
[ OK ]
[ OK ]
The purpose of
chkconfig is to relieve system administrators of manually managing all the links and scripts in
/etc/init.d and
/etc/rcX.d/.
chkconfig to list the status of a service in the different runlevels.
[root@centos ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos ~]# chkconfig --level 3 crond off
[root@centos ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:off 4:on 5:on 6:off
Every script in
/etc/init.d/ can have lines to tell
chkconfig what to do with the service.
[root@RHEL52 ~]# head -9 /etc/init.d/crond | tail -5
# chkconfig: 2345 90 60
# description: cron is a standard UNIX program that runs user-specified
# programs at periodic scheduled times. vixie cron adds a
# number of features to the basic UNIX cron, including better
# security and more powerful configuration options.
The service should be started (2345), followed by the priority for start (90) and stop (60).
Services can be enable/disable in all runlevels with one command.
[root@centos ~]# chkconfig crond off
[root@centos ~]# chkconfig --list crond
crond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos ~]# chkconfig crond on
[root@centos ~]# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
See your current runlevel with the
runlevel or
who -r command.
[root@centos ~]# runlevel
N 3
[root@centos ~]# who -r
run-level 3 Jul 28 09:15
Switch to another runlevel with the
telinit command. On linux,
/sbin/telinit is usually a (hard) link to
/sbin/init.
root@centos:~# runlevel
N 2
root@centos:~# init 3
root@centos:~# runlevel
2 3
Used to properly shut down a system.
Common switches :
-a,
-t,
-h and
-r.
root@centos:~# man shutdown # ;-)
root@centos:~# shutdown -t5 -h now
The binary /sbin/reboot is the same as /sbin/halt and /sbin/poweroff.
When in runlevel 0 or 6
halt,
reboot and
poweroff will tell the kernel to
halt,
reboot or
poweroff the system.
When not in runlevel 0 or 6, typing
reboot as root actually calls the
shutdown command with the
-r switch.
halt,
reboot and
poweroff all write to
/var/log/wtmp. To look at
/var/log/wtmp, we need to use th
last.
[root@centos ~]# last | grep reboot
reboot system boot 2.6.18-128.el5 Fri May 29 11:44 (192+05:01)
reboot system boot 2.6.18-128.el5 Wed May 27 12:10 (06:49)
reboot system boot 2.6.18-128.el5 Mon May 25 19:34 (1+15:59)
reboot system boot 2.6.18-128.el5 Mon Feb 9 13:20 (106+21:13)
It is likely that systemd will replace all the standard init/runlevel/rc functionality. Both Red Hat and Debian have decided in 2014 that systemd will be replacing init in future releases (RHEL7/CENTOS7 and Debian 8).
# Gérer les services
$ systemctl
# Connaître la liste des services actifs
$ systemctl list-units -t service
# Connaître la liste de tous les services
$ systemctl list-units -t service --all
# Gestion des unistés
$ systemctl start <unit>
$ systemctl stop <unit>
$ systemctl restart <unit>
$ systemctl reload <unit>
# Activer une unité au démarrage
$ systemctl enable <unit>
$ systemctl disable <unit>
# Mask une unité
$ systemctl mask <unit>
$ systemctl unmask <unit>
# Lire les logs
$ journalctl
systemctl to list enabled services.
The
crontab command can be used to maintain the
crontab file.
Each user can have their own crontab file to schedule jobs at a specific time.
This time can be specified with five fields in this order: minute, hour, day of the month, month and day of the week.
If fields contains asterisk (*), then this means all values of that field.
User should not edit the crontab file directly, instead they should type
crontab -e which will use the editor defined in the EDITOR or VISUAL environment variable. Users can display their cron table with
crontab -l.
The cron daemon crond is reading the cron tables, taking into account the /etc/cron.allow and /etc/cron.deny files.
When the
cron.allow file exists, then your username has to be in it, otherwise you cannot use
cron.
When the
cron.deny file does not exists, then your username cannot be in the
cron.deny file if you want to use
cron.
The /etc/crontab file contains entries for when to run hourly/daily/weekly... tasks.
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
Contain the tasks that are run at the times scheduled in /etc/crontab.
paul@centos:~$ ls -ld /etc/cron.*
drwxr-xr-x 2 root root 4096 2008-04-11 09:14 /etc/cron.d
drwxr-xr-x 2 root root 4096 2008-04-19 15:04 /etc/cron.daily
drwxr-xr-x 2 root root 4096 2008-04-11 09:14 /etc/cron.hourly
drwxr-xr-x 2 root root 4096 2008-04-11 09:14 /etc/cron.monthly
drwxr-xr-x 2 root root 4096 2008-04-11 09:14 /etc/cron.weekly
crontab -e to schedule a script to run every four minutes.
The standard method of logging on Linux was through the syslogd daemon.
Syslog was developed by Eric Allman for sendmail, but quickly became a standard among many Unix application.
The syslogd daemon can receive message on udp port 514 from many applications, and can append to log files, print, displays messages on terminal ...
syslogds daemon configuration is in /etc/syslog.conf
The new method is called reliable and extended syslogd and uses the rsyslogd daemon and the /etc/rsyslogd.conf configuration file. The syntax is backwards compatible.
Each line uses a facility to determine where the message is coming from and also a priority for the severity of the message and an action to decide on what to do with the message.
rsyslogd has many more features that can be expanded by using modules.
root@ipme00:/etc# man rsyslog.conf # C'est kdo Cédric ;-)
root@ipme00:/etc# man rsyslogd
root@ipme00:/etc#
The
man rsyslog.conf will explain the different default facilities for certain deamon, such as mail, kernel msg ...
# List of several facilities
auth (security)
authpriv
cron
daemon
ftp
kern
lpr mail
mark (internal use only)
news
syslog
user
uucp
local0-7
The worst severity message can have is emerg followed by alert and crit. Lowest should go to info and debug messages.
# List of all priorities
debug
info
notice
warning (warn)
err (error)
crit
alert
emerg (panic)
Default action: send a message to users listed as action.
root,user1 list of users, separated by comma's
* message to all logged on users
/ file (can be a printer, a console, a tty, ...)
-/ file, but don't sync after every write
| named pipe
@ other syslog hostname
Below a simple configuration of custom local4 messages in /etc/rsyslog.conf.
local4.crit /var/log/critandabove
local4.=crit /var/log/onlycrit
local4.* /var/log/alllocal4
Don't forget to restart the server after changing its configuration.
root@ipme00:/etc# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
root@ipme00:/etc#
To generate syslog test messages.
[root@ipme00 ~]# logger -p local4.debug "l4 debug"
[root@ipme00 ~]# logger -p local4.crit "l4 crit"
[root@ipme00 ~]# logger -p local4.emerg "l4 emerg"
[root@ipme00 ~]#
[root@ipme00 ~]# cat /var/log/critandabove
Feb 08 19:55:19 root root: l4 crit
Feb 08 19:55:28 root root: l4 emerg
A lot of files are always growing in size. We need to purge them.
$ cat /etc/logrotate.d/yum
/var/log/yum.log {
missingok
notifempty
size 30k
yearly
create 0600 root root
}
Displaying /proc/meminfo will tell you a lot about the memory.
[ipme@ipme00 ~]$ cat /proc/meminfo
MemTotal: 932648 kB
MemFree: 527548 kB
MemAvailable: 572792 kB
Buffers: 2232 kB
Cached: 164968 kB
SwapCached: 0 kB
Active: 144668 kB
Inactive: 100616 kB
Active(anon): 78248 kB
Inactive(anon): 5968 kB
Active(file): 66420 kB
Inactive(file): 94648 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
...
free
The
free tool can display the information provided by
/proc/meminfo in a more readable format.
[ipme@ipme00 ~]$ free -m
total used free shared buff/cache available
Mem: 910 164 515 5 230 559
Swap: 0 0 0
When the operating system needs more memory than physically present in RAM, it can use swap space.
Swap space is located on slower but cheaper memory. Notice that, although hard disks are commonly used for swap space, their access times are one thousand times slower.
The swap space can be a file, a partition, or a combination of files and partitions. cf
cat /proc/swaps.
You can find information about
swap usage using
vmstat.
[ipme@ipme00 ~]$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 0 0 525232 2232 235696 0 0 21 20 25 86 1 0 98 1 0
dmesg to find the total amount of memory in your computer.
free to display memory usage in kilobytes (then in megabytes).
vmstat during a memory leak.
The
iostat tool can display disk and cpu statistics.
[ipme@ipme ~]$ iostat -c
Linux 3.10.0-693.11.6.el7.x86_64 (ipme04) 08/02/2018 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1,24 0,05 0,47 0,69 0,00 97,55
Most Linux distribution have a package management system with online repositories containing thousands of packages.
This makes it very easy to install and remove applications, OS components, documentation and much more.
Debian package format:
.deb. Tools:
dpkg,
apt-get and
aptitude.
Red Hat package format:
.rpm. Tools:
rpm and
yum.
The
Red Hat package manager can be used on the command line with
rpm. Type
rpm --help.
Software distributed in the rpm format will be name foo-version.platform.rpm.
To obtain list of all installed software, use
rpm -qa command.
[root@ipme00 ~]$ rpm -qa | grep samba
samba-common-4.6.2-12.el7_4.noarch
samba-client-libs-4.6.2-12.el7_4.x86_64
To verify whether one package is installed, use
rpm -q.
[root@ipme00 ~]$ rpm -q gcc
le paquet gcc n'est pas installé
[root@ipme00 ~]$ rpm -q yum
yum-3.4.3-154.el7.centos.noarch
To install or upgrade a package, use the -Uvh switches.
root@ipme00:~# rpm -Uvh gcc-3.4.6-3
To remove a package, use the -e switch.
root@ipme00:~# rpm -e gcc-3.4.6-3
The rpm database is located at /var/lib/rpm. This database contains all meta information about package that are installed (via rpm). It keeps track of all files, which enables complete removes of software.
The Yellowdog Updater, Modified (yum) is an easier command to work with rpm packages. It is installed by default on Fedora and RHEL since version 5.2.
Issue yum list available to see a lit of available package.
[root@ipme00 ~]$ yum list | wc -l
10006
[root@ipme00 ~]$ yum list samba
Modules complémentaires chargés : fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Paquets disponibles
samba.x86_64 4.6.2-12.el7_4 updates
[root@ipme00 ~]$
To search for a package containing a certain string in the description or name use
yum search $string.
[root@ipme00 ~]$ yum search gcc
Modules complémentaires chargés : fastestmirror, langpacks
Loading mirror speeds from cached hostfile
====================================================================================== N/S matched: gcc =======================================================================================
gcc-c++.x86_64 : C++ support for GCC
gcc-gnat.x86_64 : Ada 95 support for GCC
gcc-objc.x86_64 : Objective-C support for GCC
gcc-objc++.x86_64 : Objective-C++ support for GCC
gcc-plugin-devel.x86_64 : Support for compiling GCC plugins
libgcc.i686 : GCC version 4.8 shared support library
libgcc.x86_64 : GCC version 4.8 shared support library
relaxngcc-javadoc.noarch : Javadoc for relaxngcc
compat-gcc-44.x86_64 : Compatibility GNU Compiler Collection
compat-gcc-44-c++.x86_64 : C++ support for compatibility compiler
compat-gcc-44-gfortran.x86_64 : Fortran support for compatibility compiler
gcc.x86_64 : Various compilers (C, C++, Objective-C, Java, ...)
...
To install an application, use
yum install $package. Naturally
yum will install all the necessary dependencies.
[root@ipme00 ~]# yum install tcl
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 27 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 3.4 kB 00:00:00
openlogic | 2.9 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 880 kB 00:00:00
(2/2): epel/x86_64/primary_db | 6.2 MB 00:00:01
Loading mirror speeds from cached hostfile
* epel: mirrors.coreix.net
Resolving Dependencies
--> Running transaction check
---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed
--> Finished Dependency Resolution
...
To bring all application up to date, by downloading and installing them, issue
yum update.
[root@ipme00 ~]# yum update -y
The configuration of yum repositories is done in /etc/yum/yum.conf and /etc/yum/repos.d/.
Configuration of yum itself is done in /etc/yum.conf. This file will contain the location of a log file and a cache directory for yum and can also contain list of repositories.
yum to search for and install the scp, tmux, and man-pages packages. Did you find them all?
A unicast communication originates from one computer and is destined for exactly one other.
A multicast is destined for a group (of computers).
A broadcast is meant for everyone.
The root name server of the internet use anycast. An anycast signal goes to the (geographically) nearest of well defined group.
To list all active network interface cards, including wireless and the loopback interface.
[root@ipme00 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.11 netmask 255.255.240.0 broadcast 10.0.15.255
inet6 fe80::20d:3aff:feb6:76c3 prefixlen 64 scopeid 0x20<link>
ether 00:0d:3a:b6:76:c3 txqueuelen 1000 (Ethernet)
RX packets 890 bytes 414485 (404.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1176 bytes 188441 (184.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig
tool is deprecated on some systems. Use the
ip tool instead.
[root@ipme00 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0d:3a:b6:76:c3 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.11/20 brd 10.0.15.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20d:3aff:feb6:76c3/64 scope link
valid_lft forever preferred_lft forever
Every host receives a hostname, often placed in a DNS name space forming the Fully Qualified Domain Name (fqdn).
[root@ipme00 ~]# cat /etc/hostname
ipme00
[root@ipme00 ~]# hostname
ipme00
See the computer's local routing table with
route.
[root@ipme00 ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
168.63.129.16 gateway 255.255.255.255 UGH 0 0 0 eth0
link-local 0.0.0.0 255.255.0.0 U 0 0 0 eth0
169.254.169.254 gateway 255.255.255.255 UGH 0 0 0 eth0
[root@ipme00 ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
168.63.129.16 gateway 255.255.255.255 UGH 0 0 0 eth0
link-local 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.169.254 gateway 255.255.255.255 UGH 0 0 0 eth0
If you can ping to another host, the tcp/ip is enabled.
[root@ipme00 ~]# ping 10.0.0.11
PING 10.0.0.11 (10.0.0.11) 56(84) bytes of data.
64 bytes from 10.0.0.11: icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from 10.0.0.11: icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from 10.0.0.11: icmp_seq=3 ttl=64 time=0.046 ms
^C
--- 10.0.0.11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.022/0.037/0.046/0.013 ms
Sniffing on the command line can be done with
tcpdump.
[root@ipme00 ~]# tcpdump -w /tmp/sshdump.tcpdump tcp port 22
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C4 packets captured
4 packets received by filter
0 packets dropped by kernel
[root@ipme00 ~]# tcpdump -r /tmp/sshdump.tcpdump
reading from file /tmp/sshdump.tcpdump, link-type EN10MB (Ethernet)
19:06:00.668871 IP ipme00.ssh > mlr78-h01-176-190-15-230.dsl.sta.abo.bbox.fr.58643: Flags [P.], seq 2952236723:2952236847, ack 2519052889, win 313, options [nop,nop,TS val 309500 ecr 598022889], length 124
19:06:00.681738 IP mlr78-h01-176-190-15-230.dsl.sta.abo.bbox.fr.58643 > ipme00.ssh: Flags [.], ack 0, win 4094, options [nop,nop,TS val 598022936 ecr 309467], length 0
19:06:00.720669 IP mlr78-h01-176-190-15-230.dsl.sta.abo.bbox.fr.58643 > ipme00.ssh: Flags [.], ack 124, win 4092, options [nop,nop,TS val 598022974 ecr 309500], length 0
19:06:04.292994 IP mlr78-h01-176-190-15-230.dsl.sta.abo.bbox.fr.58643 > ipme00.ssh: Flags [P.], seq 1:37, ack 124, win 4096, options [nop,nop,TS val 598026531 ecr 309500], length 36