# LPIC-101 # 101 System Architecture ## 101.1 Determine and config hardware settings - `lspci`: show list of pci devices - `lscpi -s <address> -v`: show detail - `lscpi -s <address> -k`: show detail of kernel drivers, modules - `lsusb`: show list of usb devices - `lsusb -d <addres> -v`: show detail - `lsusb -t`: show devices mappings - `lsmod`: Show all currently loaded modules: Name | bytes of RAM | depending modules - `modprobe -r <module_name>`: unload a module - `modinfo -p <module_name>`: Display parameters of a module - `/etc/modprobe.d/blacklist.conf`: block the loading of modules - `/etc/modprobe.conf`: config for modules's params - `/proc`: information about kernels data structures, running processes and config, stored in RAM - `/proc/cpuinfo`: CPU info - `/proc/interrupts`: interrupts by IO devices for each CPU - `/proc/dma`: show all dma channels - `/proc/ioports`: currently registered IO ports - `/sys`: device information and kernel data related to hardward - `/dev`: each files associated with each devices - `udev`: detect devices hotplug/coldplug, relied on `sysFS` ## 101.2 Boot the system #### BIOS vs UEFI - kernel is loaded by `bootloader` - BIOS assumes first 440 bytes of first storage device as first stage of bootloader (bootstrap), fist 512: Master Boot Record (contains partition table) ![](https://i.imgur.com/vgt5YPf.png) - UEFI: - `Secure Boot`: UEFI load signed EFI applications only ![](https://i.imgur.com/NTK6lYI.png) #### Bootloader - `Bootloader`: Most popular `GRUB`, can specify parameters for kernel boot - Kernel params must be added to `/etc/default/grub` in the line `GRUB_CMDLINE_LINUX` then `grub-mkdconfig -o /boot/grub/grub.cfg` - Read kernel params `/proc/cmdline` #### System initialization - Boot process: `bootloader` loads kernel into RAM, the kernel will open the `initramfs` for accessing modules to access the real fs. Kernel will then mount all filesystems configured in `/etc/fstab` and execute the `init` program (`systemd` or `Upstart`), `initramfs` is removed from RAM - Service managers: - `SysV standard`: using `runlevels` from 0 to 6 - `systemd`: modern, many features - `Upstart`: boot the speed by parallellizing the loading of system services #### Initialization Inspection - `kernel ring buffer`: store boot messages - `dmesg`: display messages from `krb` - `dmesg --clear` - `journalctl`: on systemd systems, use `-b`, `--boot`, `-k` or `--dmesg`, `--list-boots` show list of boot numbers relative to the current boot ## 101.3 Change runlevels / boot targets and shutdown or reboot the system - The service manager is the first program launched by the kernel (PID 1) #### SysVinit - `runlevels`: sets of system states - 0: system shutdown - 1, s or single: Single user mode, without network and other non-essential capabilities (maintainence mode) - 2, 3 or 4: Multi-user mode, users can login by console or network, 2 and 4 are often not used - 5: multi-user, 3 + graphical mode login - 6: system restart - `/sbin/init`: managing runlevels and associated daemons ![](https://i.imgur.com/GyTMGuR.png) - Indicate the action to the process associated with each runlevels - `/etc/inittab`: stores configuration for runlevels and scripts - `telinit q`: reload config, `telinit <runlevel>`: switch runlevel #### systemd - Resources and daemons: `units` - `unit`: name + type + configuration - Types of systemd units: - `service` - `socket`: fs socket or network socket - `device`: hardware devices - `mount`: mount points - `automount` - `target`: grouping of other units, managed as a single unit - `snapshot`: a saved state of the systemd - Using `systemctl` to control - Change system target: `systemctl set-default multi-user.target`: similar to SysV standard runlevels #### Upstart - `initctl list`: show current state of services and PIDs - `start`,`stop`, `status`: for managing services, legacy used by Ubuntu (now systemd) #### Shutdown and Restart - `shutdown`: all process received SIGTERM, followed by SIGKILL - `wall`: give messages to terminal sessions of all users # 102 Linux Installation and Package Management ## 102.1 Design hard disk layout - Disk contains many partitions, each partition contains a filesystem - `Logical Volume Manager (LVM)`: multiple partitions (even across disks) can be combined into a logical volume #### Mount Points - `mount point`: specific points in system's directory tree which filesystems mount on - `/mnt`: contains mount points (legacy), now is `/media` for user-removeable media (external disks, USB, SD,...) - `/mnt` is used for manually mount a filesystem #### Boot partition - Usually the first partition (for legacy purpose), first 528M: contains file needed by the bootloader, , the initial RAM disk and kernel images #### EFI System Partition (ESP) - UEFI uses to store boot loaders and kernel images - FAT-based - Is created upon installation and mounted under /boot/efi #### Variable data (/var) - Should be put under a seperate partition, some programs may write to `/var` until there are no space left, so put it in a seperate partition reduces the risk of system crask #### Swap - Swap partition is used to swap memory pages from RAM to disks as needed - Can use swap files ![](https://i.imgur.com/Yu6BVAa.png) #### LVM (Logical Volume Management) - Used to flexible manage spaces between partitions - Basic unit is the PV `physical volume` (block device: disk partition or RAID array) - PVs are grouped into Volume Groups (VG): abstract a single logical device, combined capacity - Volumes in VG are subdivided into fixed-sized pieces called `extents`: `Physical Extents` and `Logical Extents` (4MB default) - VG divided to Logical Volumes which like a parition, then it can add more extents or reduces extents to flex the space ## 102.2 Install a boot manager #### GRUB 2 vs GRUB Legacy - GRUB 2 is cleaner, safer, more powerful, flexible config file, modular design, better localization, internationalization - Support boot menus with splash screens, boot LiveCD ISOs #### Where is the bootloader - Legacy: Stored in MBR (contains partition table and bootloader). This bootloader is small and limited, so it passes control to the second bootloader between MBR and first partition (32 KB), this bootloader will load the OS - UEFI: Use GPT partitioning scheme (GUID Partition Table), GRUB is loaded by the firmware from the file `grubia32.efi` or `grubx64.efi` from a partition called ESP #### The `/boot` partition ![](https://i.imgur.com/reOdwLN.png) #### Contents of Boot partition - On x86 system: files are named with `-VERSION` suffix (config-4.15.0-65-generic), version is Linux kernel version - Config file: stores configuration parameters for Linux kernel, should not be modified - System map: lookup-table matching symbol names - Linux kernel (vmlinuz): this is the OS proper - Initial RAM disk: (initramfs): contains minimal root file system, utilities and kernel modules so the kernel can load the real file system - Related files: `/boot/grub` #### GRUB 2 - Install using `grub-install` - List all partition of a disk `fdisk -l <disk name>` - Config Grub2: Make changes to `/etc/default/grub` and run `update-grub` - GRUB_DEFAULT: Default menu entry to boot, 0,1, ... - GRUB_SAVEDEFAULT: true and GRUB_DEFAULT is saved then the default boot option will always be the last one selected in boot menu - GRUB_TIMEOUT: timeout, 0: auto select the default, -1: wait forever - GRUB_CMDLINE_LINUX: command line options added for entries for Linux kernel - GRUB_CMDLINE_LINUX_DEFAULT: only added for default boot (not the recovery boot) - GRUB_ENABLE_CRYPTODISK: if set to y, command with `grub` will look for encrypted disks and encrypt them, need to give passphrase while booting ![](https://i.imgur.com/XjPIDAG.png) #### Interacting with GRUB2 - `e`: To edit a menu entry -> `Ctrl + X` or `F10` to boot - `c`: To enter shell - `help`: list commands - `ls`: show list of partitions and disks - `set root=<boot partition>`: set root for boot - `linux /vmlinuz root=<root partition>`: load the kernel - `initrd /initrd.img`: Load initial RAM disk - `boot`: Booting - Rescue boot: ![](https://i.imgur.com/bQFqDv0.png) #### GRUB Legacy - Install GRUB: `grub-install <boot device>` - Install GRUB from GRUB shell: `root (hd0,0)`, `setup (hd0)` - GRUB Legcay menu entries location: `/boot/grub/menu.lst` ![](https://i.imgur.com/YmeeKKX.png) ![](https://i.imgur.com/pKS9pbg.png) ## 102.3 Manage shared libraries #### Concept - Compiler turns code into object files, linker links them to libraries to generate executable file - Static libraries: Heavier but independent, libraries are embedded in program at link time, no depedencies needed at run time - Dynamic libraries (Shared): Reduce size of program, reduce memory,... #### Share object naming conventions - Known as `soname`: - Library name prefixed by `lib` - `so`: shared object - version - E.g: libpthread.so.0 - Static libs: `libpthread.a` ![](https://i.imgur.com/S1sNESk.png) #### Configuration of Shared Library Paths - The references are resolved by the dynamic linker `ld.so` or `ld-linux.so` - These linker search for libs in `library path`, these paths are configured in file `/etc/ld.so.conf` or `/etc/ld.so.conf.d` directory - These `*.conf` must contain absolute path of library paths - `ldconfig`: run when update library paths - `ldconfig -p`: print cache - `LD_LIBRARY_PATH`: temporary library path #### Searching for the Dependencies of a Particular Executable - `ldd <executable>` ## 102.4 Use Debian package management #### The Debian Package Tool (dpkg) - DPKG will not install and remove dependencies - `dpkg -i <package>`: install - `dpkg -r <package>`: remove - `dpkg -P <package>`: purge - `dpkg -I <package>`: info - `dpkg --get-selections`: get list of installed packages - `dpkg -L <package>`: get a list of files installed by package - `dpkg-query -S <file name>`: find out which package own the file - `dpkg-reconfigure <package name>`: backup old configurations and reconfigure the package by re run the post-install script #### Advanced Package Tool (apt) - APT will install and remove dependencies - `apt-get`: install, download, remove packages - `apt-cache`: search in the package index - `apt-file`: seach for files inside packages - `apt install -f`: try to fix the broken packages - `/var/cache/apt/archives`: .deb file are downloaded into this dir before installed - Sources list - APT uses a list of sources to store location of remote packages - `/etc/apt/sources.list` - A link: `<archive type> <URL> <distribution> <one ore more components>` - E.g: `deb <url> disco main restricted universe multiverse` - Components on Ubuntu and derivatives: - `main`: officially supported, open source - `restricted`: officially supported, closed-source like device drivers,... - `universe`: community maintained OSS - `multiverse`: unsupported, closed-source or patent-encumbered - Components on Debian - `main`: Debian Free Software Guidelines (DFSG) packages, part of Debian distribution (do not rely outside packages) - `contrib`: contains DFSG packages, but depend on other packages not in main - `non-free`: not DFSG - `security`: security updates - `backports`: more recent version of packages in main - `apt-file list <package>`: list files inside a package - `apt-file search <file name>` (search uninstall packages, `dpkg-query` only search installed packages) ## 102.5 Use RPM and YUM package management #### RPM - Similar to `dpkg` on Debian-based - `rpm -i <package>`: install .rpm file - `rpm -U <package>`: upgrade - `rpm -e <package>`: erase (remove) package - `rpm -qa`: list all installed packages (query all) - `rpm -qi <package>`: query info package (-p for not installed yet package) - `rpm -ql <package>`: list files installed by the package (query list) - `rpm -qf <file>`: which package own the file #### YUM - Similar to `apt` on Debian-based - `yum search <package>`: search for package name - `yum install <package>`: install - `yum update <package>`: upgrade a package (omit package name to upgrade every packages) - `yum remove <package>`: remove package - `yum whatprovides <file name>`: search for package provides the file - `yum info <package>`: get infor of a package - Software repositories: - Are listed in `/etc/yum.repos.d/` - `yum-config-manager --add-repo <url>`: Add a repo - `yum repolist all`: list list of repos - `yum-config-manager --disable <repo name>`: disable a repo - `yum-config-manager --enable <repo name>`: enable a repo - `/var/cache/yum`: store cache and metadata (`yum clean metadata|packages`) #### DNF - Used on Fedora - Is a fork of Yum - Commands a basically similar to Yum - `/etc/yum.repos.d/`: where list of repos stored #### Zypper - Used for SUSE Linux and OpenSUSE - Features are similar to `apt` and `yum` ## 102.6 Linux as a virtualization guest #### Virtualization overview - Guests use hard disk image that are stored as individual files, access to host's RAM and CPU through hypervisor software - Common hypervisor: - Xen: open source Type-1 hypervisor, does not rely on underlying OS, `bare-metal hypervisor`, the computer can boot directly into the hypervisor - KVM(Kernel Virtual Machine): Type-1 and Type-2, is a Linux kernel module for virtualization, vms deployed with KVM use `libvirt` daemon and associated software, needs a Linux OS, can run with a running Linux installation - VirtualBox: Requires an underlying OS, Type-2 - `migration`: move a vm from a hypervisor installation to another #### Types of VMs - Fully Virtualized: Guest doesnt aware that it is a VM ![](https://i.imgur.com/M5mCBRx.png) - Paravirtualized (PVM): Guest aware that it is a VM, make use of a modified kernel and `guest drivers` help utilize software and hardware resources, better performance than fully virtualized - Hybrid: Unmodified OS with paravirtualized drivers, enhance disk and network I/O performance #### Virtual Machine Disk Storage - COW (Copy on write): `thin-provisioning` or `sparse images`: disk file is created with pre-defined upper size limit, guest OS sees the disk is high but has written only to a little amount of disk - RAW: `raw` or `full`, the space is pre-allocated, better performance #### Working with VM Templates - VMs are only files running on a hypervisor - D-Bus Machine ID: A machine identification number generated at install time, a new DBMI must be created for a cloned VM on the same hypervisor - `dbus-uuidgen --get`: get DBMI (`/var/lib/dbus/machine-id` -> `/etc/machine-id`) - `dbus-uuidgen --ensure`: validate the DMBI exists ![](https://i.imgur.com/bTUWQa4.png) #### Deploying VMs to the Cloud - Elements that are important - Computing Instances: number of instances - Block Storage: amount of storage, speed of storage - Networking: network rules, subnets, firewall, DNS,... - Use SSH to connect to cloud instances - `cloud-init` uses `cloud-config` file to config cloud instances ![](https://i.imgur.com/9JTSAy1.png) #### Containers - Uses just enough software to run an application - Docker, Kubernetes, LXD/LXC, systemd-nspawn, OpenShift,... - Containers make use of control groups (`cgroups`), cgroup is a way to partition system resources such as memory, processer time, disk, network bandwith,... # 103 GNU and Unix Commands ## 103.1 Work on the command line - Some commands are hashed for fast searching - `set` print both variables (all) and functions ## 103.2 Process text streams using filters - `bzcat` for `bzip` and `xzcat` for `xz`, `zcat` for `gzip` compressed files - `sed -n /cat/p file.txt`: similar to `grep cat file.txt` - `-n`: produces no output (unless those with `/p`) - `/p`: instructed to print - `/d`: instructed to delete - `s/`: replace - `-i.backup`: create a file name `file.txt.backup` - `sed -n '10'p file.txt`: print line 10th - `sha256sum -c <hash file>`: check the integrity of the file - `od`: list file's contents in octal format - `od -x`: hexadecimal format - `od -c`: show printable and backslash characters - `od -An`: remove offset column ## 103.3 Perform basic file management #### Create and find files - `touch` can be used to change modification and access time of files and directories - `-a`: change access time to current - `-m`: change modifcation time to current - `find`: - `-mtime`: number of days the file was last modified - `-size`: +2G (larger), 100b (100bytes), +100k (larger 100kilobytes), -20M (smaller than 20M), 0b = `-empty` - `-exec <command> '{}' \;`: exec commands - `-print` - `-delete`: delete all matches ![](https://i.imgur.com/brpE9NT.png) #### Archive - `tar`: archive files - `-z`: gunzip files (same option when extracting) - `-j`: bzip files (same option when extracting) - `cpio` (copy in, copy out): copying and extracting files from an archive - `ls | cpio -o archive.cpio`: all files listed are output to archive.cpio - `cpio -id < archive.cpio`: extract - `dd`: copy data from one location to another - `dd if=oldfile of=newfile`: copy content of oldfile to newfile (`status=progress` to print progress) - `conv=ucase`: convert text to uppercase - `if=/dev/sda of=backup.dd`: backup the whole hard disk ## 103.4 Use streams, pipes and redirects #### Redirects - By default, errors appear when the target of redirect is read-only or a directory, to produce error when redirecting to an existing file: `set -o noclobber` or `set -C` (remove: `set +o noclubber` or `set +C`) #### Heredoc and herestring - Heredoc: `<<EOF` - Herestring: `<<< HERESTRING`, eg: `sha1sum <<< $USER` #### Pipes - `tee`: Capture the output but the output still go to stdout #### Command Substitution - `xargs`: use the contents it receives via stdin to run a given command with the contents as its argument - `-n <n>`: Run each command with n argument at a time - `-0`: Use null as the separator - `-I`: replace ![](https://i.imgur.com/LJu3qLY.png) ## 103.5 Create, monitor and kill process #### Job control - `jobs`: list background jobs - `-n`: jobs have changed status since the last notification - `-p`: list process IDs - `-r`: list running jobs - `-s`: stopped (or suspended) jobs - Job specifications: - `job %<number>`: list job with job id - `job %string`: commands start with string - `job %?string`: commands contain string - `job %+` or %%: current job - `job %-`: previous job - `fg` and `bg` will act upon the current if the job id is not specified, however the `kill` needs jobid - `nohup COMMAND &`: run the job without depending on the current session #### Process monitoring - `pgrep <string>`: get PID of progress contains string - `pidof <COMMAND>`: get PID of the progresses with command - `pkill <string>`: kill progresses based on string -> SIGTERM - `killall <COMMAND>`: kill all instances of a command -> SIGTERM - `top` command explain - CPU line: percentage of CPU usage - `us`: user - `sy`: system/kernel - `ni`: processes with nice value - `id`: idle - `wa`: waiting for IO operations - `hi`: serving hardware interrupts - `si`: serving software interrupts - `st`: serving VM task in virtual environments - Task line - `PR`: Priority - `VIRT`: total mem (include swap) - `RES`: RAM usage - `SHR`: shared memory - `S`: Status, S (interruptible sleep - wait for an event to finish), R (runnable - executing or in queue to be executed), Z (zombie - terminated child process but has not been removed from process table) #### GNU Screen - Basic commands - `Ctrl-a`: prefix - `Ctrl-a A`: rename window - `Ctrl-a n`: next window - `Ctrl-a p`: previous window - `Ctrl-a "`: list of all windows - `Ctrl-a k`: kill the windows - Splitting - `Ctrl-a S`: Split horizontally - `Ctrl-a |`: Split vertically - `Ctrl-a Tab`: Move between regions - `Ctrl-a Q`: Terminate all regions except the current - `Ctrl-a X`: Kill the current region - Sessions - `screen -list` (`screen -ls`) - `screen -S <PID> -X quit`: quit a session - `Ctrl-a d`: detach a session - `screen -r <PID or name>`: attach a session - Copy/paste: `Ctrl-a [` and `Ctrl-a ]` #### tmux - `tmux new -s <Session name> -n <Windows name>` - Mostly similar to `screen` ## 103.6 Modify process execution priorities - Processes use system calls to switch controlling right to system processes, then the OS decide to return CPU control to the previous process or hand it to another process -> If a process doesn't make system call, it can control the CPU forever -> modern OS are `preemptive`, more important processes can control CPU #### The Linux Scheduler - Decides which process or thread will be executed - Scheduling policies: `real-time` and `normal` - Processes under `real-time` policy are scheduled by their priority directly: High priority take control of low prioriy, low priority only take control back if the high priority process is idle or waiting for hardware response - real-time process has higher priority than normal process, Linux runs just a few of them -> `static priorities` - Normal processes have same priority value, but they can have the other value for defining priority `nice value` -> `dynamic priorities` #### Reading priorities - Static priorities - `real-time` processes: 0-99 - `normal` processes: 100-139 - `grep ^prio /proc/PID/sched`: get the static priority value - `ps -Al` or `ps -el`: show priority of all processes (+40) - `top` (+100) #### Process Niceness - Nice value 0 = priority 120 - Nice range: -20 -> 19 - Only root can decrease the nice value of a process below 0 - `nice -n <nice value> COMMAND` - `renice <nice value> -p <PID>` - `renice <nice value> -g <GROUP>` ## 103.7 Search text files using regular expressions #### Bracket expression - `[:alnum:]`: alphanumeric - `[:alpha:]`: alphabetic - `[:ascii:]`: ASCII characters - `[:blank:]`: space or a tab - `[:cntrl:]`: control character - `[:digit:]`: 0 - 9 - `[:graph:]`: printable character except space - `[:lower:]`: lower case - `[:upper:]`: upper case - `[:print:]`: any printable characters - `[:punct:]`: printable except space and alphanumeric - `[:space:]`: space, \f, \n, \r, \t, \v - `[:xdigit:]`: hexadecimal digits #### Quantifiers - Define matches occur - Two forms of regex: `basic` and `extended` - `*`: zero or more time (both forms) - `+`: one or more time - `?`: zero or once - `+` and `?` are literals atom in basic form, need to add \ to become quantifiers #### Bounds - `{i}`: i times - `{i,}`: >= i times - `{i,j}`: i <= and <= j - Longer match substring will be considered - `{` and `}` are literals in basic form, need to add \ to become bounds #### Branches and Back References - `|`: branches (literal in basic form) - `()`: back reference (literal in basic form) ![](https://i.imgur.com/xnNcoQd.png) #### Searching with regex ![](https://i.imgur.com/rMZC8eP.png) ![](https://i.imgur.com/pQ9TTaS.png) #### grep - `-c`: how many lines had matches - `-f`: file contain regex - `-i`: ignore-case - `-n`: show the line number - `-v`: invert-match - `-H`: print the name of the file - `-z`: null data as separator (`-print0` in `find`) - `egrep` is for using extended regex (`grep -E`) - `fgrep` does not use regex (literal form) (`grep -F`) #### sed - `sed 1d`: delete first line - `sed 1,7d`: delete 1-7 lines - `sed 1,7d;11d`: delete 1-7 and 11 line - Any thing between '/' is regex - `c`: replace a **line** (same as `r FILE`) ![](https://i.imgur.com/Mv7wu7N.png) - `s/A/B/`: replace A by B (`g` for every matches in a line) ## 103.8 Basic file editing ### Vim - `vim +9 <FILE>`: jump cursor to line 9 of the file - `vim + <FILE>`: last line #### Insert mode - `Esc` to escape, insert texts #### Normal mode (Command mode) - `0,$`: Go to beginning and end of line - `1G,G`: beginning and end of document - `(,)`: beginning and end of sentence - `{,}`: beginning and end of paragraph - `w,W`: jump word and jump word including punctuation - `e,E`: end of current word - `i,I`: insert before cursor or beginning of line - `a,A`: after cursor or end of line - `s,S`: erase the character under cursor or entire line to enter insert mode - `r`: replace character under cursor - `x`: delete character - `v,V`: start a new selection with current character or entire line - `u`: undo - `Ctrl-R`: redo - `ZZ`: Close and save - `ZQ`: close and not save - `vim` can organize copied text in registers, register is specified by a character `"`, `"ly` with current selection to copy into register l, then paste with `"lp` (within session) - `m` with a character to mark a postion, then use `'` with that character to come to the position - `q` with a character to record a macro, `q` again to exit then `@` with that character to play macro (within session) #### Colon commands - `:s/REGEX/TEXT/g`: accept same syntax as sed - `:!`: run shell command - `x`, `e` or `exit`: save and exit - `:visual`: go back to navigation mode # 104 Devices, Linux Filesystems, Filesystem Hierachy Standard ## 104.1 Create partitions and filesystems #### Understanding MBR and GPT - Storing in MBR or GPT - MBR: Partition tabel is stored on the first sector of a disk called `Boot Sector` along with GRUB, limitations: inability to address disks of more than 2TB in size, limit of 4 primary partitions per disk - GUID: UEFI use - MBR management with `fdisk`: - `fdisk <disk device>`: edit the disk - `p`: view partition table - Two types of partitions `primary` and `extended`, to make the device bootable, the first partition must be primary - `n` to create a partition - `F` to find unallocated space - `d` to delete partition - `t` to change partition type - GPT management with `gdisk` - `p` to view partition table, each disk has a unique GUID (128 bit hexadecimal) - Hold up to 128 entries - `n` to create new partition - `d` to delete partition, then use `s` to sort the partition, remove gap, partition is not limited by the maximum amount of contigous unallocated space #### Create File Systems - A file system control how the data is stored and accessed on the disk - Extended Filesystem (ext) was the first filesystem for Linux -> ext2, ext3, ext4. Use `mkfs.ext2`, `mkfs.ext3`, `mkfs.ext4` to create filesystem. All of them are symbolic link of `mke2fs` - `mkfs.ext2 <partition>` or `mke2fs -t ext2 <partition>` - `mke2fs` - `-b SIZE`: size of data blocks - `-c`: check for bad blocks before creating the filesystem, twice for better check - `-d DIRECTORY`: Copied the contents of the specified directory to the root of the new partition - `-F`: force - `-n`: trial mode, safe - `-U ID`: set the UUID - `mkfs.xfs`: to create XFS file system - `mkfs.fat` and `mkfs.vfat - exFAT: maximum file size is 16 exabytes (4GB in FAT32), maximum disk size is 128 petabytes, supported by MacOS, Linux and Windows: `mkfs.exfat` - `Btrfs`: B-Tree Filesystem, specifically for Linux, copy-on-write, simple to use, default fs on SUSE: `mkfs.btrfs` - Subvolumes are filesystems inside filesystems, they are directories which can be mounted as a separate filesystem - `btrfs subvolume create /mnt/disk/BKP`: create subvolume - `mount -o btrfs -o subvol=BKP /dev/sdb1 /mnt/bkp` - Snapshots are like subvolumes, they points to the original data - `btrfs subvolume snapshot /mnt/disk /mnt/disk/snap`: create snapshot of the Btrfs filesystem /mnt/disk (`-r` for read-only) #### Managing partitions with GNU Parted - `parted DEVICE`: start parted program - `select DISK`: change to another disk - `print`: get information of current disk - `print devices`: get list of all connected devices - `print free`: print free space - `mklabel msods|gpt`: create partition table - `mkpar PARTTYPE FSTYPE START END`: create partitions ![](https://i.imgur.com/aLQBnFg.png) - `rm <INDEX OF PARTITION>`: remove partition - `rescue START END`: recover deleted partition (only on partition has filesystem) - `resizepart <INDEX> <END>`: partition must be unmounted and unused, there is enough space after it -> `resize2fs DEVICE SIZE`: resize filesystem (if omit SIZE, use all available) #### Creating swap partitions - Like creating a new partition with `fdisk` or `gdisk` but with type Linux swap ![](https://i.imgur.com/0MiMHvn.png) - `mkswap <PARTITION>`: Create swap file system - `swapon PARTITION`: enable swap (`swapoff`) ![](https://i.imgur.com/2Bo8sAg.png) ![](https://i.imgur.com/9ltvZ96.png) ## 104.2 Maintain the integrity of filesystems - Modern Linux systems are journaled (every operation is registered in an internal log - the journal) before it is executed, if the operation is corrupted, it can be reconstructed by checking the journal, avoid corruption + data lost #### Checking Disk Usage - `du`: disk usage - `-h`: human readable - `-a`: show all files and diretories (default only directories) - `-S`: space of files (exclude directories) - `-c`: for space of all, and space of files (-S) - `-d NUM`: depth - `--exclude="PATTERN"` #### Checking for Free Space - `df`: disk free - `-h`: human readable - `-i`: inodes instead of blocks - `-T`: print type of the system - `-t TYPE`, `-x TYPE`: show only or exclude file system type - `--output FIELDS`: filter outputs #### Maintaining ext2, ext3, and ext4 Filesystems - To check a filesystem for error use `fsck` - `fsck DEVICE`: never run on mounted filesystems - `-t`: type of filesystem - `A`: check all fs in /etc/fstab - `-N`: dry run - `-R`: used with -A, skip checking root system - `e2fsck`: used for ext fs family - `-p`: fix any errors found - `-y`: answer anything yes - `-n`: opposite to -y - `-f`: check for a fs even if it is clean - `tune2fs`: used for tuning ext family fs - `tune2fs -l PARTITION`: see current parameters - `mount counts`: parameter, each time the fs is mounted, it is increased by 1, when reach a threshold, `e2fsck` will be run on next boot - These parameters can be edited by `tune2fs` - `-e BEHAVIOUR`: behaviour when fs error is found, `continue`, `remount-ro`, `panic` - ext3 are ext2 with a journal - `tune2fs -j PARTITION`: add journal, convert ext2 to ext3 - `xfs_repair`: similar to `fsck` for XFS - `xfs_repair -n PARTITION`: check, no modified - `xfs_repair PARTITION`: repair - `xfs_db`: debug an XFS fs ## 104.3 Control mounting and unmounting of filesystems - Partition disks -> maintain filesystems, but before fs is accessed, it must be mounted -> attaching the fs to a specific point on system's directory tree, called a `mount` point #### Mounting and Unmounting Filesystems - `mount -t TYPE DEVICE MOUNTPOINT` - `mount -t btrfs /dev/sdb1 /mnt/test`, mountpoint need not be empty, but exist, any files in will be inaccessible when mounting - `mount -t TYPE`: list of mount with type - `-a`: mount all fs listed in /etc/fstab - `-o`: mount option - `-r` or `-ro`: read only FS - `-w` or `-rw`: writable FS - `umount DEVICE|MOUNTPOINT`: unmount a FS - `-a`: unmount all fs in /etc/fstab - `-f`: force - `-r`: if the FS can not be unmounted, make it read-only - `lsof DEVICE`: list of processes access it and files are open - Good practice to mount FS under `/mnt` #### Mounting Filesystems on Bootup - `/etc/fstab`: contains description about the FSs that can be mounted ![](https://i.imgur.com/gtnpeQv.png) - Description - `DUMP`: ext2, ext3, or ext4 FSs should be considered for backup by the `dump` command, 0 for ingnoring - `PASS`: For non-zero, defines the order which the FS will be checked on bootup - Mount options - `atime` and `noatime`: disable access time for a file with noatime - `auto` and `noauto`: mounted automatically with `mount -a` - `defaults`: `rw,suid,dev,exec,auto,nouser,async` passed to `mount` - `dev` and `nodev`: character or block devices in the FS should be interpreted - `exec` and `noexec`: allow permission to execute binaries on FS - `user` and `nouser`: allow an ordinary user to mount the system - `group`: user in the own group of the device can mount - `owner`: user own the device -> can mount - `suid` and `nosuid`: allow SETUID and SETGID - `ro` and `rw` - `remount`: not used in /etc/fstab, used for parameter of `mount -o`, remount the FS - `sync` and `async`: IO operations #### Using UUIDs and Labels - Using UUID to identify each device - `lsblk -f DEVICE`: query FS, find label, UUID - Can use labels or UUIDs in `mount` command or `/etc/fstab` file #### Mounting Disks with Systemd - Systemd can manage mounting of FSs, need to create a mount unit, place in `/etc/systemd/system` ![](https://i.imgur.com/I8prm9F.png) - The mount unit must has the same name as the mountpoint: `/mnt/external` -> `mnt-external.mount` - `systemctl daemon-reload`, `systemctl start mnt-external.mount` ![](https://i.imgur.com/zb6hImR.png) ## 104.5 Manage file permissions and ownership - The second column in `ls -l` is the number of hard links - `ls -d DIRECTORY`: list the directory itself (not inside) - File types: ![](https://i.imgur.com/V1AKPxL.png) #### Permissions on directories - `r`: read filenames, not the files - `w`: create or delete files in directories (neex `x` too) - `x`: enter the directory, but can't list file (need `r`) #### Groups - `getent group`: querying groups - `groups USERNAME`: which group user belongs to - `groups -g GROUPNAME -l`: list users of group #### Default permission - Octal = 777 - `umask` #### Special permissions - Sticky bit: `restricted deletion flag`, octal value 1, represent by `t` in other's permission, applied only to directories, prevent user from removing or renaming a file in a directory unless they own that file or directory ![](https://i.imgur.com/TVj9TBK.png) - Set GID: octal value 2, represent by `s` on group permissions, applied to executables or directories, make exes run the privileges of the group owner, file or directory created in directory under inherit the group - Set UID: octal value 4, represent by `s` on user permissions, applied on files only, run under privileges of owner user ## 104.6 Create and change hard and symbolic links - Two kinds of links: - Symbolic links (soft links): point to other files, stop working if the original file deleted - Hard links: second name for the original file, additional entry in the fs pointing to the same place (inode) - `ln TARGET LINK_NAME`: create hard links, when delete a file, an entry is deleted but not the data, so with hard links, you can access to it from other entries, target and link must both be in the same FS, hard links are only for files, just like duplicate a file but with same data pointing - `ln -s TARGET LINK_NAME`: create soft links, can be applied to files and dirs, the path should be absolute for moving, can use between partitions, soft links point to names (not inode), soft link doesn't increase link count ## 104.7 Find system files and place files in the correct location - All Linux distribution follow `Filesystem Hierachy Standard (FHS)`, a standard layout for fs #### The Filesystem Hierachy Standard - Linux Foundation standardize the structure and contents of directories in Linux - `/`: root directory - `/bin`: essential binaries, for all users - `/boot`: files needed for boot process, including initial RAM disk and Linux kernel - `/dev`: device files - `/etc`: configuration files - `/home`: user data - `/lib`: shared libraries - `/media`: user-mountable removable media, flash drives, CD, DVD-ROM,... - `/mount`: mount points for temporarily mounted filesystems - `/opt`: application software packages - `/root`: root's home - `/run`: run time variable data - `/sbin`: system binaries - `/srv`: data served by the system - `/tmp`: temporary files - `/usr`: read-only user data - `/proc`: processes data - `/var`: variable data written during system operation - Temporary files: ![](https://i.imgur.com/K47IABh.png) #### Finding files - `locate -A PATTERN PATTERN ...`: show all matches for all patterns - `locate -c PATTERN`: show matches count - `updatedb`: database for `locate`, configuration file located at `/etc/updatedb.conf` - `PRUNEFS`: ignore filesystem types - `PRUNENAMES`: ignore directory names - `PRUNEPATHS`: ignore paths - `PRUNE_BIND_MOUNTS`: yes|no, `mount --bind` will be ignore if `yes` - `which -a COMMAND`: show all matches - `type -a COMMAND`: show all matches - `type -t COMMAND`: show type of commands