Linux Knowledge Tree
in Coding
Knowledge tree of Linux
This winter break is one of the 2 breaks that I didn’t back to China in 7 years. I have some extra long time sitting in front of my computer. I think this is a good time for me to spend some effort to have a better knowledge of Linux. The learning resource I mainly rely on is 鸟哥的 Linux 私房菜. And my note will also reference the website.
- Computer Architecture
- Linux Intro
- Linux Install
- File Permission
- Linux File System
- Compress File
- vim
- bash
- Regex
- Shell script
- Account management
- Crontab
- Process and SELinux
- Kernel Compile
- Useful Command
Computer Architecture
- RISC and CISC: RISC (e.g. ARM) contains reduced small instructions and CISC (x86) contains multiple low level instructions.
- NorthBridge connect to: CPU, RAM, GPU SouthBridge connect to: Hard drive, USB, Internet card
- Clock multiplier: Internal CPU clock rate / external supplied clock rate. Usually fixed. Overclocking is increase the external clock rate, so that internal CPU clock rate increase
- 32-bit and 64-bit: The length of the RAM address that the CPU can handle. Each address corresponding to a byte, so that 32-bit machine can only have 4GB at most.
- Dual Channel RAM: So that the RAM Bandwidth is 128-bit. Notice that the RAM also will be partitioned into 2 part. And each memory operations will be read/write to both partition.
- DRAM and SRAM: DRAM is for RAM while SRAM is for L2 cache
- Hard Drive: Sector (512bytes), Track, Cylinder
- Operation System roles:
- System call interface
- Process control
- Memory management
- FileSystem management
- Device driver
Linux Intro
- UNIX: Everything is a file
- BSD: Berkeley Software Distribution, FreeBSD is a distribution of BSD
- GNU: GNU is Not UNIX. Free software running on UNIX. Emacs, GCC & GLIBC, Bash shell
- GPL: GNU General Public Liscense. Comparison of free and open-source software licences - Wikipedia
- POSIX: Portable Operating System Interface. Interface between OS kernel and application
- Linux: Unix-like system which based on POSIX. Version number: Odd number it tip branch Even number is stable branch.
Linux Install
- Disk Partition: First sector in hard disk contains 2 data: MBR (Master Boot Record) 446bytes and Partition Table 64bytes.
- Primary Partition, Extended Partition, Logical Partition: We can have maximum 4 Primary + Extended. We can only have 1 Extended. And the Extended partition can be split to Logical Partitions (/dev/hda[] index start from 5). SATA hard drive can only have 11 Logical Partition to the most.
- Boot sequence: BIOS -> MBR -> Boot loader -> OS kernel
- Boot Loader: installed on MBR by OS
- Provide boot options menu
- Load kernel files from the boot sector in the same partition
- Redirect to other boot loader (Windows) if needed
- mount: mount a partition to the OS. Different folder under the same directory tree can be mounted by different partitions. Manually mount will be load to /mnt/myFile
- Grub is the default boot loader for most distribution
- Swap: Allocate disk space for temporary store less frequent used RAM data.
- ext4: Linux journaling file system
- switch tty and GUI: [Ctrl] + [Alt] + [F1]~[F7]
- runlevel: set OS running mode, e.g. with GUI or not
File Permission
- Overview: Linux File Properties
- Command for updating file permission:
- chgrp: change file user group
- chown: change the owner of the file
- chmod: change the permission of the file Only the user currently has the permission of ‘w’ can exec the above commands
- When assign permission to new user, we should be very careful for the ‘w’ permission
- File type: Linux File Types and Extensions
- Linux File Hierarchy Standard:
- /: root directory
- /usr: UNIX software resource, store software
- /var: store file for runtime
- /media and /mnt: for mount external storage devices. /media for long term mount and /mnt for temporary mount
- /proc: virtual filesystem that map system memory data into file system
- /etc, /bin, /dev, /lib, /sbin: these 5 folders must be in the same disk partition as root directory
- /usr/local: user installed software
- Others: Linux FHS
- SUID, SGID, SBIT: When running an executable that own by others and have set the SUID permission, the user is temporary grant the same permission as the owner. E.g. any user can execute passwd
Linux File System
- Hard drive basics: Hard drive basics
- Traditional file system and software raid:
- Traditional File system: each partition can only be formatted to 1 file system
- Software Raid: 1 partition can be divided into multiple file system and multiple partition can together form a file system
- Indexed Allocation (EXT2)
- Super block: record the overall info of the file system
- inode: Each inode record the indexing of block for a file, 1 inode can map to multiple blocks. 128bytes
- block: minimum storage unit for the file system, size can be 1KB/2KB/4KB. Block can also be used as indirect extension when storing big file
- FAT (File Allocation Table): used by usb drive
- No inode
- block connection are stored within block like linklist
- Fragmentation: the blocks for a file can be very scatter. So need to rearrange the blocks
- Journaling File System (EXT3): Every time when need to update a file, first write the log to the journal system, so that if the actual write fail, we can always recover from log
- When create a new folder, we will be assign an inode and 1 block, that is why usually folder size is 4096. Also if too much file, might contain more blocks
- Virtual FileSystem Switch: an interface layer between the OS and the actual file system. So that Linux can support multiple types of File system.
- Link:
- Hard Link: file name link to the same inode
- Symbolic Link: file name link to different inodes
- Swap: Use disk partition as RAM to store temporary not used memory data.
Compress File
- Compress tool:
- gzip: extension is .gz
- bzip2: extension is .bz2
- tar: modern compress tool
- Backup
vim
bash
- We can defined what type of shell to be launch for different users in /etc/passwd
- Sub-shell cannot use the variables defined in parent shell. But they can be used after export to env
- When assign variable:
- “$VAR” will use the actual value of VAR
- ‘$VAR’ is just $VAR, ‘ means escape all
- Also if running command, the content in ‘’ will be run first: ls -l ‘locate crontab’
- bash has 7 ttys, one of them is GUI
- login shell and non-login shell
- login shell: the first shell that require login
- non-login shell: the shell launched after login shell. No need to login, but will not source /etc/profile
- Sequence of sourcing variables: bash
- Bash special char: bash
- Data redirection, will redirect such channel into file, etc.
- stdin: 0
- stdout: 1
- stderr: 2
- Pipe:
- Only redirect the stdout, ignore stderr
- The receiver command must able to receive pipe input
Regex
- Wildcard and regex: wildcard is an bash interface that used by regex
- regular expression
Shell script
- #!/bin/bash
- Different way of executing shell script
- ./script: run the script under a sub shell process, the variable will not be export
- source script: run in parent shell process
- Shell Scripts
- Shell parameters:
- $#: number of arguments
- $@:
- $*: concatenate arguments to string
- shift: shift argument index
- Functions: usually running in parent shell process, unless:
- Run in background with &
- Run in a pipe
- Static syntax check: sh -n script.sh
- Verbose run: sh -x script.sh
Account management
- UID
- root user: 0
- system account: 1 - 499
- user: 500 - 65535
- su: change account. su -: root
- sudo: grant running root command to common user. Need to set account in /etc/sudoer
- …
Crontab
- Set recurrent tasks for server machine, when get killed, will restart later
- at: execute once
- ate/atrm: remove scheduled task
- batch: execute only when OS is not busy
- anacron: handle recurrent task when machine is not always on.
Process and SELinux
- UID: user ID. GID: user group ID
- Thread ID and Process ID:
- PID is generated through fork, OS determine permission grant via PID
- TID(PID) is generated within a process. It will also be assigned a PID. Thread and process are treated equally in linux.
- PPID: parent PID.
- TGID: PID of the first thread that created in a process
- bash PID is the PID for the started process
- PID
- fork-and-exec: when fork will generate a temporary process exactly as the parent. The only different is the PID and the PPID. Then exec the actual program
- The first process of Linux OS PID is 1
- daemon: service running in background
- Most daemon start script is at /etc/init.d/
- job control
- &: throw job to background
- jobs: list background job number
- fg: bring background job to front
- bg: run the paused background job
- kill: -9 kill abnormal job, -15 normal stop a job
- kill -9 %jobnumber
- killall: kill all process under a parent process
- nohup: run task when log out
- Signals:
- SIGHUP: restart
- SIGINT: ctrl+c
- SIGKILL: kill
- SIGTERM: normally stop
- SIGSTOP: ctrl+z pause
- CPU scheduler:
- PRI: dynamic priority that only can be updated by OS, the lower the higher priority
- PRI = PRI + nice
- nice can be updated by user.
- Root can set nice from -20 ~ 19
- user can set nice from 0 - 19 and user can only set nice more and more higher
- command: nice and renice
- /proc: map the OS related memory into file
- SELinux:
- Subject: process
- Object: resource that need to be access
- Policy: policy
- Enforce: restrict. Permissive: only show warning
- setenforce
- SELinux
Kernel Compile
Useful Command
- Command List, Command Cheatsheet
- startx: start X window from tty1-6
- bc: calculator
- Set Locale: LANG=en_US.UTF-8
- sync: dump data in RAM to disk
- Shutdown: shutdown -h now
- su - [username] : change account
- basename/dirname: obtain the file name or path
- Print file content:
- cat/tac
- nl
- more
- less
- head
- tail
- od: read the file in binary format
- umask: remove the default permission for a created file
- file: print meta data of a file
- Find files:
- which: find executable in PATH
- whereis: find file
- locate: find file
- find: find file, slow
- type: find file
- File system:
- df: read from superblock
- du: read from actual file, slow
- link:
- ln: hard link
- ln -s: Symbolic Link
- disk:
- fdisk: edit disk partition
- mkfs/mke2fs: format disk
- fsck/badblocks: disk check
- mount: mount a media/partition to a specific folder
- umount: remove media device
- Other command for setting the disk parameters: mkond, e2label, tune2fs, heparm
- Manage the boot default mount medias: Edit /etc/fstab
- dd: create an empty file. Also can copy files from disk sector
- free: display memory usage
- Compress:
- compress/gzip/zcat/bzip2/bzcat: mainly for compressing a single file
- tar:
- compress: tar -jcv -f filename.tar.bz2
- check: tar -jtv -f filename.tar.bz2
- extract: tar -jxv -f filename.tar.bz2
- Backup:
- dump: back file system
- restore: restore file system
- cpis: backup tool that work with find command
- CD/DVD write:
- mkisofs: create iso file for content
- cdrecord: disk write tool
- Env variables:
- env: get default variables
- set: get custom variables under bash
- unset: release a variable
- PS1: set the primary prompt variable
- Reserved variable:
- echo $$: print PID
- echo $?: print last command result
- !!: exec last command
- locale: set the language and text encoding
- Variables:
- read: read keyboard input to a variable
- array: declare an array
- declare/typeset: declare variable
- ulimit: set system usage limit
- ulimit -c unlimited: enable core dump
- Modify Var:
- #, ##, %, %%, /, //
- username=${username-root}: if username not set then set to root
- alias rm=‘rm -i’
- source == .
- cat a.log » b: no override original b
- cmd > /dev/null 2>&1: redirect everything
- 2> will redirect the stderr
- &1 will redirect the stdout
- sort related:
- sort
- wc: count
- uniq: count unique
- tee: direct data to both screen and to file
- char update command: tr, col, join, pastes, cut, expand
- split: split file
- xargs: when piping, create input pipe for next stage, incase some command cannot take pipe
- -: single dash means waiting for stdin
- –: double dash means any arguments after the – are treated as filenames and arguments
- Text editing
- nl: number of lines
- sed: handling per line
- awk: handler base on delimiter
- egrep: support extended regex format
- diff tool:
- diff: compare per line
- cpp: compare per byte
- patch: update the file with diff patch
- process:
- ps aux: check all process
- ps axjf: check process tree
- ps -l: only check bash process
- pstree
- top:
- -d: set interval
- 1: display all cores
- i: only display active tasks
- M: rank by memory usage
- free -m: check memory usage
- Clean up memory
- Clear page check only: sync; echo 1 > /proc/sys/vm/drop_caches
- Clear inodes: sync; echo 2 > /proc/sys/vm/drop_caches
- Clear all: sync; echo 3 > /proc/sys/vm/drop_caches
- uname: check kernel info
- dmesg: check kernel generated info
- uptime: check start up time
- netstat: network info
- fuser: check the program that currently using the file
- isof: list the file that opened by a process
- ldd: list the shared lib that used by a program