Openwrt_OpenSSH

🔐權限要求

網路來源 SSH 埠 使用者類型 密碼登入 金鑰登入 是否允許登入
內部網路 (192.168.99.0/24) 22 一般使用者 ✅ 允許 ✅ 允許 ✅ 允許
root ✅ 允許 ✅ 允許 ✅ 允許
外部網路(非 192.168.99.0/24 2222 一般使用者 ✅ 允許 ✅ 允許 ✅ 允許
root ❌ 禁止 ❌ 禁止 ❌ 禁止

✅ 1. 安裝 OpenSSH 並停用 Dropbear

opkg update
opkg install openssh-server
/etc/init.d/dropbear disable
/etc/init.d/dropbear stop
/etc/init.d/sshd enable
/etc/init.d/sshd start

✅ 2. 設定 SSH 登入規則

vi /etc/ssh/sshd_config

加入或修改以下內容:

Port 22	 		# OpenSSH 監聽內部網路的預設 SSH 埠(LAN 使用)
Port 2222		 # OpenSSH 監聽外部網路的 SSH 埠(WAN 使用)
# 禁止 root 從外部登入
Match Address 192.168.99.0/24 	# 套用以下設定給來自 192.168.99.x 的 IP(內部網路)
PermitRootLogin yes				 # 允許 root 使用者登入(僅限內部網路)
PasswordAuthentication yes 			# 允許使用密碼登入(僅限內部網路)
PubkeyAuthentication yes 			# 允許使用 SSH 金鑰登入(僅限內部網路)


Match Address !192.168.99.0/24		 # 套用以下設定給非 192.168.99.x 的 IP(外部網路)
PermitRootLogin no 					# 禁止 root 使用者登入(外部網路)
PasswordAuthentication yes 			# 允許一般使用者使用密碼登入(外部網路)
PubkeyAuthentication yes 			# 允許一般使用者使用 SSH 金鑰登入(外部網路)

重啟 OpenSSH

/etc/init.d/sshd restart

✅ 3. 設定防火牆規則(區分 22 與 2222 埠)

編輯 /etc/config/firewall

# 允許內部網路使用 port 22
config rule
option name 'Allow-SSH-Internal'
option src 'lan'
option dest_port '22'
option proto 'tcp'
option target 'ACCEPT'

# 允許外部網路使用 port 2222
config rule
option name 'Allow-SSH-External'
option src 'wan'
option dest_port '2222'
option proto 'tcp'
option target 'ACCEPT'

然後重新啟動防火牆:

/etc/init.d/firewall restart