-1

This question already has an answer here:

I need to access a file on an NFS fileserver where root squash is turned on. I'm using bash on Arch Linux. I am using this type of command:

# sudo -u authduser ls /mounted/filesystem/path/aa.user.js

This succeeds and the output is:

/mounted/filesystem/path/aa.user.js

However, there are multiple files, such as bb.user.js. When I try the following command it fails:

# sudo -u authduser ls /mounted/filesystem/path/*.user.js

The output is:

ls: cannot access '/mounted/filesystem/path/*.user.js': No such file or directory

I have tried quoting, escaping and various other things without success. What is the correct way?

marked as duplicate by muru, Ipor Sircer, terdon bash Nov 22 '18 at 13:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It does not appear to be a duplicate to me. You can see from the output in my question that it is not a permissions issue. – MountainX Nov 22 '18 at 7:39
  • 1
    No, your output doesn't show that. – muru Nov 22 '18 at 7:40
  • OK, you're correct. But given that the specifics are the opposite of that question, I think this is still a distinct and different question. – MountainX Nov 22 '18 at 7:48
1

The issue with the wildcard is that it's expanded by the shell, before running the sudo command.

If only authduser will be able to list files under /mounted/filesystem/path/, but not your user who is running the sudo command, then the shell will not be able to expand the wildcard and will pass it unmodified. Since ls itself doesn't expand wildcards, this will result in the error you're seeing.

See Why isn't this sudo mv operation with wildcard working? for more details on that.

For a solution to this problem, you can use sudo's -s argument, which spawns a shell as the target user (rather than simply executing a command directly), in which case if you also give it a command, it will be passed to the shell using the -c option (which is the usual option for the shell to interpret and execute a command, including wildcard expansion.)

In that case, you'll want to put the command within quotes (preferably single quotes), so that the shell running the sudo command will not try to interpret the command, only the one spawned by sudo as the target user:

# sudo -u authduser -s 'ls /mounted/filesystem/path/*.user.js'

See the man page of sudo for more details, in particular take a look at the -s option.

UPDATE: It's not really possible to use sudo -s with a command here. See “sudo -s ” runs command in a shell, but wildcards or metacharacters not working for more details.

  • I had tried the -s option, and that's the one I would prefer to use. I couldn't get it to work. Your command example also gives the error "No such file or directory" (with single quotes or double quotes). The command I gave in my answer does work. But I would prefer sudo -s, and if you figure out why it isn't working here, I'll accept your answer. Thanks. – MountainX Nov 22 '18 at 8:02
  • @MountainX what Linux distribution are you using and which version of sudo? I believe the -s option is a recent addition, so might not be present in your version... – filbranden Nov 22 '18 at 8:13
  • @MountainX Does this command work for you? sudo -u authduser -s whoami – filbranden Nov 22 '18 at 8:21
  • Yes, sudo -u authduser -s whoami gives the expected result authduser. – MountainX Nov 25 '18 at 1:42
  • @MoutainX sudo -s does not work here. See “sudo -s <command>” runs command in a shell, but wildcards or metacharacters not working for details on why it does not work. – filbranden Nov 27 '18 at 4:57
0

The following approach works:

sudo -u authduser bash -c "ls /mounted/filesystem/path/*.user.js"

muru was correct that the issue is similar to Why isn't this sudo mv operation with wildcard working?, except the opposite in that root lacks permissions and the regular user has permissions. The issue comes down to order of precedence of operations, and the solution is based on expanding the wildcard after the sudo operation rather than before it.

  • 2
    The issue is exactly the same: the target user for sudo has permissions, the user running sudo doesn't. – muru Nov 22 '18 at 7:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.