基础知识
1.linux启动流程
- 加载 BIOS 的硬件资讯与进行自我测试,并依据配置取得第一个可启动的装置;
- 读取并运行第一个启动装置内 MBR 的 boot Loader (亦即是 grub, spfdisk 等程序);
- 依据 boot loader 的配置加载 Kernel ,Kernel 会开始侦测硬件与加载驱动程序;
- 在硬件驱动成功后,Kernel 会主动呼叫 init 程序,而 init 会取得 run-level 资讯;
- init 运行 /etc/rc.d/rc.sysinit 文件来准备软件运行的作业环境 (如网络、时区等);
- init 运行 run-level 的各个服务之启动 (script 方式);
- init 运行 /etc/rc.d/rc.local 文件;
- init 运行终端机模拟程序 mingetty 来启动 login 程序,最后就等待使用者登陆啦;
2.ubuntu运行等级(Run level)
- 0 – Halt(系统直接关机)
- 1 – Single(单人维护模式,用在系统出问题时的维护)
- 2 – Full multi-user with display manager (GUI)
- 3 – Full multi-user with display manager (GUI)
- 4 – Full multi-user with display manager (GUI)
- 5 – Full multi-user with display manager (GUI)
- 6 – Reboot(重新启动)
ubuntu默认启动级别为2(/etc/init/rc-sysinit.conf定义),此处2-5均是GUI,而RedHat/CentOs 的启动级别则是:
- 0 – halt (系统直接关机)
- 1 – single user mode (单人维护模式,用在系统出问题时的维护)
- 2 – Multi-user, without NFS (类似底下的 runlevel 3,但无 NFS 服务)
- 3 – Full multi-user mode (完整含有网络功能的纯文字模式)
- 4 – unused (系统保留功能)
- 5 – X11 (与 runlevel 3 类似,但加载使用 X Window)
- 6 – reboot (重新启动)
RedHat/CentOs桌面版默认启动级别为5
3.启动系统服务与相关启动配置档(/etc/rcN.d/*)
ubuntu /etc/目录下有rc0.d rc1.d ..rc6.d 分别对应上述7个启动级别
以rc2.d 为例:
1 |
iflab@ifLab:/etc/rc2.d$
ll |
3 |
drwxr-xr-x
2 root root 4096 Jan 11 15:41 ./ |
4 |
drwxr-xr-x
101 root root 4096 Jan 28 15:20 ../ |
5 |
-rw-r--r--
1 root root 677 Jul 27 2012 README |
6 |
lrwxrwxrwx
1 root root 18 Jan 11 15:00 S20fcgiwrap -> ../init.d/fcgiwrap* |
7 |
lrwxrwxrwx
1 root root 15 Jan 11 14:53 S20nginx -> ../init.d/nginx* |
8 |
lrwxrwxrwx
1 root root 15 Jan 11 14:08 S50rsync -> ../init.d/ rsync * |
9 |
lrwxrwxrwx
1 root root 19 Jan 11 14:08 S70dns-clean -> ../init.d/dns-clean* |
10 |
lrwxrwxrwx
1 root root 18 Jan 11 14:08 S70pppd-dns -> ../init.d/pppd-dns* |
11 |
lrwxrwxrwx
1 root root 14 Jan 11 13:40 S75sudo -> ../init.d/ sudo * |
12 |
lrwxrwxrwx
1 root root 21 Jan 11 14:09 S99grub-common -> ../init.d/grub-common* |
13 |
lrwxrwxrwx
1 root root 18 Jan 11 13:39 S99ondemand -> ../init.d/ondemand* |
14 |
lrwxrwxrwx
1 root root 18 Jan 11 13:39 S99rc. local
-> ../init.d/rc.
local * |
15 |
iflab@ifLab:/etc/rc2.d$ |
文档名全部以 Sxx 或 Kxx ,其中 xx 为数字,且这些数字在文件之间是有相关性的!
全部是连结档,连结到 stand alone 服务启动的目录 /etc/init.d/ 去
/etc/rc2.d/[SK]xx 其实就是跑到 /etc/init.d/ 去找到相对应的服务脚本, 然后分别进行 start (Sxx) 或 stop (Kxx) 的动作
那么为什么 K 与 S 后面要有数字呢?因为各不同的服务其实还是互有关系的。举例来说,如果要启动 WWW 服务,总是得要有网络吧?所以 /etc/init.d/network 就会比较先被启动啦!那么您就会知道在 S 或者是 K 后面接的数字是啥意思了吧?嘿嘿,那就是运行的顺序啦!那么哪个文件被最后运行呢? 看到最后一个被运行的项目是啥?没错,就是 S99local ,亦即是: /init.d/rc.local
这个文件啦!
4.登陆和欢迎信息控制
登陆linux的欢迎界面可由/etc/issue和/etc/motd控制。/etc/issue文件的使用方法与/etc/motd文件相差不大,它们的主要区别在于:当一个网络用户或通过串口登录系统上时,/etc/issue的文件内容显示在login提示符之前,而/etc/motd内容显示在用户成功登录系统之后。
登陆前显示信息:
1 |
iflab@ifLab:~$
cat
/etc/issue
|
2 |
Ubuntu
12.04.3 LTS \n \l |
issue 内各代码意义:
- \d 本地端时间日期;
- \l 显示第几个终端机接口;
- \m 显示硬件等级 (i386/i486/i586/i686…);
- \n 显示主机网络名称;
- \o 显示 domain name;
- \r 操作系统版本 (相当于 uname -r)
- \t 显示本地端时间;
- \s 操作系统名称;
- \v 操作系统版本。
可以通过修改/etc/issue内容制作自己喜欢的登陆信息。这里还要注意的是,除了 /etc/issue 外还有个 /etc/issue.net。这个是提供给 telnet 这个进程登录程序用的。当我们使用 telnet 连接主机时,主机的登入画面就会显示 /etc/issue.net 而不是 /etc/issue 。
登陆后显示信息:
1 |
iflab@ifLab:~$
cat
/etc/motd
|
2 |
Welcome
to Ubuntu 12.04.3 LTS (GNU/Linux 3.8.0-29-generic x86_64) |
6 |
System
information as of Wed Feb 19 09:54:12 CST 2014 |
8 |
System
load: 1.57 Processes: 96 |
9 |
Usage
of /: 34.9% of 14.38GB Users logged in :
1 |
10 |
Memory
usage: 8% IP address for
eth0: 222.***.***.***
|
13 |
Graph
this data and manage this system at: |
17 |
76
packages can be updated. |
18 |
34
updates are security updates. |
通过下述命令我们知道/etc/motd只是一个符号链接
1 |
iflab@ifLab:~$
ll /etc/motd |
2 |
lrwxrwxrwx
1 root root 13 Jan 11 13:37 /etc/motd -> /var/run/motd |
那么,我我们怎么定制呢?
我们找到生成的脚本在目录/etc/update-motd.d/中
1 |
iflab@ifLab:~$
ll /etc/update-motd.d/ |
3 |
drwxr-xr-x
2 root root 4096 Jan 11 15:27 ./ |
4 |
drwxr-xr-x
101 root root 4096 Jan 28 15:20 ../ |
5 |
-rwxr-xr-x
1 root root 1220 Apr 19 2012 00-header* |
6 |
-rwxr-xr-x
1 root root 1358 Apr 19 2012 10-help-text* |
7 |
lrwxrwxrwx
1 root root 46 Jan 11 15:27 50-landscape-sysinfo -> /usr/share/landscape/landscape-sysinfo.wrapper* |
8 |
-rwxr-xr-x
1 root root 149 May 16 2012 90-updates-available* |
9 |
-rwxr-xr-x
1 root root 129 Aug 9 2012 91-release-upgrade* |
10 |
-rwxr-xr-x
1 root root 142 May 16 2012 98- fsck -at-reboot* |
11 |
-rwxr-xr-x
1 root root 144 May 16 2012 98-reboot-required* |
12 |
-rwxr-xr-x
1 root root 1158 Apr 19 2012 99-footer* |
修改这几个文件,可以得到自己想要的结果。
修改后用:sudo run-parts /etc/update-motd.d 去执行就会立即见到效果。
OK,了解了上述知识,我们来——
简单定制
1.添加新的脚本来动态生成welcome信息:
1 |
sudo
nano /etc/init.d/welcome
|
13 |
ipaddress=` ifconfig
|
grep
inet |
awk
'NR==1 {print $2}'
|
awk
'BEGIN { FS=":" } { print $2 }'
` |
16 |
echo
'[0;36m'
> /etc/issue
|
17 |
cat
/etc/issue.ascii >> /etc/issue
|
18 |
echo
"[0m"
>> /etc/issue
|
19 |
echo
'[0;36m'
>> /etc/issue
|
20 |
echo
"[1m*** Welcome to the eduStack ***"
>> /etc/issue
|
21 |
echo
"[1m*** Built using Ubuntu 12.04 - Kernel \r (\l). ***[0m"
>>
/etc/issue |
23 |
if
[ -f
"/etc/init.d/networking"
];
then |
24 |
if
[
"$ipaddress"
=
""
] || [
"$ipaddress"
=
"127.0.0.1"
];
then |
25 |
/etc/init.d/networking
force-reload |
26 |
ipaddress=` ifconfig
|
grep
inet |
awk
'NR==1 {print $2}'
|
awk
'BEGIN { FS=":" } { print $2 }'
` |
30 |
echo
'[1;33m'
>> /etc/issue
|
32 |
if
[
"$ipaddress"
!=
""
] && [
"$ipaddress"
!=
"127.0.0.1"
];
then |
33 |
echo
"[1m*** You can access the application at
http://$ipaddress ***"
>> /etc/issue
|
34 |
echo
"[1m*** The default username and password is 'iflab' and 'bistu123'. ***"
>> /etc/issue
|
38 |
echo
"[1m*** The machine could not configure the network interface. ***"
>> /etc/issue
|
43 |
if
[ -f
"/home/iflab/firstboot"
];
then |
44 |
echo
'[1;31m'
>> /etc/issue
|
45 |
echo
"******************************************************************************"
>> /etc/issue
|
46 |
echo
"* To access the console, please use login 'iflab' and password 'iflab' *"
>> /etc/issue
|
47 |
echo
"* *"
>> /etc/issue
|
48 |
echo
"* WARNING *"
>> /etc/issue
|
49 |
echo
"* For security reasons, upon your first login, you will be prompted to *"
>> /etc/issue
|
50 |
echo
"* change the default password. *"
>> /etc/issue
|
51 |
echo
"******************************************************************************"
>> /etc/issue
|
52 |
echo
'[0m'
>> /etc/issue
|
添加可执行权限
1 |
sudo
chmod
+x /etc/init.d/welcome
|
修改脚本启动和停止次序
1 |
sudo
update-rc.d welcome defaults 80 30
|
2.添加欢迎LOGO:
3 |
___
__| |_ _\\ `--.| |_ __ _ ___| | __ |
4 |
/
_ \\/ _` | | | |`--. \\ __/ _` |/ __| |/ / |
5 |
|
__/ (_| | |_| /\\__/ / || (_| | (__| < |
6 |
\\___|\\__,_|\\__,_\\____/
\\__\\__,_|\\___|_|\\_\\ |
该字符图像可通过http://www.network-science.de/ascii/ 生成,生成的字符需要防止\转义!!
3.生成首次启动标记:
4.修改当前用户.bashrc文件,在文件末尾添加首次登陆检测:
1 |
if
[ -f
"/home/iflab/firstboot"
];
then |
2 |
mv
/home/iflab/firstboot /home/iflab/.firstboot_done
|
5.强制该用户密码过期:
6.将自定源修改回官方源
7.清除网络信息,改为dhcp
8.reboot 测试:
首次登陆:
再次登录:
9.关机,生成虚拟机OVA模板
参考文档:
1.鸟哥的私房菜:http://vbird.dic.ksu.edu.tw/linux_basic/
2.ubuntu help: https://help.ubuntu.com/community/UbuntuBootupHowto