Ubuntu server启动脚本以及欢迎页定制


基础知识

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
2 total 12
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
3  
4 iflab@ifLab:~$

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)
3  
4  * Documentation:  https://help.ubuntu.com/
5  
6   System information as of Wed Feb 19 09:54:12 CST 2014
7  
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.***.***.***
11   Swap usage:   0%
12  
13   Graph this data and manage this system at:
14  
16  
17 76 packages can be updated.
18 34 updates are security updates.
19  
20 iflab@ifLab:~$

通过下述命令我们知道/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/
2 total 36
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
1 #!/bin/bash
2 # eduStack welcome page
3 ### BEGIN INIT INFO
4 # Provides:       welcome
5 # Required-Start: $network $remote_fs
6 # Required-Stop:  $network $remote_fs
7 # Default-Start:  3
8 # Default-Stop:
9 # Short-Description:  eduStack service
10 # Description:  eduStack service
11 ### END INIT INFO
12  
13 ipaddress=`ifconfig | grep inet | awk 'NR==1 {print $2}' | awk 'BEGIN { FS=":" } { print $2 }'`
14  
15 rm /etc/issue
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
22  
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 }'`
27     fi
28 fi
29  
30 echo '[1;33m' >> /etc/issue
31  
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
35   echo "[1m*** Please refer to http://edustack.org for details. ***[0m"  >> /etc/issue
36   echo "" >> /etc/issue
37 else
38   echo "[1m*** The machine could not configure the network interface.      ***"  >> /etc/issue
39   echo "[1m*** Please visit http://edustack.org for details.  ***[0m"  >> /etc/issue
40   echo "" >> /etc/issue
41 fi
42  
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
53 fi

添加可执行权限

1 sudo chmod +x /etc/init.d/welcome

修改脚本启动和停止次序

1 sudo update-rc.d welcome defaults 80 30

2.添加欢迎LOGO:

1 nano /etc/issue.ascii
1           _       _____ _             _   
2          | |     /  ___| |           | |  
3   ___  __| |_   _\\ `--.| |_ __ _  ___| | __
4  / _ \\/ _` | | | |`--. \\ __/ _` |/ __| |/ /
5 |  __/ (_| | |_| /\\__/ / || (_| | (__|   <
6  \\___|\\__,_|\\__,_\\____/ \\__\\__,_|\\___|_|\\_\\

该字符图像可通过http://www.network-science.de/ascii/ 生成,生成的字符需要防止\转义!!

3.生成首次启动标记:

1 touch ~/firstboot

4.修改当前用户.bashrc文件,在文件末尾添加首次登陆检测:

1 if [ -f "/home/iflab/firstboot" ]; then
2  mv /home/iflab/firstboot /home/iflab/.firstboot_done
3  fi

5.强制该用户密码过期:

1 sudo usermod -L iflab
2  sudo chage -d 0 iflab
3  sudo usermod -U iflab

6.将自定源修改回官方源

7.清除网络信息,改为dhcp

1 auto eth0
2 iface eth0 inet dhcp

8.reboot 测试:

首次登陆:

QQ图片20140219101325

 

再次登录:QQ图片20140219101336

9.关机,生成虚拟机OVA模板

 

参考文档:

1.鸟哥的私房菜:http://vbird.dic.ksu.edu.tw/linux_basic/

2.ubuntu help:   https://help.ubuntu.com/community/UbuntuBootupHowto

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Ubuntu 20.04 Server 安装步骤: 1. 下载 Ubuntu 20.04 Server 镜像文件并制作启动盘。 2. 将启动盘插入服务器并启动,选择安装 Ubuntu Server。 3. 选择安装语言、时区等基本设置。 4. 设置主机名、用户名和密码。 5. 选择安装软件包,可以选择最小化安装或安装其他软件包。 6. 等待安装完成,重启服务器。 7. 登录系统,进行必要的配置和安装所需的软件包。 以上是 Ubuntu 20.04 Server 安装的基本步骤,具体操作可以参考官方文档或其他相关教程。 ### 回答2: Ubuntu 20.04是最新的长期支持版本,适用于服务器和桌面环境。下面是关于Ubuntu 20.04服务器安装的步骤。 1. 下载Ubuntu 20.04 Server ISO镜像:首先访问Ubuntu官方网站并下载Ubuntu 20.04 Server ISO镜像文件。确保选择适合您计算机体系结构的镜像文件,例如AMD64。 2. 创建启动U盘:使用镜像刻录工具,将ISO镜像文件写入一个USB闪存驱动器,以便能够从该驱动器启动计算机。 3. 启动计算机并选择安装:将USB闪存驱动器插入您的计算机,并确保计算机在BIOS设置启动到该驱动器。选择安装Ubuntu 20.04 Server,然后按照提示进行操作。 4. 安装过程:在启动时,选择所需的语言,然后选择键盘布局。接下来,系统将检测一些硬件信息并为您配置网络。在这一步骤中,您需要提供有关服务器的一些详细信息,例如主机名、用户名和密码等。 5. 磁盘分区:将选择是否自动分区或手动分区。如果您不熟悉分区过程,建议选择自动分区选项。如果您需要更精确地控制磁盘分区,选择手动分区选项。 6. 安装软件:选择您要安装的软件包。您可以选择仅安装基本系统、安装标准系统工具和服务器软件,或自定义选择您需要的软件包。 7. 进行最后的安装:确认您的选择并安装系统。安装过程将自动为您安装所需的软件包并设置必要的配置。 8. 重启服务器:安装完成后,系统将要求您重启计算机。按照提示重新启动服务器。 9. 完成设置:在重新启动后,您可以使用之前设置的用户名和密码登录Ubuntu 20.04服务器。您还可以根据需要进行其他配置,例如安装和配置服务器应用程序、设置防火墙等。 这些是在Ubuntu 20.04上安装服务器的基本步骤。根据您的需求和环境,可能还需要进行其他配置和设置。 ### 回答3: Ubuntu 20.04 是一个流行的 Linux 操作系统版本。它有许多用途,可以用于个人使用、企业应用和服务器环境。下面是关于 Ubuntu 20.04 服务器安装的简要步骤: 1. 下载镜像文件:您可以从 Ubuntu 官方网站上下载 Ubuntu 20.04 服务器版的镜像文件。确保选择正确的版本和适合您服务器的镜像。 2. 创建启动设备:您需要一个可用的安装介质将 Ubuntu 20.04 操作系统安装到服务器上。您可以将镜像文件刻录到 DVD 上,或者创建一个可引导的 USB 驱动器来安装它。 3. 启动服务器:将启动设备插入服务器,并将服务器启动。确保您已将服务器设置为从所选的启动设备引导。 4. 选择安装选项:一旦服务器已启动,您将看到一个欢迎屏幕。根据您的需求进行选择,例如选择安装语言和键盘布局。 5. 确定系统分区:在安装过程中,您需要确定用于安装 Ubuntu 20.04 的磁盘分区。您可以选择自动分区选项或手动分区选项,具体取决于您的需求。 6. 设置管理员帐户:您需要设置一个管理员帐户,该帐户将用于管理服务器。提供所需的详细信息,例如用户名、密码,以及您是否需要加密您的家目录。 7. 安装操作系统:一旦完成上述设置,安装程序将开始在服务器上安装 Ubuntu 20.04。这可能需要一些时间,具体取决于您的系统和网络速度。 8. 完成安装:完成安装后,您将收到一条消息,说明安装已成功完成。您可以选择重启服务器并开始使用 Ubuntu 20.04。 以上是使用约 300 字回答您关于 Ubuntu 20.04 服务器安装的问题。希望这些步骤对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值