Course Content#
Directory Structure#
-
-
-
The top root is the root directory /, not the root user.
-
In addition, there is a root folder under the root directory /, which is the home directory of the root user.
- Except for the root user, other users cannot access it.
-
bin, sbin binary/[system]
-
etc configuration folder [global]
-
opt optional, not very meaningful, users can choose to place files there
-
run currently running files, generally not concerned
-
var dynamic data
- mail system notification mechanism, not e-mail
- log logs [important, do not modify]
- Viewing information usually requires sudo privileges, usually need to check the location of errors and warnings
- The last displayed login information is stored in the wtmp file
-
boot kernel files, related to startup
-
home parent directory of all user homes
- User homes are all under home, with paths ${HOME} or ~
-
lib, lib64 libraries
-
media media [used for mounting floppy disks and optical drives in the early days, not for USB drives, which are used less now]
-
mnt mount directory
- Mount other file systems, such as USB drives, CD... drives in WSL
- Conventional mount points, not automatically mounted, can also have mount points at any other location
-
proc processes
- After checking the corresponding process ID through ps, you can find the corresponding directory with the ID in the proc directory
- Most of the files in the directory are 0KB because they are not real, they are just for display
- Among them, there is a fd directory → file descriptor
-
- You can enter abc into stdout[#1], and it will be displayed on the terminal
- Ignore what 10 is for now
-
-
tmp temporary folder
- It will be cleared when the system shuts down, not a buffer
- [PS] tmpfile command can create temporary files, refer to man tmpfile
-
dev device files
-
usr files installed by users
- Generally, it is the administrator user, ordinary users cannot install software
- local shared files [manuals...]
Startup Process#
- 👉Press the power button
- 👉Load BIOS: hardware detection [CPU, disk, memory, etc.], read configuration [system installation location, etc.], until MBR is read
- BIOS-Basic Input/Output System, firmware based on CMOS chip
- Firmware is based on software and hardware, and some programs are solidified [burned] on a chip
- Always powered [button battery], initialized when power is lost
- BIOS-Basic Input/Output System, firmware based on CMOS chip
- 👉Read MBR in the first bootable device [primary boot partition]
- Stores the boot program Boot Loader
- Load kernel: detect hardware, load drivers
- At this time, the kernel takes over the work of BIOS
- 👉Execute the boot program [Ubuntu: Grub]
- Load the virtual file system and start the kernel
- 👉Initialize the disk, read the system image file
- 👉Start process 1
- Formerly called init, now called cnd
- At this time, the system has its own functions
- 👉Load the disk, mount data, start various services, connect to the terminal
- 👉[Finally] Run the x windows system→with a graphical interface
Run Levels#
In Linux, different run levels correspond to different services to start the system.
run level | meaning | remarks |
---|---|---|
0 | halt | Shut down the system |
1 | single user mode | Used for maintenance when the system has problems, similar to safe mode |
2 | multi-user, without nfs | nfs: network file system |
3 | full multi-user mode | Complete multi-user pure text mode [commonly used] |
4 | unused | Reserved by the system, not used |
5 | X11 | Load X windows based on run level 3 |
6 | reboot | Restart |
- Development of startup methods
- System V [start one by one]
- Upstart [group start, services without dependencies can start at the same time]
- Systemd [all services start together, services with dependencies are slightly delayed, further improving concurrency]
- The startup speed of the system does not represent the performance of the system. For example, on large machines, restart once every few years, and you need to check the hardware carefully.
- Hot start: Skip the hardware detection process of BIOS without necessary, and speed up the startup process
Configuration Files#
File System#
- /etc/fstab File systems mounted at startup
- Static file system information that indicates the system disk to be mounted
- For example, if you need to mount another disk for a company machine, you can set it here
- /etc/mtab Currently mounted file systems
User System#
- /etc/passwd User information
- Not only ordinary users, but also many system users
- User placeholder:uid:gid description directory shell
- The password placeholder used to store passwords in the past, is now stored in the following👇
- /etc/shadow User passwords
- The displayed passwords are encrypted, and the cost of decryption may be greater than the value obtained
- /etc/group Group information
- /etc/gshadow Group passwords
- Basically not used, root can manage the entire system, no need to divide into groups
- /etc/sudoers Sudoer list
- % represents a group
- Can be added by yourself
Shell#
- echo ${SHELL} View the shell type of the user
- /etc/shells List of available shells
[For zsh]
- /etc/zsh/zprofile User preferences [global]
- ⭐Search in man zsh - FILES to see all the file paths that can be configured for zsh
- For example, echo "In ..." in 4 global configurations and 4 user configurations
- When the user logs in to zsh, the display order is as follows:
-
- Order: zshenv → zprofile → zshrc → zlogin, first global (G), then user (L)
- [PS] User configuration files
- $ZDOTDIR/.zshenv
- $ZDOTDIR/.zprofile
- $ZDOTDIR/.zshrc
- $ZDOTDIR/.zlogin
- $ZDOTDIR/.zlogout
- $ZDOTDIR-defaults to the user's home directory
System Environment#
- /etc/environment Environment variables
- PATH environment variable: ${PATH}
- The which command will find the corresponding file of the command in the paths in this [PATH environment variable] [the file needs to be executable]
- ⭐Add the path "." to PATH: PATH=${PATH}:.
- You can directly add :. after PATH to connect
- You can also use export PATH=${PATH}:.
- ❗ But both methods modify in memory, and they will disappear after reconnecting the shell
- See the next 2 sections for details-environment variables
- /etc/updatedb.conf File retrieval database configuration information
- updatedb updates the database [after the update, newly created files can be found by locate, the database is not updated in real time]
- /etc/issue, /etc/issue.net Distribution information, [displayed when logging in remotely]
- /etc/os-release More detailed system information
Network#
- ⭐/etc/hosts Host list
- Also known as static DNS, you can write the corresponding relationship
- ❗ Domain name [hostname] → IP address
- [My understanding] Similar to the hosts in Windows, when resolving a name, it first looks for hosts and then uses DNS
- /etc/hostname Hostname
- ①Need sudo privileges to modify→new hostname
- ②Restart the host; or use [hostname new hostname] to temporarily modify the hostname in memory, otherwise the hostname in the environment will not change after the modification
- ③Then reconnect
- [My understanding] The first step changes the real hostname, and the second step hostname is used to temporarily modify the hostname in memory, and directly restarting can also refresh the memory
- ⭐/etc/resolv.conf Domain name resolution server address
- DNS Dynamic Domain Name Server
- ❗ Domain name → IP address
- /etc/network/interfaces Network card configuration file, check it yourself first
Environment Variables#
- env All environment variables, usually named in uppercase letters
- Call by adding $ and the name of the environment variable
- PATH executable folder path
- OLDPWD last working directory
- HISTSIZE size of saved history [input command]
- bash defaults to saving 1000 lines
- zsh does not have this variable temporarily
- You can view the input command history through history, and search for incognito browsing
- export
- export PATH=${PATH}:.
- Equivalent to PATH=${PATH}:.
- Compared with directly assigning a value to an environment variable, export is more standardized
- This is a modification in memory. If you want to solidify the variable and make it effective every time you connect, you can add the above statement to any [configuration file]. For zsh configuration files, see the previous 3 sections-Shell
- [PS] You can use set to view all locally defined environment variables
Software Management#
- Source code installation
git clone XXXX # Download the source code
cd XXX
make # Compile
make install # Install
- Software package management system
- ⭐Recommended apt: It can be understood as a more advanced integrated version of apt-get and apt-cache
- man apt can see its description: advanced, better interactivity than apt-get and apt-cache
- Example:
In-Class Exercises#
Code Demonstration#
Additional Knowledge#
- Variables declared and defined in the shell are stored in memory, in the process space. When the shell is closed and reopened, the variables are gone.
- Don't overinterpret the concept of "everything is a file". Files are static and need processes to maintain them and make them dynamic.
- Variables are stored in the process space.
- ps can view the process ID
- ⭐Use ssh to log in to a cloud host with an alias, such as ssh Ten. The effect on WSL is as follows👇
-
- For WSL or MacOS
- ①Add the IP of the cloud host to /etc/hosts on the local machine, corresponding to an alias Ten, such as: 45.123.111.1 Ten
- ②Create a user on the local machine, with the same name as the user of the cloud host, such as: hz [local user], hz [cloud host user]
- ③At this time, ssh Ten on the local machine, enter the password to log in
- ❗ Invalid after restart [for WSL 2, search for other methods for other systems, WSL 1 cannot chattr]
- Use sudo chattr +i /etc/hosts to make /etc/hosts read-only, and it will not be reset after restarting
- If you want to modify it again: sudo chattr -i /etc/hosts
- [PS] You can also use ssh-copy-id for passwordless login, tldr ssh-copy-id can view the help description
-
- Domain names and IP addresses can be many-to-many, and can be used for load balancing
Points to Consider#
Tips#
- [Under bash] /etc/profile User preferences [global] [When the default shell is bash, this is the first file to run]