【问题标题】:how to execute bash script inside vps remotely如何在 vps 中远程执行 bash 脚本
【发布时间】:2022-01-21 11:34:44
【问题描述】:

我的任务是让第二个开发人员能够重新启动 gunicorn 服务,而无需让他访问整个虚拟机,他可以通过 ftp 在他的项目目录中上传他的项目文件,但他无法重新启动gunicorn 本人。

我的“script.sh”是:

sudo service gunicorn restart

我可以用这个命令在终端自己执行 script.sh,它工作正常:
./script.sh

如何远程执行此脚本?也许通过网址? 或者如果有其他更好的方法来完成这个任务,请分享

谢谢!

【问题讨论】:

  • 这么多种方法。您可以编写一个服务,当文件上传到特定目录时,它会检测到并执行您想要的操作。您可以使用某些服务设置 http REST api。您可以编写自定义协议服务。您可以使用特殊的 sudo 访问权限授予对开发的 ssh 访问权限,以仅触发此特定操作。您可以设置一个服务,当您收到一封特殊的电子邮件时,您将从开发人员那里收到,它将读取电子邮件并执行操作。范围太广 - 您想要如何解决问题?
  • 所有这些方式看起来都很有趣!如果你能说得更具体一些,也许有一些指南\文章\视频的链接,我会很高兴的!我最喜欢你的两个想法,一个是编写一个检测特定目录中上传文件的服务。其次是授予开发人员访问权限,他只能使用该访问权限触发特定命令。提前谢谢!

标签: bash virtual-machine vps


【解决方案1】:

我最喜欢你的两个想法,一个是编写一个检测特定目录中上传文件的服务

基本上是一个脚本:

inotifywait /the/dir -m -e CREATE,CLOSE | while read -f action file; do 
      rm "$file"
      systemctl restart that_thing
done

可能会对特定文件名进行一些过滤和/或密码检查,例如文件名的内容必须是一些密码。

如果您不使用 Linux,您可以每秒左右轮询文件是否存在,而不是 inotifywait。

man inotifywaithttps://mywiki.wooledge.org/BashFAQ/001 + https://mywiki.wooledge.org/BashGuide。我怀疑您的服务器使用 rc-scripts ,现在是 2021 年,考虑迁移到 systemd。 https://www.linode.com/docs/guides/introduction-to-systemctl/ + https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/ .

其次是授予开发人员访问权限,通过该访问权限他只能触发特定命令

设置ssh服务器,给开发者一个用户账号,安装sudo然后在/etc/sudoers.d/other_developer创建一个文件,内容如下:

otherdeveoperuseraccount ALL=(root) NOPASSWD: /usr/bin/systemctl restart that_service

然后其他开发人员将能够运行该特定命令。

请参阅 man sudoman sudoershttps://unix.stackexchange.com/questions/279125/allow-user-to-run-a-command-with-arguments-which-contains-spaces 。总而言之,关于如何设置ssh服务器和添加用户帐号,感兴趣的是一些Linux电脑管理的基本介绍——网上肯定是层出不穷。

【讨论】:

  • 用户是否有权使用 sudoers 条目通过 ssh 运行标准 shell 命令,例如 ls
  • Will the user have access to run standard shell commands like ls over ssh with the sudoers entry? 是的。听起来不错——他是开发人员,而不是用户。你可以设置一个 chroot 来限制对 dbus 和 systemctl 的访问。
  • 非常感谢您提供如此详细的回答!我很感激!第二个变体运行良好,我只更改为:developername ALL=(ALL) NOPASSWD: /usr/sbin/service gunicorn restart。我肯定也会尝试 ftp 变体,它看起来更难,但更安全。无法提高您的答案,没有足够的声誉
  • 不过,如果您正在寻找一个真正的设置,最好设置一个 Ci/CD 管道(例如作为 GitLab 的一部分)以在每次提交时自动执行所有重新启动光盘。或者例如使用 Docker 或 Kubernetes 进行部署。
【解决方案2】:

一种方式:

  1. 创建具有最低权限的用户。 (不幸的是,一些 shell 特权是 req)。
  2. 覆盖The ONLY command,它会为此用户强制在 ssh 上运行。
# 1. Create a user with shell = /bin/sh & no home-dir
sudo adduser limiteduser --shell=/bin/sh --no-create-home

# 2. Restrict further in sshd_config
nano /etc/ssh/sshd_config

## > Add the following at the end
Match user limiteduser
  ForceCommand /path/to/restart-script
  # ForceCommand ls    # Tried this for testing.

P.S:一些副作用包括limiteduser 可以访问隧道端口。由于用户已经拥有 FTP 访问权限;我假设安全要求不是很严格。


另一种方式 - 因为存在 FTP 访问。

  • 当用户完成 FTP 上传后,请他在以下位置创建一个文件:/path/to/ftp/folder/request-restart.txt
  • [Per-minute] 编写一个 cron 作业来检查该文件并运行重启脚本并删除该文件。
    • @kamilkuk 的答案中的 inotify 脚本是一个更好的实时版本。使用 cron 的最大分辨率为 1 分钟。
  • 这可以扩展为用户提供更多request-some-other-command
# cron-watch-n-restart-script.sh
FILE="/path/to/ftp/folder/request-restart.txt"
if [[ -f "$FILE" ]]; then
    /path/to/restart-script.sh
    rm $FILE
fi

# Cron job entry (crontab -e): every minute
* * * * * /path/to/cron-watch-n-restart-script.sh

【讨论】:

  • 这实际上非常方便,我只更改了 cron 任务。在这里添加它“nano /etc/crontab”,这是工作“* * * * * root /bin/bash /home/admin/agent/restart.sh”,谢谢!
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 2021-06-15
  • 2018-09-15
  • 1970-01-01
  • 2012-05-30
  • 2014-08-29
  • 1970-01-01
  • 2010-10-10
相关资源
最近更新 更多