Loading

在authorized_keys中加入自定义行为实现wakeonlan

需求

内网设备通过跳板机ssh proxyjump暴露在公网中
希望在跳板机不提供shell的情况下实现wakeonlan

方法

受到Gogs配置git用户authorized_keys的启发
可以加入commandenvironment指令控制某个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>即可进行跳转。

拓展

  1. 若要在authorized_keys中使用command="custom_cmd"参数则登陆脚本必须能够处理$SHELL -c custom_cmd
  2. 使用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

posted @ 2022-01-30 23:54  azureology  阅读(230)  评论(0)    收藏  举报