1

UPDATE 24/12/2021

I had to Ctrl + C out of the accecpted answers find and grep code to try and find the culprit half way in order to restart due to something else but I will update this post once I've found the root problem.

However, the accepted answer's redefine sudo code was used by me to fix the issue and I can sudo away without crashing!

So, for anyone else that may have the issue before I find the source problem here is what I did:

.bashrc

A the bottom of my .bashrc file

# 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-fix

#!/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
}

Thank you @roaima

UPDATE 23/12/2021

From some of the questions from the comments:

Can I log in as root?:

I can and have done, as it's Ubuntu the root user is disabled, but I was able to in my test.sh add sudo passwd root and gave myself a password.

Then once this is all fixed I will disable it again with sudo passwd -l root either in the terminal directly as the problem will be fixed or again in test.sh if I have to stick with this odd workaround.

Does sudo date also trigger this?

Yes sudo date also triggers the issue, Any sudo command does it unless as I say the sudo command is in a .sh and called.

Difference between bash and sh

Changing the shebang in my test.sh file makes no difference. It still works.

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

Shell Info

echo "$SHELL"
/bin/bash

Please add the result of running type sudo to your question

This wasn't what I was expecting, to be fair.

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


As this wasnt't what I was expecting I ran the following to see if that sudo function was in a file somewhere but it was only in the markdown file that is this post.

I also ran which sudo

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

which sudo
/usr/bin/sudo

strace sudo

In the original question I mention strace sudo so for completeness here it is

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 +++


Something odd has started happening on my laptop running Ubuntu 20.04

When I open a terminal (either the default one or Terminator) and try to run any sudo command for example sudo ls it will hang, then just close/crash the terminal.

It is consistent and survives reboots etc.

It does not even get to the stage where it will ask for password.

However, if I create a test.sh file and run that e.g ./test.sh then any sudo command will work as expected. It will ask for password then work.

For example, this works:

test.sh

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

sudo ls

But in a terminal window, this crashes:

sudo ls

I've Googled, and Stack Overflow'd everything I can think of, I've not installed anything new as the laptop hasn't been doing anything for a while.

I've run strace sudo which led me to a superuser post which I tried, but that did not work:

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.

So before I try anything else, I thought I would ask for some help or assistance as I'm not sure what to do next.

Any help would be appreciated.

OS Info

 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
6
  • Can you log as root?
    – esantix
    Dec 22, 2021 at 12:25
  • I believe it can be a difference between bash and sh. Probably adding #!/bin/sh instead of bash will break also. Check if thats the problem. Its not a solution but a hint from where it might come from. askubuntu.com/questions/57040/…. You might have something "broken" in sh but not in bash.
    – esantix
    Dec 22, 2021 at 12:30
  • 1
    Hello solrevdev. I understand that sudo ls unexpectedly closes the terminal session window. Does sudo date also trigger this? (I'm trying to determine whether it's ls or sudo that may be at fault here.) Lastly, please add the result of running type sudo to your question (not here in the comments). Dec 22, 2021 at 13:04
  • @roaima and Santiago, I have updated my post with your questions. Thanks for taking the time to help, It is very much appreciated.
    – solrevdev
    Dec 23, 2021 at 8:48

1 Answer 1

1

So, we've identified why sudo isn't working properly. It looks like what it's trying to do is to redefine just a solo sudo without arguments to mean repeat the last command you've entered, but with sudo as a prefix.

Clever idea but broken implementation - the unquoted $@ must be enclosed in double quotes for it to work properly, and to avoid recursion the sudo command must be used inside the function rather than the function itself:

# 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
}

Now, to find where it's defined is going to be harder. This command would be a good starting point (similar to what I assume is your recursive grep but that allows for the space between the function name and the () to be optional, as well as excluding irrelevant directories):

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

Once you've found it I would recommend you submit a bug report. (Assuming it works for you, you'd be welcome to suggest the code I've provided as a fix.)

3
  • Thanks so much, I am running that now, but interestingly I forgot to put that in my test.sh file and pasted in the terminal and expected it to crash, but it's working. So in another window I typed command sudo ls and that works! So the prefix command is another workaround. Hopefully I can find the culprit in the result of your find command.
    – solrevdev
    Dec 23, 2021 at 9:48
  • command sudo works in my terminal without crashing… at least that saves me having to run everything through a test.sh file for now.!
    – solrevdev
    Dec 23, 2021 at 9:53
  • 1
    Thanks very much, I can now happily sudo again in my terminal and will update the post once I've found the culprit. Have a great xmas!
    – solrevdev
    Dec 24, 2021 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .