Ubuntu rc.local 有効化と設定方法【簡単解説】

Ubuntuでは、デフォルトでrc.localファイルは有効になっていません。rc.localファイルを有効にして使用する必要がある場合は、以下の手順に従って操作してください。

  1. ターミナルを開き、次のコマンドを入力してrc.localファイルを開いて編集してください。
sudo nano /etc/rc.local
  1. 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.

# 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で保存を確認し、最後にEnterキーでファイル名を確認してください。
  2. rc.localファイルに実行権限を付与するには、以下のコマンドを入力してください:
sudo chmod +x /etc/rc.local
  1. 最後、変更を適用するためにシステムを再起動するには、次のコマンドを入力してください。
sudo reboot

その後、システムの起動時に、rc.localファイルに追加されたコマンドやスクリプトが自動的に実行されます。

bannerAds