Linux Basics (Day 3)

Filesystems, system booting and services


The Linux Filesystem Hierarchy

What's a filesystem?

In computing, a file system is used to control how information is stored and retrieved. Without a file system, information placed in a storage area would be one large body of information with no way to tell where one piece of information stops and the next begins. -- ( )

Which Filesystem to choose

ext4 pretty standard now, rock solid, medium performance
ext3 Still ok, but not good for large filesystems
ext2 Legacy, only useful in special use cases (i.e. use drives, /boot)
xfs Great performance (multi-threaded, great for large filesystems)
btrfs ZFS-like filesystem for Linux. Has lots of potential but not quite ready for production

The File System

$ ls /
bin  boot  data  dev  etc  home  lib  lib64  lost+found  media
mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  util  var

Installed programs and utilities

/bin                /usr/sbin
/sbin               /usr/local/bin
/usr/bin            /usr/local/sbin

PATH environment variable

$ echo $PATH

which command

$ which bash

Where are drives mounted?

$ dmesg | tail
[260930.20]  sdb: sdb1
[260930.32] sd 6:0:0:0: >[sdb] Asking for cache data failed
[260930.32] sd 6:0:0:0: >[sdb] Assuming drive cache: write through
[260930.32] sd 6:0:0:0: >[sdb] Attached SCSI removable disk

Three Tiers of Filesystem Hierarchy


See also man hier

/bin & /sbin

/usr (Historical Context)

People were running out of disk space so:

/usr Merge

/usr (Modern Context)

$ ls /usr
bin  games    lib    libexec  sbin   src
etc  include  lib64  local    share  tmp
Location Description
/usr/bin Packages installed by package manager
/usr/sbin Packages installed by package manager
/usr/etc Rarely used; files from /etc can be symlinked here
/usr/games Binaries for games and educational programs

/usr (Modern Context)

Location Description
/usr/include Include files for the C compiler
/usr/lib Object libraries (including dynamic libs); some unusual binaries
/usr/lib64 64-bit libraries
/usr/libexec Executables used with libraries; not used much
/usr/local Programs (and their configuration) locally installed by user go here
/usr/share Application data; typically examples and documentation
/usr/src/linux Kernel source goes here




/lib & /lib64

/media & /mnt


Special filesystem procfs contains a file-representation of the current state of the kernel and running processes.

# Which Linux kernel version are you running?
$ cat /proc/version
Linux version 2.6.32-504.3.3.el6.x86_64 (
(gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Dec 17
01:55:02 UTC 2014
# Learn about system's hardware
$ less /proc/cpuinfo
$ less /proc/meminfo
# Some parts of /proc can be written as well as read...
$ echo 3 > /proc/sys/vm/drop_caches # drop caches



User-Specific Data & Configuration

Space on drives

Use df to see disk free space.

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda8        73G   29G   41G  42% /

Use du to see disk usage.

$ du -sh /home/
21G /home/

Default output is in bytes, -h for human-readable output.

Commands for working with filesystems

Creating filesystems

$ mkfs -t ext4 /dev/sdb1
$ mkfs.ext4 /dev/sdb1
# Each FS has their own options. Look at man mkfs.<filesystem>

Mounting filesystems

$ mount /dev/sdb1 /data
# -t for type
# -o for options
# requires device path and mount point
$ umount /data

More filesystem commands

Tuning filesystems

$ tune2fs -m 0 /dev/sda1
# -m Reserved Blocks Percentage
# -l List contents of the superblock

Resizing filesystems online

$ resize2fs /dev/sda1
$ xfs_growfs /data



Blocks and dd

$ dd if=/dev/random of=/dev/sda
# What will this do?

Filesystem Consistency

$ fsck.ext4 -C 0 /dev/sda1
# -C 0 displays progress output to stdout
# Always do this in an "offline" state, i.e. single user mode

More about Journaling



Steps in boot process

  1. Hardware (POST)
  2. Bootloader (GRUB, NTLDR, etc)

As far as Linux cares:

  1. Kernel initialization
  2. Hardware configuration
  3. System processes
  4. Operator intervention (single-user)
  5. Execution of start-up scripts
  6. Multi-user operation





Boot Loaders (Grub)

GRUB Configuration

menuentry 'CentOS Linux (3.10.0-123.13.2.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-123.9.3.el7.x86_64-advanced-7c6fc4d5-965b-4824-80d6-d93dc649c4d9' {
  set gfxpayload=keep
  insmod gzio # also part_msdos, ext2
  set root='hd0,msdos1'
  search --no-floppy --fs-uuid --set=root 782935aa-7e68-4087-a50a-3aebacbb0277
  linux16 /vmlinuz-3.10.0-123.13.2.el7.x86_64 root=/dev/mapper/centos_util-root ro vconsole.keymap=us crashkernel=auto  vconsole.font=latarcyrheb-sun16 biosdevname=0 rhgb quiet LANG=en_US.UTF-8
  initrd16 /initramfs-3.10.0-123.13.2.el7.x86_64.img

GRUB Configuration



for f in /mount/*.sh; do
  [ -f "$f" ] && . "$f"
  [ -d "$NEWROOT/proc" ] && break;
exec switch_root "$NEWROOT" "$INIT" $initargs

real init

Single User Mode


Startup Script Tasks


System-V Boot Style

Run levels:

level 0 sys is completely down (halt)
level 1 or S single-user mode
level 2 through 5 multi-user levels
level 6 reboot level


# The default runlevel.

# What to do in single-user mode.

# What to do when CTRL-ALT-DEL is pressed.
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

# terminals
1:2345:respawn:/sbin/getty 38400 tty1
T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100

init.d Scripts

$ service sshd status
openssh-daemon (pid  1186) is running...

$ service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

Starting services on boot

$ chkconfig --list sshd
sshd            0:off 1:off 2:on  3:on  4:on  5:on  6:off

$ chkconfig sshd off

$ chkconfig --list sshd
sshd            0:off 1:off 2:off 3:off 4:off 5:off 6:off

Configuring init.d Scripts

$ cat /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ -g"

Shutting Down