需求
内网设备通过跳板机ssh proxyjump暴露在公网中
希望在跳板机不提供shell的情况下实现wakeonlan
方法
受到Gogs配置git用户authorized_keys
的启发
可以加入command
和environment
指令控制某个key的具体登陆行为
实现
禁止登陆
将跳板用户jump
的登录shell设定为/home/jump/nologin
并写入以下内容
#!/bin/bash
echo "Hi there! You've successfully authenticated, but we do not provide shell access."
# wake on lan if mac address is set via $MAC
if [ ! -z ${MAC+x} ];then
wakeonlan $MAC > /dev/null 2>&1 &
fi
当检测到$MAC
变量存在时发起唤醒
设置变量
在/home/jump/.ssh/authorized_keys
中加入环境变量
environment="MAC=11:22:33:44:55:66" ssh-ed25519 AAAAXXXXXXXXXXXXXXXXX name@host
激活配置
默认environment
参数被禁用,需要手动开启sshd_config
中对应设置
sudo vi /etc/ssh/sshd_config
# change the following settings
PasswordAuthentication no
PubkeyAuthentication yes
PermitUserEnvironment yes
# restart sshd service
sudo systemctl restart sshd
测试效果
发起ssh连接,跳板机不返回shell但远程终端已被唤醒。
ssh jump@ip.jump.host -p <port>
后续对对~/.ssh/config
传入ProxyJump jump@ip.jump.host:<port>
即可进行跳转。
拓展
- 若要在
authorized_keys
中使用command="custom_cmd"
参数则登陆脚本必须能够处理$SHELL -c custom_cmd
- 使用
ProxyJump
时jump host将在完成认证后直接进行端口转发,跳过login shell
,command
以及ssh rc
,若一定要作为jump host执行脚本可尝试使用AuthorizedKeysCommand
在认证阶段进行处理,可供使用的变量如下
# /etc/ssh/sshd_config
AuthorizedKeysCommand /opt/ssh_auth.sh %u %k %t %f %h
# %u = username
# %k = pubkey
# %t = keytype
# %f = fingerprint
# %h = home
参考
authorized_keys
sshd_config(5) - OpenBSD manual pages
SSH AuthorizedKeysCommand 配置和坑 | Blog·Tanky Woo