Fourth Chapter Initial Entry into the Linux World
"Linux system boot Process "
Linux starts up in a similar way to the Windows startup process, but Windows doesn't see the boot information, and when Linux starts we see a lot of startup information, such as whether a service is started or not.
The boot process of Linux system can be divided into five parts: kernel boot, running init, system initialization, terminal, user login system.
A Kernel boot
When the computer is powered on, it is the BIOS post that starts with the boot device (usually the hard disk) set up in the BIOS. After booting Linux from the GRUB program on the boot device, Linux takes control of the CPU from their hands after the boot successfully completes the boot task, and then the CPU starts executing the Linux core Image code and starts the Linux boot process. That is, the so-called kernel boot started, in the kernel boot process is actually very complex, we will be when it is a black box, anyway, the Linux kernel does some column work, finally the kernel calls the INIT program loaded, so the kernel boot work is done. To the next protagonist, Init.
B Run Init
The init process is the starting point for all processes in the system, and you can compare it to the ancestor of all processes in the system, without which any process in the system will not start. The INIT program first needs to read the configuration file/etc/inittab. Inittab is a non-executable text file that consists of several lines of instructions. The details are as follows: (You can execute commands on your Linux cat/etc/inittab to get it)
# Inittab This file describes how the INIT process should set up
# The system in a certain run-level.
#
# Author:miquel van Smoorenburg,
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# Default RunLevel. The runlevels used by RHS is:
# 0-halt (do not set Initdefault to this)
# 1-single User mode
# 2-multiuser, without NFS (the same as 3, if you don't havenetworking)
# 3-full Multiuser mode
# 4-unused
# 5-x11
# 6-reboot (do not set Initdefault to this)
#
# # #表示当前缺省运行级别为5 (Initdefault);
Id:5:initdefault:
# # #启动时自动执行/etc/rc.d/rc.sysinit script (sysinit)
# System initialization.
Si::sysinit:/etc/rc.d/rc.sysinit
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
# # #当运行级别为5时, run the/ETC/RC.D/RC script with 5 as the parameter, and Init will wait for it to return (wait)
L5:5:WAIT:/ETC/RC.D/RC 5
L6:6:WAIT:/ETC/RC.D/RC 6
# # #在启动过程中允许按CTRL-alt-delete Restart System
# Trap Ctrl-alt-delete
Ca::ctrlaltdel:/sbin/shutdown-t3-r now
# when our UPS tells us Power had failed, assume we have a few minutes
# of Power left. Schedule a shutdown for 2 minutes.
# This does, the course, assume you have Powerd installed and your
# UPS connected and working correctly.
PF::p owerfail:/sbin/shutdown-f-H +2 "Power Failure; System shutting down "
# If Power was restored before the shutdown kicked in, cancel it.
Pr:12345:powerokwait:/sbin/shutdown-c "Power restored; Shutdown Cancelled "
# # #在2, 3, 4, 5 level on the Ttyx as the parameters to execute the/sbin/mingetty program, open Ttyx terminal for user login,
# # #如果进程退出则再次运行mingetty程序 (respawn)
# Run Gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty Tty2
3:2345:respawn:/sbin/mingetty Tty3
4:2345:respawn:/sbin/mingetty Tty4
5:2345:respawn:/sbin/mingetty Tty5
6:2345:respawn:/sbin/mingetty Tty6
# # #在5级别上运行xdm程序, provide XDm graphical login interface, and re-execute on exit (respawn)
# Run XDm in RunLevel 5
X:5:respawn:/etc/x11/prefdm-nodaemon
Take the Inittab file above as an example to illustrate the format of Inittab. Lines where # begins with # are comment lines, and each row has the following format in addition to the comment line:
Id:runlevel:action:process
The detailed explanations of the above items are as follows:
1. Id
The ID refers to the entry identifier, which is a string, and for other login program items such as Getty or Mingetty, the ID is the same as the TTY number, otherwise the Getty program will not function properly.
2. Runlevel
RunLevel is the identity of the runlevel at which Init is located, typically using 0-6 and s or S. 0, 1, 6 operation level is reserved by the system: 0 as Shutdown action, 1 as restart to single-user mode, 6 for restart, s and s mean the same, the single-user mode, and no need to inittab files, and therefore does not appear in the Inittab, in fact, into the single-user mode, Init runs/sbin/sulogin directly on the console (/dev/console). In the general system implementation, the use of 2, 3, 4, 5 levels, in the CentOS system, 2 represents a multi-user mode without NFS support, 3 is a full multi-user mode (also the most commonly used level), 4 reserved for user-defined, 5 represents the XDm graphical login method. 7-9 levels are also available, and traditional UNIX systems do not define these levels. RunLevel can be multiple values in parallel to match multiple runlevel, and for most actions, only if RunLevel matches the current runlevel is successful.
3. Action
An action is a description of how the subsequent process works. Action-desirable values include: Initdefault, Sysinit, boot, bootwait, and so on: Initdefault is a special action value that identifies the default boot level, and when Init is activated by the core, It reads the Initdefault entry in Inittab, obtains runlevel in it, and acts as the current runlevel. If there is no Inittab file, or there are no Initdefault entries in it, INIT will request input runlevel on the console. Actions such as sysinit, boot, bootwait, and so on, will run unconditionally when the system starts, ignoring the runlevel. The rest of the action (without initdefault) is related to a runlevel. The definitions of each action are described in detail in the Inittab Man Handbook.
4. Process
Process is the specific execution procedure. The program can be followed with parameters.
Tips: If you don't understand this file, it doesn't matter, as you learn more about Linux, you can look back at the file and you'll be enlightened. But now you have to understand the meanings of the various levels of runlevel.
C initialization of the system
There is a line in the init configuration file: Si::sysinit:/etc/rc.d/rc.sysinit It calls execution/etc/rc.d/rc.sysinit, and Rc.sysinit is a bash shell script, It is mainly to do some system initialization work, Rc.sysinit is each run level must first run the important script. Its main tasks are: Activating the swap partition, checking the disk, loading the hardware modules, and some other priorities that need to be performed.
Rc.sysinit has about 850 rows, but each single feature is relatively simple, with comments, suggesting that interested users can read the file on their own machine in order to understand the details of the initialization of the system. Because this file is longer, it is not listed in this article, and does not make a specific introduction. When the Rc.sysinit program finishes executing, it will return to Init to continue to the next step. Usually the next step is to/ETC/RC.D/RC the program. For example, run Level 3, Init executes the following line in the configuration file Inittab:
L5:5:WAIT:/ETC/RC.D/RC 5
This line indicates that running with the 5 parameter/ETC/RC.D/RC,/ETC/RC.D/RC is a shell script that accepts 5 as a parameter to execute all RC startup scripts under the/etc/rc.d/rc5.d/directory,/etc/rc.d/rc5.d/ These startup scripts in the directory are actually some connection files, not the actual RC startup scripts, and the actual RC startup scripts are actually placed in the/etc/rc.d/init.d/directory. These RC startup scripts have a similar usage, and they generally accept start, stop, restart, status, and so on.
The RC startup script in/etc/rc.d/rc5.d/is usually a connection file that starts with K or S, and will run with the start parameter for startup scripts starting with S. If you find that the corresponding script also has a K-link, and is already in the running state (the file under/var/lock/subsys/as the flag), the stop will be the first parameter to stop these started daemons, and then rerun. This is done to ensure that when Init changes the RunLevel, all associated daemons are restarted.
As to which Daemons will be run at each run level, the user can be set from the row by using "System Services" in Chkconfig or Setup.
D set up terminal
After the RC finishes executing, return to init. At this point the basic system environment has been set up, various daemons have been started. Init then opens 6 terminals so that the user can log on to the system. The following 6 lines in Inittab define 6 terminals:
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty Tty2
3:2345:respawn:/sbin/mingetty Tty3
4:2345:respawn:/sbin/mingetty Tty4
5:2345:respawn:/sbin/mingetty Tty5
6:2345:respawn:/sbin/mingetty Tty6
From the above can be seen in the 2, 3, 4, 5 of the operating level will be respawn to run the Mingetty program, mingetty program can open terminal, set mode. At the same time it will display a text login interface, which is the login interface we often see, in this login screen will prompt the user to enter the user name, and the user input user will be passed as a parameter to the login program to verify
Identity of the user.
E User Login System
For graphical users with a run level of 5, their login is via a graphical login interface. After successful login, you can go directly to KDE, GNOME and other window manager. This article is mainly about the text mode of login: When we see the Mingetty login interface, we can enter the user name and password to log into the system.
Linux Account Verification Program is Login,login will receive mingetty from the user name parameters. Login then parses the username: If the user name is not root and there is a/etc/nologin file, login will output the contents of the Nologin file and then exit. This is typically used for system maintenance to prevent non-root users from logging on. Only the terminal registered in/etc/securetty allows the root user to log on, and if the file does not exist, root can log on at any terminal. The/etc/usertty file is used to make additional access restrictions to the user, and there are no additional restrictions if the file does not exist.
After analyzing the user name, login will search/etc/passwd and/etc/shadow to verify the password and set up additional information about the account, such as what the home directory is and what shell to use. If you do not specify a home directory, the default is the root directory, and if you do not specify a shell, the default is/bin/bash.
When the login program succeeds, it will output the most recent login information (recorded in/var/log/lastlog) to the corresponding terminal, and check whether the user has new mail (in/usr/spool/mail/'s corresponding user list). Then start setting up the various environment variables: for bash, the system first looks for the/etc/profile script file and executes it, and then executes it if there is a. bash_profile file in the user's home directory, in which case other configuration files may be called. After all the configuration files have been executed, the various environment variables are set up, and a familiar command prompt will appear, and the entire boot process is over.
" how to switch between graphic mode and text mode "
Linux presets provide six command window terminals let's log in. By default we are logged in the first window, that is, Tty1, the six windows are Tty1,tty2 ... tty6, you can press CTRL + ALT + F1 ~ F6 to toggle them. If you have a graphical interface installed, by default it is entered into the graphical interface, you can press CTRL + ALT + F1 ~ F6 to enter one of the command window interfaces. When you go to the Command window interface and then return to the graphical interface just press CTRL + ALT + F7 to come back. If you are using a VMware virtual machine, the shortcut keys for the command window switch are ALT + Space + f1~f6. If you are in the graphical interface, press ALT + Shift + Ctrl + F1~F6 to switch to the command window.
" Learn to use shortcut keys "
CTRL + C: This is the shortcut to terminate the current command, of course you can also enter a large string of characters, do not want it to run directly CTRL + C, the cursor will jump into the next line.
Tab: This key is the most useful key, but also the author of the highest probability of hitting a key. Because when you hit half a command, it will help you complete it. Not only the command, when you hit a directory, the same can be complete, do not believe you try.
Ctrl + D: Exit the current terminal, and you can also enter exit.
Ctrl + Z: Pause the current process, such as you are running a command, suddenly feel a bit of a problem to pause, you can use this shortcut. After pausing, you can use FG to recover it.
Ctrl + L: Clear the screen so that the cursor moves to the first line.
" learn to query Help document -man"
This man is usually used to see the Help document for a command. For example:
Enter man LS in fact formatted as man + command
You'll see the relevant help documentation. The instructions from the command to the parameters of the command and the usage are described in great detail. That's good.
"Linux system directory Structure "
After logging in to the system, enter LS in the current command window and you will see
The following is an explanation of these directories:
/bin bin is the abbreviation for binary. This directory holds the most frequently used commands.
/boot Here are some of the core files used when starting Linux, including some connection files and image files.
/Dev Dev is an abbreviation for device. This directory is a Linux external device that accesses the device in Linux in the same way that it accesses the file.
/etc This directory is used to store all the configuration files and subdirectories required for system administration.
Home directory, in Linux, each user has its own directory, which is typically named after the user's account name.
/lib This directory contains the system's most basic dynamic connection shared library, which acts like a DLL file in Windows. These shared libraries are required for almost all applications.
/lost+found This directory is generally empty, and when the system shuts down illegally, some files are stored here.
/media Linux system will automatically identify some devices, such as USB flash drive, optical drive, etc., when identified, Linux will attach the identified device to this directory.
/MNT system provides this directory for users to temporarily mount other file systems, we can mount the CD-ROM drive on the/mnt/, and then enter the directory to view the contents of the CD-ROM.
/opt This is the directory where additional installation software for the host is placed. For example, if you install an Oracle database, you can put it in this directory. The default is empty.
/proc This directory is a virtual directory, it is the mapping of system memory, we can access this directory directly to obtain system information. The contents of this directory are not on the hard disk but in memory, we can also directly modify some of the files inside, such as can be used to block the host ping command, so that others cannot ping your machine:
echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all.
/root The directory is a user home directory for system administrators, also known as super-privileged users.
/sbin S is the meaning of super user, which is a system manager used by the system administrator.
/selinux This directory is a unique directory of Redhat/centos, SELinux is a security mechanism, similar to Windows Firewall, but this mechanism is more complex, this directory is to store selinux related files.
/srv This directory holds data that needs to be extracted after some services are started.
/sys This is a big change in the linux2.6 kernel. In this directory, a new file system Sysfs is installed in the 2.6 kernel, and the Sysfs file system integrates the information of the following 3 file systems: Proc File system for process information, DEVFS file system for devices, and devpts file system for pseudo terminal. The file system is a visual reflection of the kernel device tree. When a kernel object is created, the corresponding files and directories are also created in the kernel object subsystem.
/tmp This directory is used to store some temporary files.
/usr This is a very important directory, the user's many applications and files are placed in this directory, similar to the Program Files directory under Windows.
/usr/bin: The application that the system user uses.
/usr/sbin: A more advanced hypervisor and system daemon used by super users.
/USR/SRC: The default drop directory for the kernel source code.
/var This directory is stocked with things that are constantly expanding, and we are accustomed to placing directories that are often modified in this directory. Includes various log files.
In the Linux system, there are several directories are more important, usually need to be careful not to accidentally delete or arbitrarily change the internal files. /etc: Also mentioned above, this is the configuration file in the system, if you change a file in this directory may cause the system will not start. /bin,/sbin,/usr/bin,/usr/sbin: This is the placement directory of the system preset execution files, such as LS is in the/bin/ls directory. It is worth noting that,/bin,/usr/bin is the instruction for the system user (except for root), and/sbin,/usr/sbin is the command to use for root. /var: this is a very important directory, the system ran a lot of programs, then each program will have a corresponding log generated, and these logs are recorded in this directory, specifically in the/var/log directory, the other mail preset placement is also here.
" How to turn off the computer properly "
In fact, most of the Linux domain is used on the server, rarely encountered the shutdown operation. After all, running a service on the server is endless, unless in exceptional circumstances, the last resort will be turned off.
Linux is different from Windows, under Linux, because every program (or service) is executed in the background, so there may be quite a few people working on your console at the same time behind the screen you can't see, such as browsing the web, sending letters, sending files by FTP, etc. If you just press the power switch to shut down, other people's data may be interrupted! That would be a headache! In addition, the biggest problem is that if the shutdown is not normal, it can cause file system damage (because it is too late to write data back to the file, so some service files will be problematic!) )。
If you are shutting down, you must ensure that no other users are online in the current system. Can release who this command, and if you want to see the network online status, you can release netstat-a this command, and to see the background of the program can execute ps-aux this command. Use these instructions to let you know a little about the current state of the host! (these commands will be mentioned in a later section, so just understand!) )
The correct shutdown process is: SYSNC? Shutdown? Reboot? Halt
Sync synchronizes the data from memory to the hard disk.
Shutdown shutdown instructions, you can look at the help documentation for man shutdown. For example, you can run the following command to shut down:
Shutdown–h ' This server would shutdown after ten mins ' This command tells you that the computer will shut down in 10 minutes and will be displayed in the current screen of the logged-on user.
Shutdown–h now immediately shuts down the machine
Shutdown–h 20:25 The system will shut down at 20:25 today.
Shutdown–h +10 10 minutes after shutdown
Shutdown–r now system restarts immediately
Shutdown–r +10 system restarts after 10 minutes
Reboot is a reboot, equivalent to Shutdown–r now
Halt shutdown system, equivalent to Shutdown–h now and Poweroff
In conclusion, whether it is rebooting the system or shutting down the system, first run the sync command to write the data in memory to disk. The command to shut down has shutdown–h now halt Poweroff and Init 0, and the command to restart the system has shutdown–r now reboot init 6.
" forgot root password How to do "
Previously I forgot the administrator password of Windows, because I will not erase the password with the CD and then only reinstall the system. Now think about what a stupid thing it is. The same Linux system you will also encounter the forgotten root password situation, if you encounter such a situation how to do? Do you want to reinstall the system? Of course not! Go to single-user mode and change the root password. How to get into it.
1 restarting the Linux system
3 seconds to press ENTER, the following interface appears
then enter E
Enter single on the last side of the second line, with a space. To move to the second line by pressing the down tip, press "E" to enter edit mode
Add a single return to the rear
Finally Press "B" to start, after the start of the single-user mode is entered
Now that you have entered the single-user mode, you can change the root password. The command for more passwords is passwd
" Rescue mode using the system installation CD "
Rescue mode is rescue, this mode is mainly applied to, the system can not enter the situation. For example, grub corruption or a configuration file modification error. How do I use rescue mode?
CD-ROM boot, press F5 to enter rescue mode
Enter Linux Rescue carriage return
Choose the language, I suggest you choose English
Select US keyboard
This asks if you want to start the network, and sometimes it may be networked debugging. We choose No
This tells us that the system will then be mounted in the/mnt/sysimage. There are three options, Continue is mounted to continue the next step, read-only mount as read-only, so more secure, sometimes file system corruption, read-only mode will prevent the file system from nearly one step corruption; Skip is not mounted, enter a command Window mode. Here we choose Continue.
At this point, the system has been mounted in the/mnt/sysimage. Next, enter chroot/mnt/sysimage into the admin environment.
Tips: In fact, you can also change the root password in rescue mode. This rescue mode is similar to the Windows PE system. When chroot/mnt/sysimage/is run, then LS sees that the directory structure is the same as the directory structure in the original system. That's right! The environment is identical to that of the original system. You can enter exit or press CTRL + D to exit the environment. And then you can look at LS.
This directory is actually the directory structure in rescue mode, and our system files are all in the/mnt/sysimage directory.
The fourth chapter enters the Linux world preliminarily