当用户通过 ssh 连接时运行脚本

当用户通过 ssh 连接时运行脚本

我正在安装服务器;我正在为 SSH 流量做准备。
我希望每次来自某个组的用户连接时运行一个脚本(与新连接的用户的对话框)。
我该如何做到这一点(有点像 SSH 挂钩帖子中的那样)?

我想要一个特定的群体,我将其称为用户,他们只想启动游戏服务器或类似的东西,能够以最小的头痛来做到这一点。我的想法是制作一个脚本,每次他们连接时都会启动该脚本,握住他们的手并允许他们执行不同的操作,所有这些都以游戏服务器为中心。我不希望管理员每次都被该脚本打扰。

答案1

为什么不与/etc/配置文件服务器上的文件,有一个条件吗?

例如:仅适用于团体玩家, 剧本/etc/profile-grp/玩家被执行(源自/etc/profile)。它将日期写入用户目录的日志文件(~/log)中。

grp=player

# Write a script
sudo mkdir /etc/profile-grp
echo "date >> ~/log" | sudo tee /etc/profile-grp/${grp}

# Script permissions
sudo chown root:${grp} /etc/profile-grp/${grp}
sudo chmod 640 /etc/profile-grp/${grp}

# In /etc/profile
echo " 
if ( groups | grep ${grp} > /dev/null); then
   . /etc/profile-grp/${grp}
fi" | sudo tee -a /etc/profile

unset grp
  • 只有玩家组(和root)可以读取脚本。
  • 自由编辑脚本。只有 root 才能更改它。

答案2

一些选项:

  • 更改每个用户的登录shell,为交互脚本。
  • 编辑/复制每个用户的 .profile,每次 bash 启动时都会运行该配置文件,当它停止用户获取提示时。
  • 编辑用户 ~/.ssh/authorized_keys,以便告诉 ssh 在用户连接时运行特殊命令。这类似于选项 1,但ssh仅此而已。它还可以用于允许许多人使用不同的密钥登录到同一帐户,但具有不同的操作(用户分离,按操作)。

相关内容