如何限制或限制对应用程序的访问?

Gre*_*era 18 permissions command-line scripts steam games

我儿子去年圣诞节收到了一台新笔记本电脑,主要用于他的新学校......现在他有了自己的笔记本电脑,他很想安装 Steam。

但是妻子要我删除 Steam,因为笔记本电脑主要是供学校使用的……如果我不需要,我宁愿不这样做。

有没有办法限制或以其他方式限制对 Steam 的访问?也许是 Steam 本身的密码,或者设置它可以运行的时间?

理想情况下,它必须是使用起来非常简单的东西,因为我的工作经常让我远离家(和电脑、电信和......)很长一段时间,而我的妻子在电脑周围不像我那么舒服,更容易使用好得多。

肯定有什么东西可以完成这项任务吗?

Jac*_*ijm 5

对流程或应用程序设置时间限制

通过一个小的后台脚本,您可以为进程或应用程序设置时间限制。
只要您的用户不知道管理员的密码,就不太容易被超越。

下面的解决方案

就是这么小的后台脚本。它将每天的使用限制在定义的分钟数内,并在脚本的头部设置。一旦设置完毕(这并不太困难),它运行起来非常容易,之后不需要任何额外的操作。

剧本

#!/usr/bin/python3
import subprocess
import os
import sys
import time

#--- set the time limit below (minutes)
minutes = 1
#--- set the process name to limit below
app = "gedit"

uselog = "/opt/limit/uselog"
datefile = "/opt/limit/currdate"

def read(f):
    try:
        return int(open(f).read().strip())
    except FileNotFoundError:
        pass

currday1 = read(datefile)

while True:
    time.sleep(10)
    currday2 = int(time.strftime("%d"))

    # check if the day has changed, to reset the used quantum
    if currday1 != currday2:
        open(datefile, "wt").write(str(currday2))
        try:
            os.remove(uselog)  
        except FileNotFoundError:
            pass

    try:
        # if the pid of the targeted process exists, add a "tick" to the used quantum
        pid = subprocess.check_output(["pgrep", app]).decode("utf-8").strip()
        n = read(uselog)
        n = n + 1 if n != None else 0
        # when time exceeds the permitted amount, kill the process
        if n > minutes*6: 
            subprocess.Popen(["kill", pid])
        open(uselog, "wt").write(str(n))
    except subprocess.CalledProcessError:
        pass

    currday1 = currday2
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 在您的桌面(或其他任何地方)上,创建一个名为:limit
  2. 将脚本复制到一个空文件中,将其另存为limit_use(无扩展名)文件夹内使其可执行
  3. 在脚本的头部编辑要限制的进程名称以及允许的最大分钟数。在示例中:

    #--- set the time limit below (minutes)
    minutes = 1
    #--- set the process name to limit below
    app = "gedit"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将文件夹复制到目录/opt

    cp -r /path/to/limit /opt
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在编辑/etc/rc.local以使脚本在启动时运行root

    sudo -i gedit /etc/rc.local
    
    Run Code Online (Sandbox Code Playgroud)

    就在排队之前

    exit 0
    
    Run Code Online (Sandbox Code Playgroud)

    另一行:

    /opt/limit/limit_use &
    
    Run Code Online (Sandbox Code Playgroud)

就是这样

当有人试图杀死后台脚本时:

在此输入图像描述

(不允许的动作)

解释; 怎么运行的

  • 该脚本每 10 秒检查一次目标进程是否正在运行。如果是这样,它会在总使用量中“添加”一“点”,并记录在文件中 ( /opt/limit/uselog)。如果达到每日限制,脚本将不再允许该进程运行,如果存在则将其终止。
  • 在日期更改时(日期记录在文件中,因此重新启动将无济于事),日志文件将被删除,从而允许建立新的使用时间量。
  • 由于脚本在启动时运行,因此rc.local只有具有 sudo 权限的用户才能停止脚本,即使只有在用户知道进程名称的情况下也是如此。

停止脚本

如果您想停止脚本,请使用以下命令:

sudo kill "$(pgrep limit_use)"
Run Code Online (Sandbox Code Playgroud)

但同样,您需要 sudo 密码才能执行此操作。




编辑

尽管上面的脚本应该提供一种相当安全的方式来限制应用程序的使用,正如 @Bytecommander 所提到的,但它可以被超越,尽管不是很容易。结合下面的措施将使这种情况发生的可能性很小,除非你的儿子知道设置,并且对 Linux/Ubuntu 有丰富的经验。

附加措施

下面的附加措施距离“简单解决方案”稍远一些,但设置起来仍然不太困难。如果我们怀疑的犯罪分子发现脚本是从 调用的/etc/rc.local,会设法成为 root,并删除 中的行/etc/rc.local或者能够以这种方式停止脚本,我们可以让他面对下一个问题:屏幕在之后黑屏此外,该解决方案会在重启后 5 分钟后检查后台脚本是否正在运行,如果没有运行则停止运行。

额外的措施是启动 - 检查该行是否/opt/limit/limit_use &存在于 中/etc/rc.local在 5 分钟后检查脚本是否仍在运行。由于脚本从(隐藏在启动应用程序中)启动器运行,/etc/xdg/autostart因此很难找出正在发生的情况,除非您知道它是如何完成的。这两项措施的结合使您的儿子不太可能发现,即使他发现了,也可能没有什么可以阻止他。

如何设置

涉及两个简单的步骤:

  1. 将以下代码复制到一个空文件中,将其保存blackout.desktop在桌面上:

    [Desktop Entry]
    Name=not allowed
    Exec=/bin/bash -c "sleep 15 && /usr/local/bin/blackout.py"
    Type=Application
    Terminal=false
    NoDisplay=true
    
    Run Code Online (Sandbox Code Playgroud)

    将文件复制到/etc/xdg/autostart

    sudo cp /path/to/blackout.desktop /etc/xdg/autostart
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将下面的脚本复制到一个空文件中,将其保存blackout.py在桌面上,使其可执行并将其复制到/usr/local/bin

    cp /path/to/blackout.py /usr/local/bin
    
    Run Code Online (Sandbox Code Playgroud)

    剧本

    #--- set the time limit below (minutes)
    minutes = 1
    #--- set the process name to limit below
    app = "gedit"
    
    Run Code Online (Sandbox Code Playgroud)

解释

中的启动器/etc/xdg/autostart将为所有用户启动一个应用程序(在本例中为额外的安全检查)。这可以在本地覆盖,但您必须知道检查运行。通过将该行NoDisplay=true放入我们的启动器中,它不会出现在本地Startup Applications,因此在不知道它存在的情况下,它不太可能被发现。

此外,你的儿子只有 15 秒的时间来找出答案(然后屏幕就黑了),所以他会遇到一个严重的问题,除非他是一个天才,有丰富的 Ubuntu 经验和创造性思维。