Bash

sudo 使終端崩潰,但在 .sh 文件中工作

  • December 24, 2021

2021 年 12 月 24 日更新

我不得不在接受的答案中找到CtrlCgrep 程式碼來嘗試找到罪魁禍首,以便由於其他原因重新啟動,但是一旦我找到了根本問題,我會更新這篇文章。

但是,我使用接受的答案的重新定義 sudo 程式碼來解決問題,我可以 sudo 離開而不會崩潰!

因此,對於在我找到源問題之前可能遇到問題的其他任何人,這就是我所做的:

.bashrc

我的 .bashrc 文件的底部

# https://unix.stackexchange.com/a/683619/344030
if [ -f ~/.sudo-fix ]; then
   source ~/.sudo-fix
else
 echo '🚨 Unable to find ~/.sudo-fix so sudo may break again. look in Dropbox/Projects/hplaptop/sudo-broken/.sudo-fix for file to copy to home'
fi

.sudo 修復

#!/bin/bash

#
# hplaptop fix only - https://unix.stackexchange.com/a/683619/344030
#

# Redefine sudo to reuse the last command if no args
sudo() {
   if [ $# -gt 0 ]
   then command sudo "$@"
   else command sudo "${BASH:-bash}" -c "$(history -p '!!')"
   fi
}

謝謝@roaima

2021 年 12 月 23 日更新

從評論中的一些問題來看:

我可以以 root 身份登錄嗎?:

我可以並且已經做到了,因為它是 Ubuntu,root 使用者被禁用,但我能夠在我的test.sh添加中sudo passwd root給自己一個密碼。

sudo passwd -l root然後,一旦這一切都解決了,我將直接在終端中再次禁用它,因為問題將得到解決,或者test.sh如果我必須堅持這種奇怪的解決方法,則再次禁用它。

sudo date 也會觸發這個嗎?

sudo date的也會觸發問題,除非我說 sudo 命令在 .sh 中並被呼叫,否則任何 sudo 命令都會這樣做。

bash 和 sh 的區別

在我的 test.sh 文件中更改 shebang 沒有任何區別。它仍然有效。

-#!/bin/bash
+#!/bin/sh

殼牌資訊

echo "$SHELL"
/bin/bash

請將執行結果添加type sudo到您的問題中

公平地說,這不是我所期望的。

type sudo
sudo is a function
sudo () 
{ 
   if [ "$1" ]; then
       sudo $@;
   else
       sudo "$BASH" -c "$(history -p !!)";
   fi
}

由於這不是我所期望的,所以我執行以下命令來查看該 sudo 函式是否在某個文件中,但它僅在該文章的降價文件中。

我也跑了 which sudo

rg -l -F 'sudo ()'
Dropbox/Projects/hplaptop/sudo-broken/readme.md

which sudo
/usr/bin/sudo

strace 須藤

在我提到的原始問題中strace sudo,為了完整起見,它是

strace sudo
execve("/usr/bin/sudo", ["sudo"], 0x7ffff0ccfa10 /* 74 vars */) = 0
access("/etc/suid-debug", F_OK)         = -1 ENOENT (No such file or directory)
brk(NULL)                               = 0x55fb4fd7e000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffe69316ac0) = -1 EINVAL (Invalid argument)
fcntl(0, F_GETFD)                       = 0
fcntl(1, F_GETFD)                       = 0
fcntl(2, F_GETFD)                       = 0
access("/etc/suid-debug", F_OK)         = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/tls/haswell/x86_64/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/tls/haswell/x86_64", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/tls/haswell/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/tls/haswell", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/tls/x86_64/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/tls/x86_64", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/tls/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/tls", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/haswell/x86_64/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/haswell/x86_64", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/haswell/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/haswell", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/x86_64/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo/x86_64", 0x7ffe69315d10) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/sudo/libaudit.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/sudo", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=142209, ...}) = 0
mmap(NULL, 142209, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2bea9e9000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libaudit.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\3407\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=133200, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2bea9e7000
mmap(NULL, 176296, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea9bb000
mprotect(0x7f2bea9be000, 118784, PROT_NONE) = 0
mmap(0x7f2bea9be000, 32768, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f2bea9be000
mmap(0x7f2bea9c6000, 81920, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xb000) = 0x7f2bea9c6000
mmap(0x7f2bea9db000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7f2bea9db000
mmap(0x7f2bea9dd000, 37032, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2bea9dd000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/sudo/libselinux.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@p\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=163200, ...}) = 0
mmap(NULL, 174600, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea990000
mprotect(0x7f2bea996000, 135168, PROT_NONE) = 0
mmap(0x7f2bea996000, 102400, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x6000) = 0x7f2bea996000
mmap(0x7f2bea9af000, 28672, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7f2bea9af000
mmap(0x7f2bea9b7000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x26000) = 0x7f2bea9b7000
mmap(0x7f2bea9b9000, 6664, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2bea9b9000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/sudo/libutil.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libutil.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\23\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=14848, ...}) = 0
mmap(NULL, 16656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea98b000
mmap(0x7f2bea98c000, 4096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1000) = 0x7f2bea98c000
mmap(0x7f2bea98d000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f2bea98d000
mmap(0x7f2bea98e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f2bea98e000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/sudo/libsudo_util.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@[\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=105312, ...}) = 0
mmap(NULL, 108824, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea970000
mmap(0x7f2bea975000, 61440, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7f2bea975000
mmap(0x7f2bea984000, 20480, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f2bea984000
mmap(0x7f2bea989000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18000) = 0x7f2bea989000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/sudo/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360q\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\t\233\222%\274\260\320\31\331\326\10\204\276X>\263"..., 68, 880) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=2029224, ...}) = 0
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\t\233\222%\274\260\320\31\331\326\10\204\276X>\263"..., 68, 880) = 68
mmap(NULL, 2036952, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea77e000
mprotect(0x7f2bea7a3000, 1847296, PROT_NONE) = 0
mmap(0x7f2bea7a3000, 1540096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x25000) = 0x7f2bea7a3000
mmap(0x7f2bea91b000, 303104, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19d000) = 0x7f2bea91b000
mmap(0x7f2bea966000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f2bea966000
mmap(0x7f2bea96c000, 13528, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2bea96c000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libcap-ng.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220$\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=27064, ...}) = 0
mmap(NULL, 28984, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea776000
mmap(0x7f2bea778000, 12288, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f2bea778000
mmap(0x7f2bea77b000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7f2bea77b000
mmap(0x7f2bea77c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x5000) = 0x7f2bea77c000
close(3)                                = 0
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libpcre2-8.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\"\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=584392, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2bea774000
mmap(NULL, 586536, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea6e4000
mmap(0x7f2bea6e6000, 409600, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f2bea6e6000
mmap(0x7f2bea74a000, 163840, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x66000) = 0x7f2bea74a000
mmap(0x7f2bea772000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x8d000) = 0x7f2bea772000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \22\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=18816, ...}) = 0
mmap(NULL, 20752, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea6de000
mmap(0x7f2bea6df000, 8192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1000) = 0x7f2bea6df000
mmap(0x7f2bea6e1000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f2bea6e1000
mmap(0x7f2bea6e2000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f2bea6e2000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\201\0\0\0\0\0\0"..., 832) = 832
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\345Ga\367\265T\320\374\301V)Yf]\223\337"..., 68, 824) = 68
fstat(3, {st_mode=S_IFREG|0755, st_size=157224, ...}) = 0
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\345Ga\367\265T\320\374\301V)Yf]\223\337"..., 68, 824) = 68
mmap(NULL, 140408, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f2bea6bb000
mmap(0x7f2bea6c2000, 69632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x7000) = 0x7f2bea6c2000
mmap(0x7f2bea6d3000, 20480, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18000) = 0x7f2bea6d3000
mmap(0x7f2bea6d8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c000) = 0x7f2bea6d8000
mmap(0x7f2bea6da000, 13432, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f2bea6da000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f2bea6b9000
arch_prctl(ARCH_SET_FS, 0x7f2bea6b9c80) = 0
mprotect(0x7f2bea966000, 12288, PROT_READ) = 0
mprotect(0x7f2bea6d8000, 4096, PROT_READ) = 0
mprotect(0x7f2bea6e2000, 4096, PROT_READ) = 0
mprotect(0x7f2bea772000, 4096, PROT_READ) = 0
mprotect(0x7f2bea77c000, 4096, PROT_READ) = 0
mprotect(0x7f2bea989000, 4096, PROT_READ) = 0
mprotect(0x7f2bea98e000, 4096, PROT_READ) = 0
mprotect(0x7f2bea9b7000, 4096, PROT_READ) = 0
mprotect(0x7f2bea9db000, 4096, PROT_READ) = 0
mprotect(0x55fb4fcaa000, 4096, PROT_READ) = 0
mprotect(0x7f2beaa39000, 4096, PROT_READ) = 0
munmap(0x7f2bea9e9000, 142209)          = 0
set_tid_address(0x7f2bea6b9f50)         = 321158
set_robust_list(0x7f2bea6b9f60, 24)     = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7f2bea6c2bf0, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f2bea6d03c0}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7f2bea6c2c90, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f2bea6d03c0}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
statfs("/sys/fs/selinux", 0x7ffe69316a10) = -1 ENOENT (No such file or directory)
statfs("/selinux", 0x7ffe69316a10)      = -1 ENOENT (No such file or directory)
brk(NULL)                               = 0x55fb4fd7e000
brk(0x55fb4fd9f000)                     = 0x55fb4fd9f000
openat(AT_FDCWD, "/proc/filesystems", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
read(3, "nodev\tsysfs\nnodev\ttmpfs\nnodev\tbd"..., 1024) = 418
read(3, "", 1024)                       = 0
close(3)                                = 0
access("/etc/selinux/config", F_OK)     = -1 ENOENT (No such file or directory)
prlimit64(0, RLIMIT_AS, NULL, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_AS, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = 0
prlimit64(0, RLIMIT_CPU, NULL, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_CPU, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = 0
prlimit64(0, RLIMIT_DATA, NULL, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_DATA, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = 0
prlimit64(0, RLIMIT_FSIZE, NULL, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_FSIZE, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = 0
prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024, rlim_max=1024*1024}) = 0
prlimit64(0, RLIMIT_NOFILE, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = -1 EPERM (Operation not permitted)
prlimit64(0, RLIMIT_NOFILE, {rlim_cur=256, rlim_max=RLIM64_INFINITY}, NULL) = -1 EPERM (Operation not permitted)
prlimit64(0, RLIMIT_NOFILE, {rlim_cur=1024*1024, rlim_max=1024*1024}, NULL) = 0
prlimit64(0, RLIMIT_NPROC, NULL, {rlim_cur=63493, rlim_max=63493}) = 0
prlimit64(0, RLIMIT_NPROC, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = -1 EPERM (Operation not permitted)
prlimit64(0, RLIMIT_NPROC, {rlim_cur=63493, rlim_max=63493}, NULL) = 0
prlimit64(0, RLIMIT_RSS, NULL, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_RSS, {rlim_cur=RLIM64_INFINITY, rlim_max=RLIM64_INFINITY}, NULL) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}, NULL) = 0
fcntl(0, F_GETFL)                       = 0x402 (flags O_RDWR|O_APPEND)
fcntl(1, F_GETFL)                       = 0x402 (flags O_RDWR|O_APPEND)
fcntl(2, F_GETFL)                       = 0x402 (flags O_RDWR|O_APPEND)
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=5699248, ...}) = 0
mmap(NULL, 5699248, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2bea149000
close(3)                                = 0
openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=3678, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=3678, ...}) = 0
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\10\0\0\0\10\0\0\0\0"..., 4096) = 3678
lseek(3, -2338, SEEK_CUR)               = 1340
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\10\0\0\0\10\0\0\0\0"..., 4096) = 2338
close(3)                                = 0
stat("/etc/sudo.conf", 0x7ffe69316710)  = -1 ENOENT (No such file or directory)
geteuid()                               = 1000
access("/home/solrevdev/.cargo/bin/sudo", X_OK) = -1 ENOENT (No such file or directory)
access("/home/solrevdev/.cargo/bin/sudo", X_OK) = -1 ENOENT (No such file or directory)
access("/home/solrevdev/.local/bin/sudo", X_OK) = -1 ENOENT (No such file or directory)
access("/home/solrevdev/.cargo/bin/sudo", X_OK) = -1 ENOENT (No such file or directory)
access("/home/solrevdev/.local/bin/sudo", X_OK) = -1 ENOENT (No such file or directory)
access("/usr/local/sbin/sudo", X_OK)    = -1 ENOENT (No such file or directory)
access("/usr/local/bin/sudo", X_OK)     = -1 ENOENT (No such file or directory)
access("/usr/sbin/sudo", X_OK)          = -1 ENOENT (No such file or directory)
access("/usr/bin/sudo", X_OK)           = 0
stat("/usr/bin/sudo", {st_mode=S_IFREG|S_ISUID|0755, st_size=166056, ...}) = 0
openat(AT_FDCWD, "/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=2996, ...}) = 0
read(3, "# Locale name alias data base.\n#"..., 4096) = 2996
read(3, "", 4096)                       = 0
close(3)                                = 0
openat(AT_FDCWD, "/usr/share/locale/en_GB/LC_MESSAGES/sudo.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale/en/LC_MESSAGES/sudo.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/share/locale-langpack/en_GB/LC_MESSAGES/sudo.mo", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1571, ...}) = 0
mmap(NULL, 1571, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f2beaa38000
close(3)                                = 0
openat(AT_FDCWD, "/usr/share/locale-langpack/en/LC_MESSAGES/sudo.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "sudo", 4sudo)                     = 4
write(2, ": ", 2: )                       = 2
write(2, "effective uid is not 0, is /usr/"..., 133effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?) = 133
write(2, "\n", 1
)                       = 1
exit_group(1)                           = ?
+++ exited with 1 +++

在我執行 Ubuntu 20.04 的筆記型電腦上開始發生一些奇怪的事情

當我打開一個終端(預設終端或終結者)並嘗試執行任何 sudo 命令時,例如sudo ls它將掛起,然後關閉/崩潰終端。

它是一致的並且可以在重新啟動等情況下繼續存在。

它甚至沒有進入要求輸入密碼的階段。

但是,如果我創建一個 test.sh 文件並執行它,例如,./test.sh那麼任何 sudo 命令都將按預期工作。它會要求輸入密碼然後工作。

例如,這有效:

test.sh

#!/bin/bash
echo 'sudo will work from here and ask for password and list dir'
echo '----------------------------------------------------------'
echo ''
echo ''

sudo ls

但是在終端視窗中,這會崩潰:

sudo ls

我用Google搜尋過,Stack Overflow 搜尋了我能想到的一切,我沒有安裝任何新東西,因為筆記型電腦已經有一段時間沒有做任何事情了。

我已經執行strace sudo,這導致我嘗試了一個超級使用者文章,但這沒有用:

https://superuser.com/a/1580324

Hold space when booting Pop!_OS
Select Recovery, which brings up the OS installer
SUPER + T to bring up terminal
sudo -i to switch to root
mount -o remount,rw /
mount --all
chown root:root /usr/bin/sudo
chmod 4755 /usr/bin/sudo
Restart Pop!_OS and login normally
Now I am able to run sudo commands such as sudo apt install python3-virtualenv without issues... so far.

所以在我嘗試其他任何事情之前,我想我會尋求一些幫助或幫助,因為我不確定下一步該做什麼。

任何幫助,將不勝感激。

作業系統資訊

solrevdev@hplaptop
OS: Ubuntu 20.04 focal
Kernel: x86_64 Linux 5.4.0-91-generic
Uptime: 11m
Packages: 3208
Shell: bash 5.0.17
Resolution: 1920x1080
DE: GNOME 3.36.5
WM: Mutter
WM Theme: 
GTK Theme: Yaru [GTK2/3]
Icon Theme: Yaru
Font: Ubuntu 11
Disk: 148G / 249G (63%)
CPU: Intel Core i5-7200U @ 4x 3.1GHz [46.0°C]
GPU: Mesa Intel(R) HD Graphics 620 (KBL GT2)
RAM: 3632MiB / 15930MiB

所以,我們已經確定了為什麼sudo不能正常工作。看起來它試圖做的是重新定義一個sudo不帶參數的獨奏,以表示重複您輸入的最後一個命令,但帶有sudo前綴。

聰明的想法,但實現失敗 - 未引用的$@ 必須用雙引號括起來才能正常工作,並且為了避免遞歸,sudo必須在函式內部使用命令而不是函式本身:

# Redefine sudo to reuse the last command if no args
sudo() {
   if [ $# -gt 0 ]
   then command sudo "$@"
   else command sudo "${BASH:-bash}" -c "$(history -p '!!')"
   fi
}

現在,要找到它的定義位置將變得更加困難。該命令將是一個很好的起點(類似於我假設的遞歸grep,但允許函式名和 之間的空格()是可選的,以及排除不相關的目錄):

command sudo find / \( -path '/dev' -o -path '/proc' -o -path '/run' -o -path 'sys' \) -prune -o -type f -exec grep -l 'sudo *()' {} +

一旦你找到它,我會建議你送出一個錯誤報告。(假設它對您有用,歡迎您提出我提供的程式碼作為修復程序。)

引用自:https://unix.stackexchange.com/questions/683476