Ubuntu&Debian设置开机信息

作者:waMoYu 发布时间: 2024-10-24 阅读量:27 评论数:0

未登入时提示信息设置

一、添加rc-local.service服务脚本

创建/etc/systemd/system/rc-local.service服务脚本 运行下述命令,生成服务脚本

cat > /etc/systemd/system/rc-local.service << EOF
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target
​
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
​
[Install]  
WantedBy=multi-user.target
Alias=rc-local.service
EOF

二、创建rc.local脚本

cat > /etc/rc.local << EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
EOF

三、赋予rc.local可执行权限

chmod +x /etc/rc.local

四、添加自启脚本或命令并测试创建欢迎信息的shell脚本并赋予执行权限

将所需要执行的命令或脚本写入exit 0之前即可,如:

cd ~&&touch OSInfo.sh&&chmod +x OSInfo.sh

五、将以下代码写入到OSInfo.sh

根据需求修改显示内容

vim OSInfo.sh
#!/bin/bash
​
# 清空issue
echo "" > /etc/issue
echo "" > /etc/motd
# 获取所有网卡名称及IP地址,不去掉子网掩码
#ip=`ip addr | grep inet | grep -v inet6 | awk '{print("\033[34m|\033[0m\033[32m",$NF,"\t->",$2,"\033[0m")}'`
​
# 获取所有网卡名称及ip地址,去掉子网掩码
#ip=`ip addr | grep inet | awk '{print("\033[34m|\033[0m\033[32m",$NF,"\t->",$2,"\033[0m")}' | sed 's/host//g' | sed 's/noprefixroute//g' | sed 's/link//g'`
​
# 间隔符
t1="\033[34m+----------------------------\033[0m"
t2="\033[34m|\033[0m\033[32m"
t3="\033[0m"
​
#********************下方是开机提示信息********************#
echo "" >> /etc/issue
echo -e "$t1" >> /etc/issue
echo -e "$t2 Debian v11.6.0 $t3" >> /etc/issue
echo -e "$t1" >> /etc/issue
echo -e "$t2 UserName -> root $t3" >> /etc/issue
echo -e "$t2 PassWord -> Test@123 $t3" >> /etc/issue
echo -e "$t1" >> /etc/issue
echo -e "$t2 NetWork $t3" >> /etc/issue
echo -e "$ip" >> /etc/issue
echo -e "$t1" >> /etc/issue
echo "" >> /etc/issue
​
#********************下方是登入欢迎信息********************#
echo "The programs included with the Debian GNU/Linux system are free software;" >> /etc/motd
echo "the exact distribution terms for each program are described in the" >> /etc/motd
echo "individual files in /usr/share/doc/*/copyright." >> /etc/motd
echo "" >> /etc/motd
echo "Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent" >> /etc/motd
echo "permitted by applicable law." >> /etc/motd
​
echo "" >> /etc/motd
echo -e "$t1" >> /etc/motd
echo -e "$t2 Debian v11.6.0 $t3" >> /etc/motd
echo -e "$t1" >> /etc/motd
echo -e "$t2 UserName -> root $t3" >> /etc/motd
echo -e "$t2 PassWord -> Test@123 $t3" >> /etc/motd
echo -e "$t1" >> /etc/motd
echo -e "$t2 NetWork $t3" >> /etc/motd
echo -e "$ip" >> /etc/motd
echo -e "$t1" >> /etc/motd
echo "" >> /etc/motd

六、添加自启脚本或命令并测试

将所需要执行的命令或脚本写入exit 0之前即可,如:

vim /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
bash /root/OSInfo.sh
exit 0

登入后欢迎信息设置

  1. 进入“/etc/update-motd.d”路径下。

cd /etc/update-motd.d
  1. 创建“99-welcome-info”文件。

touch 99-welcome-info
  1. 将以下代码写入到“99-welcome-info”文件中。

vim 99-welcome-info
#!/bin/bash

# 获取所有网卡名称及IP地址,不去掉子网掩码
ip=`ip addr | grep inet | grep -v inet6 | awk '{print("\033[34m|\033[0m\033[32m",$NF,"\t->",$2,"\033[0m")}'`

# 间隔符
t1="\033[34m+----------------------------\033[0m"
t2="\033[34m|\033[0m\033[32m"
t3="\033[0m"

#********************下方是登入欢迎信息********************#
printf "\n"
printf "$t1\n"
printf "$t2 Ubuntu 22.04.3 LTS $t3\n"
printf "$t1\n"
printf "$t2 UserName: root $t3\n"
printf "$t2 PassWord: Test@123 $t3\n"
printf "$t1\n"
printf "$t2 NetWork $t3\n"
printf "$ip\n"
printf "$t1\n"
  1. 给“99-welcome-info”添加执行权限。

sudo chmod 755 99-welcome-info

4.脚本添加到rc.local中

vim /etc/rc.d/rc.local

将“/root/hello.sh”添加到rc.local的末尾

/root/OsInfo.sh

5.使用Systemd服务

在最新版本的CentOS(如CentOS 7及更高版本)中,/etc/rc.local文件不再默认存在,因此不再是系统启动时执行自定义脚本的推荐方式。取而代之的是使用Systemd服务单元或Cron作业来执行自定义脚本。以下是一些替代方案: Systemd是现代Linux发行版中常用的初始化系统,您可以创建一个Systemd服务单元,以在系统启动时执行自定义脚本。以下是一个示例: 1. 创建一个Systemd服务单元文件(例如,/etc/systemd/system/my-custom-script.service),并使用文本编辑器打开它:

sudo nano /etc/systemd/system/my-custom-script.service
**2. 在该文件中定义您的自定义脚本服务单元。例如:**
```shell
[Unit]
Description=My Custom Script

[Service]
Type=oneshot
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=multi-user.target

其中,ExecStart行指定要执行的脚本的路径。 3. 保存并关闭文件。 4. 启用并启动Systemd服务:

sudo systemctl enable my-custom-script
sudo systemctl start my-custom-script

这将使您的脚本在系统引导时自动运行。

评论