ubuntu设置开机信息

ubuntu设置开机信息

未登入时提示信息设置

一、添加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 hello.sh&&chmod +x hello.sh

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

根据需求修改显示内容

vim hello.sh
#********************下方是开机信息********************#
#!/bin/sh -e
echo "" > /etc/issue
ip=`ip addr | grep "scope global" | grep "eth0"  | awk '{print $2}' |awk -F/ '{print $1}'`
#ip=`ip addr | grep inet | grep eth0 | awk '{print($2)}'`
#这一段用于获取IP地址。

echo "******Ubuntu Server******" >> /etc/issue
echo "*->  username:root" >> /etc/issue
echo "*->  password:Test@123" >> /etc/issue
echo "*->  IPv4:$ip" >> /etc/issue
echo "*************************" >> /etc/issue

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

将所需要执行的命令或脚本写入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/hello.sh
exit 0

登入后欢迎信息设置

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

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

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

    vim 99-my-welcome-info
    
    #********用户登入后的欢迎信息********#
    #!/bin/sh -e
    ip=`ip addr | grep "scope global" | grep "ens3"  | awk '{print $2}' |awk -F/ '{print $1}'`
    #ip=`ip addr | grep inet | grep eth0 | awk '{print($2)}'`
    #这一段用于获取IP地址。
     printf "********Ubuntu Server********\n"
     printf "*->  username:root\n"
     printf "*->  password:Test@123\n"
     printf "*->  IPv4:$ip\n"
     printf "*****************************\n\n"
    
  4. 给“99-my-welcome-info”添加执行权限。

    sudo chmod 755 99-my-welcome-info
    
posted @ 2023-04-04 22:24  滴滴打怪  阅读(168)  评论(0)    收藏  举报