如果终端具有 sudo 权限,如何警告并显示还剩多少时间?

Aqu*_*wer 10 sudo

How to see in the terminal that you have sudo privileges? (so knowing I have these privileges, I can just type sudo -k) It would be better to not add too much information into the terminal also, just some kind of simple and small alert!

REMAINING TIME IS IMPOSSIBLE, right?

Also, how to see how much time I still have with those privileges on that specific terminal? (so I can just let it end instead of typing sudo -k). I think that remaining time could show up each time I press Enter key.

EDIT: I researched more and found that /var/lib/sudo/$USER/$nPts (where $nPts is current terminal pts as of ps -p $$) has its timestamp updated everytime a sudo command is issued, and it is only accessible if you use a sudo command, so the remaining time will always be the configured one...

PS。:或者,我尝试过,alias sudo='sudo -k'但我不喜欢为每个命令输入密码......而且,如果你输入了这个alias sudos='sudo -k'(或者alias ssudo='sudo -k',如果你输入了很长的一行,只需按 Home 并输入 's')就可以工作只想通过键入发出一个命令sudos。但是我仍然想知道什么时候我有正常的 sudo 权限才能知道我需要采取行动......

Aqu*_*wer 5

【我还是想知道有没有更好的回答。】

我找到了一种有效的方法,并且提示将保留在一行中:

在你的最后~/.bashrc添加这个:

function FUNCsudoOn() { 
    if sudo -n uptime 2>/dev/null 1>/dev/null; then 
        echo -ne "\E[0m\E[93m\E[41m\E[1m\E[5m SUDO \E[0m"; 
        #echo #without newline, the terminal seems to bugout with lines that are too big... discomment this if you find any problems...
    fi; 
}
function FUNCpromptCommand () { 
    FUNCsudoOn
}
export PROMPT_COMMAND=FUNCpromptCommand
#export PS1="\`FUNCsudoOn\`$PS1" #this also works, use instead of PROMPT_COMMAND
Run Code Online (Sandbox Code Playgroud)

编辑:我发现这sudo -n uptime会更新 sudo 超时,所以每次你按下 Enter 键时,sudo 时间都会更新......这使得知道剩余时间没用,因为它总是配置的,默认为 15 分钟。 .

并找到适合您口味的最佳颜色格式,您可以将ScriptEchoColor与以下--escapedchars选项一起使用:

echoc --escapedchars "@{nRlyo} SUDO " #that outputs below...
echo -e "\E[0m\E[93m\E[41m\E[1m\E[5m SUDO \E[0m"
Run Code Online (Sandbox Code Playgroud)

只是停止闪烁删除\E[5m\E[0m\E[93m\E[41m\E[1m SUDO \E[0m


wal*_*tor 4

你的终端没有“sudo权限”,除非你做了一些愚蠢的事情,比如sudo bash 不要这样做!

要“拥有正常的 sudo 权限”,您所要做的就是不要sudo在命令前面键入内容。

/usr/bin/id是看看会发生什么的好方法sudo。例如:

id -a        # returns my UID info
sudo id -a   # returns root's info
id -a        # me again
sudo id -a   # root again
Run Code Online (Sandbox Code Playgroud)

手册sudo页显示“sudoers 策略会将凭据缓存 15 分钟,除非在 sudoers(5) 中被覆盖”。这意味着,默认情况下,如果您发出命令sudo(并输入密码),然后等待 14.9 分钟后再发出第二条sudo命令,则无需再次输入密码。如果等待 15.1 分钟,则必须重新输入密码。sudo -k只需立即使 15 分钟计时器到期即可。我能看到的唯一用途sudo -k是,如果您要将解锁的终端交给您不信任的人UID 0