---
# System prepended metadata

title: 在 Ubuntu 18.04 建立 rc.local

---

# 在 Ubuntu 18.04 建立 rc.local

1. 寫一個 rc-local.service 檔案: `sudo vim /etc/systemd/system/rc-local.service`
2. 將以下內容複製到 rc-local.service
```
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
```
3. 建立 rc.local: `sudo vim /etc/rc.local`
4. 將以下內容複製到 rc.local
```
#!/bin/sh -e

exit 0
```
5. 更新 rc.local 的權限: `sudo chmod +x /etc/rc.local`
6. 啟用 rc-local.service
```
sudo systemctl enable rc-local.service
sudo systemctl start rc-local.service
```