溫馨提示×

ubuntu中rc.local怎么開啟和使用

小億
362
2024-02-28 12:17:18
欄目: 編程語言

在Ubuntu中,rc.local文件默認(rèn)是不啟用的,如果需要啟用并使用rc.local文件,可以按照以下步驟操作:

  1. 打開終端,輸入以下命令打開rc.local文件進行編輯:
sudo nano /etc/rc.local
  1. 在rc.local文件中添加您需要在系統(tǒng)啟動時自動運行的命令或腳本。例如,您可以添加以下內(nèi)容:
#!/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.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

exit 0
  1. 保存并退出rc.local文件。在nano編輯器中,按下Ctrl+X,然后按Y確認(rèn)保存,最后按Enter鍵確認(rèn)文件名。

  2. 授予rc.local文件執(zhí)行權(quán)限,輸入以下命令:

sudo chmod +x /etc/rc.local
  1. 最后,重新啟動系統(tǒng)以應(yīng)用更改,輸入以下命令:
sudo reboot

之后,系統(tǒng)啟動時,rc.local文件中添加的命令或腳本將會自動執(zhí)行。

0