【问题标题】:Execute shell script everytime a new user is created每次创建新用户时执行 shell 脚本
【发布时间】:2013-03-12 03:57:12
【问题描述】:

每次添加新用户时执行命令的最简单方法是什么?

我检查了/etc/adduser.conf,但里面没有任何东西看起来可以做到这一点?

我考虑将它添加到用户.bashrc,以便在他们登录时执行,但我宁愿在他们登录之前设置它。

任何帮助将不胜感激。

【问题讨论】:

  • 如果你想让它在登录 shell 上运行,.bash_profile 是放置它的地方。如果你把它放在.bashrc 中,它将在它们启动非登录shell 时运行(如果.bash_profile 包含.bashrc,则在登录shell 中)。
  • @lnductiveload 就是这样......这些脚本只会在新用户实际登录时运行 - 我希望脚本在添加新用户后立即运行。

标签: bash shell ubuntu


【解决方案1】:

来自man adduser

   If  the  file  /usr/local/sbin/adduser.local exists, it will be exe‐
   cuted after the user account has been set up  in  order  to  do  any
   local setup.  The arguments passed to adduser.local are:
   username uid gid home-directory

看来您可以在此处添加用户创建操作。

【讨论】:

  • 您可能还想chmod +x adduser.local
  • 这可能是特定于 Debian 及其后代的发行版,至少在 RHEL 等人中不存在。
  • 我们可以从adduser.local 运行另一个chmod +x shell 脚本吗?我正在尝试,但它无法找到 .sh 文件,shell 脚本在终端中运行良好。
  • @UlrichSchwarz,你有没有设法在 RHEL/Centos 机器上做同样的事情?
【解决方案2】:

几年前我这样做了,编写了一个自定义脚本,它可以满足我的所有需求,并在脚本中调用adduser 以将用户添加到系统中。

【讨论】:

    【解决方案3】:

    我不知道直接方法... 但是,您可以检查/etc/passwd 文件或/home directrory 中的更改,例如使用inotifywait 或使用一些cron 作业。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 2014-03-25
      • 1970-01-01
      • 2018-09-06
      • 2022-01-24
      相关资源
      最近更新 更多