OpenWrt システム初期設定

:warning:Japanese notation
![OpenWrt_icon.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3412833/00cb7976-bf5f-1c85-4544-6cbbc043b497.png)

# こちらに統合しました

::: note
### [オールインワンスクリプト](https://qiita.com/site_u/items/c6a50aa6dea965b5a774)

![selector.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3412833/809fa65b-d98d-16f6-50e2-72a0030c1a56.png)

### 対応スクリプト

- [system-config.sh](https://github.com/site-u2023/config-software/blob/main/system-config.sh)
- [system.sh](https://github.com/site-u2023/config-software/blob/main/system.sh)

:::

# はじめに
::: note
### 初心者対応構成
- スクリプトでの自動設定
- [UCI (ttyd)](https://openwrt.org/docs/guide-user/base-system/uci)、[Webコンソール (LuCi)](https://openwrt.org/docs/guide-user/luci/start)、[ファイラー (SFTP)](https://openwrt.org/docs/guide-user/services/nas/sftp.server)にて比較作業がおススメ:exclamation:
:::

::: note
### デバイスアクセス
#### [PowerShell](https://learn.microsoft.com/ja-jp/powershell/scripting/what-is-windows-powershell?view=powershell-7.4)の開始
- キー入力：**`Win`+`x` > `a` > `はい`**

#### [UCI（SSH）アクセス](https://openwrt.org/docs/guide-quick-start/sshadministration)
```powershell:powershell:初期設定用
ssh -o StrictHostKeyChecking=no -oHostKeyAlgorithms=+ssh-rsa root@192.168.1.1
```
- root@192.168.1.1's password:`初期値：パスワード無し`

<details><summary>OpenSSHのインストールが無い場合:exclamation:</summary>

- 機能の確認
※Windows 10 Fall Creators Update(1709)以降標準搭載
```powershell:powershell
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
```
- 機能のインストール
```powershell:powershell
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
```
---

:::

# 最初にやる事

:::note
### デバイス情報確認

<details><summary>ディストリビューション</summary>

```bash
cat /etc/openwrt_release

```
```bash
cat /etc/openwrt_version

```
```bash
cat /etc/os-release

```
```bash
uname -a

```
```bash
uname -r

```
---

</details>

<details><summary>システム情報</summary>

[システム情報](https://openwrt.org/toh/linksys/wrt610n/sysinfo)

CPU
```sh
cat /proc/cpuinfo

```
MTD
```sh
cat /proc/mtd

```
MODULES
```sh
cat /proc/modules

```
devices
```sh
cat /proc/devices

```
free
```sh
free

```
meminfo
```sh
cat /proc/meminfo

```
iomem
```sh
cat /proc/iomem

```
ps
```sh
ps

```
ifconfig
```sh
ifconfig

```
dmesg
```sh
dmesg

```



プロセス数の上限
```SH
sysctl kern.maxproc kern.maxprocperuid 

```
```sh
sysctl kernel.threads-max

```
---

</details>

:::

::: note
### LuCi 確認
※リリースビルドのみ
- [openwrt.lan](http://openwrt.lan) [(192.168.1.1)](http://192.168.1.1)
:::

:::note
### [ROOTパスワード](https://openwrt.org/jp/doc/howto/firstlogin#telnet_%E3%81%A7%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3)
```bash
passwd

```
`Changing password for root`
`New password:`
```bash:passwd:入力モード
任意のパスワード
```
`Retype password:`
```bash:passwd:入力モード
再入力
```
`passwd: password for root changed by root`
```bash
exit

```
:::

::: note
### [SSHアクセスインターフェース](https://openwrt.org/docs/guide-user/base-system/dropbear)
※WAN側からアクセスする場合不要
```bash
uci set dropbear.@dropbear[0].Interface='lan'
uci commit dropbear

```
:::

# 設定
:::note warn
config(設定ファイル)は `/etc/config/`以下に格納
※以下設定は、新規インストール用
:::

:::note
### [システム](https://openwrt.org/docs/guide-user/base-system/uci#show_a_subsystem_s_current_configuration)
```bash
# 設定値
HOSTNAME='openwrt' # ルーター名
DESCRIPTION=`cat /etc/openwrt_version` # 説明
NOTES=`date` # 備考
# システム
uci set system.@system[0]=system
uci set system.@system[0].hostname=${HOSTNAME}
uci set system.@system[0].description="${DESCRIPTION}"
uci set system.@system[0].timezone='JST-9'
uci set system.@system[0].zonename='Asia/Tokyo'
uci set system.@system[0].conloglevel='6' # ログ出力レベル:注意
uci set system.@system[0].cronloglevel='9' # Cronログレベル:警告
# NTPサーバー
uci set system.ntp.enable_server='1'
uci set system.ntp.use_dhcp='0'
uci set system.ntp.interface='lan'
uci delete system.ntp.server
uci add_list system.ntp.server='0.jp.pool.ntp.org'
uci add_list system.ntp.server='1.jp.pool.ntp.org'
uci add_list system.ntp.server='2.jp.pool.ntp.org' 
uci add_list system.ntp.server='3.jp.pool.ntp.org'
uci commit system
/etc/init.d/system reload
/etc/init.d/sysntpd restart
# note
uci set system.@system[0].notes="${NOTES}"
uci commit system
/etc/init.d/system reload

```
:::

::: note
### [LED](https://openwrt.org/docs/guide-user/base-system/led_configuration)
利用可能LED確認
```bash
ls /sys/class/leds/ 

```

参考
```bash
# POWER:WHITE:OFF
uci add system led
uci set system.@led[-1].name='WHITE-OFF'
uci set system.@led[-1].sysfs='white:power'
uci set system.@led[-1].trigger='none'
uci set system.@led[-1].default='0'
# POWER:RED:ON
uci add system led
uci set system.@led[-1].name='POWER-RED'
uci set system.@led[-1].sysfs='red:powe
r'
uci set system.@led[-1].trigger='default-on'
# set
uci commit system
/etc/init.d/led restart

```
:::

:::note
### [オフロード](https://openwrt.org/docs/guide-user/firewall/firewall_configuration#config_sections)
- ソフトウェア フローオフロード
```bash
uci set firewall.@defaults[0].flow_offloading='1'
uci commit firewall
/etc/init.d/firewall restart

```
- ハードウェア フローオフロード
※ハードウェア NAT サポートが必要です。 mt7621 のみにおいて実装されています。
※MT7986B / WSR-600AX8 において有効
※MT7986A / BPI-R3 において有効

```bash
uci set firewall.@defaults[0].flow_offloading_hw='1'
uci commit firewall
/etc/init.d/firewall restart

```
:::

:::note
### [パケットステアリング](https://openwrt.org/docs/guide-user/base-system/basic-networking#global_section)
```bash
uci set network.globals.packet_steering='1'
uci commit network

```
:::

:::note
### [カスタムDNS](https://openwrt.org/docs/guide-user/base-system/dhcp_configuration#providing_custom_dns_with_dhcp)
```bash
# delete
uci -q delete dhcp.lan.dhcp_option
uci -q delete dhcp.lan.dns
# IPV4
uci add_list dhcp.lan.dhcp_option="6,1.1.1.1,8.8.8.8"
uci add_list dhcp.lan.dhcp_option="6,1.0.0.1,8.8.4.4"
# IPV6
uci add_list dhcp.lan.dns="2606:4700:4700::1111"
uci add_list dhcp.lan.dns="2001:4860:4860::8888"
uci add_list dhcp.lan.dns="2606:4700:4700::1001"
uci add_list dhcp.lan.dns="2001:4860:4860::8844"
# set
uci commit dhcp
/etc/init.d/dnsmasq restart
/etc/init.d/odhcpd restart

```
確認
- [Connection Information](https://1.1.1.1/help)
:::

:::note
### [Wi-Fi](https://openwrt.org/docs/guide-user/network/wifi/basic#mac80211_options)
※W53、W56はDFSにて起動遅延60秒

設定値
```bash
# Aチャンネル
SSID_A='SSID_A'
PASSWORD_A='password'
# Bチャンネル
SSID_B='SSID_B'
PASSWORD_B='password'

```
設定
※Random Mac AddressはRC2以降
※トライバンドは`radio2` `default_radio2`を追加
```bash
# Aチャンネル
uci set wireless.radio0.country='JP'
uci set wireless.radio0.txpower='10'
uci set wireless.radio0.noscan='1'
uci set wireless.radio0.frag='2347'
uci set wireless.radio0.rts='2346'
uci set wireless.default_radio0.encryption='sae-mixed'
uci set wireless.default_radio0.ssid=${SSID_A}
uci set wireless.default_radio0.key=${PASSWORD_A}
uci set wireless.default_radio0.multicast_to_unicast='1'
uci set wireless.default_radio0.isolate='1'
# uci set wireless.default_radio0.macaddr='random' # Random Mac Address
# uci set wireless.default_radio0.wpa_disable_eapol_key_retries='1'
# Bチャンネル
uci set wireless.radio1.country='JP'
uci set wireless.radio1.txpower='10'
uci set wireless.radio1.noscan='1'
uci set wireless.radio1.frag='2347'
uci set wireless.radio1.rts='2346'
uci set wireless.default_radio1.encryption='sae-mixed'
uci set wireless.default_radio1.ssid=${SSID_B}
uci set wireless.default_radio1.key=${PASSWORD_B}
uci set wireless.default_radio1.multicast_to_unicast='1'
uci set wireless.default_radio1.isolate='1'
# uci set wireless.default_radio1.macaddr='random' # Random Mac Address
# uci set wireless.default_radio1.wpa_disable_eapol_key_retries='1'
# set
uci delete wireless.radio0.disabled
uci delete wireless.radio1.disabled
uci commit wireless
/etc/init.d/network restart

```
- Target Wake Time (TWT) 
※Only WiFi6 (ax)
参考：Linux Hostpad
  - he_twt_required: Whether TWT is required
    0 = not required (default)
    1 = required
    he_twt_required=0 or 1
  - he_twt_responder: Whether TWT (HE) responder is enabled
    0 = disabled
    1 = enabled if supported by the driver (default)
    he_twt_responder=0 or 1

```bash:ON
sed -i -e "s/he_twt_required:0/he_twt_required:1/g" /lib/netifd/wireless/mac80211.sh
uci add_list wireless.radio0.hostapd_options='he_twt_responder=1'
uci add_list wireless.radio1.hostapd_options='he_twt_responder=1'
uci commit wireless

```
```bash:OFF
sed -i -e "s/he_twt_required:1/he_twt_required:0/g" /lib/netifd/wireless/mac80211.sh
uci del_list wireless.radio0.hostapd_options='he_twt_responder=0'
uci del_list wireless.radio1.hostapd_options='he_twt_responder=0'
uci commit wireless

```
確認
- Microsoft Store
  - [WiFi Analyzer](https://www.microsoft.com/store/apps/9NBLGGH33N0N)

他設定
- [iwchanによるWi-Fi自動チャンネル選択](https://qiita.com/site_u/items/f42be7c0953187b9428a#iwchan%E3%81%AB%E3%82%88%E3%82%8Bwi-fi%E8%87%AA%E5%8B%95%E3%83%81%E3%83%A3%E3%83%B3%E3%83%8D%E3%83%AB%E9%81%B8%E6%8A%9E)
- [アイロス DFS リセット](https://qiita.com/site_u/items/f42be7c0953187b9428a#%E3%82%A2%E3%82%A4%E3%83%AD%E3%82%B9-dfs-%E3%83%AA%E3%82%BB%E3%83%83%E3%83%88)
:::

:::note
### [バンドステアリング](https://openwrt.org/docs/guide-user/network/wifi/dawn)
設定値
```bash
BANDSTEERING='バンドステアリング' # 2Gと5GのSSIDを統一
PASSWORD='password' # 2Gと5Gのパスワードを統一

```
設定
```bash
# opkg update
opkg install dawn
opkg install luci-app-dawn
uci set dawn.@network[0].broadcast_ip='192.168.1.255'
uci set dawn.@metric[0].kicking=1
# 5G
uci set wireless.default_radio0.ssid=${BANDSTEERING}
uci set wireless.default_radio0.key=${PASSWORD}
uci set wireless.default_radio0.ieee80211r '1'
uci set wireless.default_radio0.ieee80211k='1'
uci set wireless.default_radio0.ieee80211v='1'
uci set wireless.default_radio0.ft_over_ds='1'
# 2G
uci set wireless.default_radio1.ssid=${BANDSTEERING}
uci set wireless.default_radio1.key=${PASSWORD}
uci set wireless.default_radio1.ieee80211r '1'
uci set wireless.default_radio1.ieee80211k='1'
uci set wireless.default_radio1.ieee80211v='1'
uci set wireless.default_radio1.ft_over_ds='1'
# set
uci commit dawn
uci commit wireless
/etc/init.d/dawn restart
/etc/init.d/network restart
# reboot

```
wpad(full)は入らないので書かない事
~~option bss_transition '1'~~
~~option wnm_sleep_mode '1'~~
~~option time_advertisement '2'~~
~~option time_zone 'GMT0'~~
~~option rrm_neighbor_report '1'~~
~~option rrm_beacon_report '1'~~
:::

::: note
### PING宛先
```bash
uci set luci.diag.dns='one.one.one.one'
uci set luci.diag.ping='one.one.one.one'
uci set luci.diag.route='one.one.one.one'
uci commit luci
/etc/init.d/rpcd reload
```
確認
- [LuCi@診断](http://192.168.1.1/cgi-bin/luci/admin/network/diagnostics)
:::
:::note
### [CRON(スケジュール)](https://openwrt.org/docs/guide-user/base-system/cron)

参考
```bash
# 再起動 06:00
echo "00 06 * * * sleep 70 && touch /etc/banner && reboot" >> /etc/crontabs/root
# WiFi A OFF 00:00
echo "00 00 * * * uci set wireless.default_radio0.disabled=1; uci commit; wifi" >> /etc/crontabs/root
# WiFi A ON 08:00
echo "00 08 * * * uci set wireless.default_radio0.disabled=0; uci commit; wifi" >> /etc/crontabs/root
# LED 夜間 OFF 00:00 ※再起動で設定値復旧
echo "00 00 * * * echo "none" > /sys/class/leds/white:power/trigger" >> /etc/crontabs/root
/etc/init.d/cron restart

```


参考：CPUパフォーマンスCRONスケジュールその1
※[CPUパフォーマンス](https://qiita.com/site_u/items/1197d2b7888dc4cf322c#cpu%E3%83%91%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%B3%E3%82%B9)インストール必須
※00:00 ~ 08:00 ローパフォーマンス
```bash
# opkg update
# opkg install cpu-perf
uci set cpu-perf.config.enabled=1
uci set cpu-perf.cpu0.scaling_governor='schedutil'
uci set cpu-perf.cpu0.scaling_max_freq='1017600'
uci set cpu-perf.cpu1.scaling_governor='schedutil'
uci set cpu-perf.cpu1.scaling_max_freq='1017600'
uci set cpu-perf.cpu2.scaling_governor='schedutil'
uci set cpu-perf.cpu2.scaling_max_freq='1017600'
uci set cpu-perf.cpu3.scaling_governor='schedutil'
uci set cpu-perf.cpu3.scaling_max_freq='1017600'
uci commit uhttpd
/etc/config/uhttpd reload
uci commit cpu-perf
/etc/init.d/cpu-perf start

# CPU パフォーマンス
echo "00 00 * * * /etc/init.d/cpu-perf start" >> /etc/crontabs/root
echo "00 08 * * * /etc/init.d/cpu-perf stop" >> /etc/crontabs/root

```
参考：CPUパフォーマンスCRONスケジュールその2
※[CPUパフォーマンス](https://qiita.com/site_u/items/1197d2b7888dc4cf322c#cpu%E3%83%91%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%B3%E3%82%B9)インストール必須
※00:00 ~ 08:00 ローパフォーマンス
```bash
# opkg update
# opkg install cpu-perf
/etc/init.d/cpu-perf start
uci set cpu-perf.config.enabled=1
uci commit cpu-perf
# CPU-PERF 1017600
echo "00 00 * * * uci set cpu-perf.cpu0.scaling_max_freq='1017600'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 00 * * * uci set cpu-perf.cpu1.scaling_max_freq='1017600'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 00 * * * uci set cpu-perf.cpu2.scaling_max_freq='1017600'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 00 * * * uci set cpu-perf.cpu3.scaling_max_freq='1017600'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 08 * * * /etc/init.d/cpu-perf reload" >> /etc/crontabs/root
# CPU-PERF 2208000
echo "00 08 * * * uci set cpu-perf.cpu0.scaling_max_freq='2208000'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 08 * * * uci set cpu-perf.cpu1.scaling_max_freq='2208000'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 08 * * * uci set cpu-perf.cpu2.scaling_max_freq='2208000'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 08 * * * uci set cpu-perf.cpu3.scaling_max_freq='2208000'; uci commit; cpu-perf" >> /etc/crontabs/root
echo "00 08 * * * /etc/init.d/cpu-perf reload" >> /etc/crontabs/root

```

スケジュール確認
```bash
crontab -l

```

:::

# 他デバイス設定
::: note
### [Wi-Fi](https://qiita.com/site_u/items/f42be7c0953187b9428a)
- [Dynamic frequency selection (DFS)](https://qiita.com/site_u/items/f42be7c0953187b9428a#dynamic-frequency-selection-dfs)

- [Guest Wi-Fi (QR code)](https://qiita.com/site_u/items/f42be7c0953187b9428a#guest-wi-fi-qr-code)

- [Target Wake Time (TWT)](https://qiita.com/site_u/items/f42be7c0953187b9428a#target-wake-time-twt)
:::

::: note
### [USB](https://qiita.com/site_u/items/597199882dc4d56c2385)

:::

::: note
### [ボタンとスイッチ](https://qiita.com/site_u/items/08764ce9473231482c17)

:::

# WAN
::: note
### [選択型インターネット接続自動設定](https://qiita.com/site_u/items/4b8076cb8c9b05bc3f9a)
階層
※それぞれの階層に設定とその復元がある
※一度設定すると、そのバックアップを取るので、復元が出来る仕様
- OCNバーチャルコネクト
  - ニチバン対策のみ 
- V6プラス・IPv6オプション
  - ニチバン対策のみ 
- トランジックス
- クロスパス
- v6 コネクト
- ひかりone系・他PPPoE
- アクセスポイント
- 終了
:::

::: note
### [DNS暗号化及び広告ブロッカー](https://qiita.com/site_u/items/cf34ea1ee9a1971272bc)
- [AdGuard](https://qiita.com/site_u/items/cf34ea1ee9a1971272bc#adguard)
- [HTTPS-DNS-PROXY](https://qiita.com/site_u/items/cf34ea1ee9a1971272bc#https-dns-proxy)
- [STUBBY](https://qiita.com/site_u/items/cf34ea1ee9a1971272bc#stubby)
- [AdBlock](https://qiita.com/site_u/items/cf34ea1ee9a1971272bc#adblock)
:::

# 初期化

:::note alert
### [ファクトリーリセット(初期化)](https://openwrt.org/docs/guide-user/troubleshooting/failsafe_and_factory_reset#soft_factory_reset)
```bash
# 要注意
firstboot && reboot now

```

`This will erase all settings and remove any installed packages. Are you sure? [N/y]`

- `y`

### [デバイスリセットボタン](https://openwrt.org/docs/guide-user/troubleshooting/failsafe_and_factory_reset#reset_button)
デバイスのリセットボタンを5秒押し続ける
:::

# あとがき


# 参考資料（感謝:relaxed:）