4

My OS is Ubuntu 14.04 LTS. I got a file /root/1.txt, and logged in with 'yu', who doesn't have read permission on this file.

When I use the code if [ -e /root/1.txt ] , it returns false. I know I can use su - root to change user and get the right result.

My question is: How can I do this without su - root? Is there a way to solve this situation with sudo?

I have solved this situation with test

sudo test -e /root/1.txt &&echo "the file is exist" ||echo "the file isn't exist"

3
  • 4
    The issue isn't with the file 1.txt, it's that root's home directory isn't world-readable and really shouldn't be. Apr 18, 2016 at 4:51
  • Is your problem solved with sudo test?
    – Jeff Schaller
    Apr 18, 2016 at 10:06
  • @JeffSchaller Yes
    – user164825
    Apr 19, 2016 at 0:32

3 Answers 3

6

Don't.

/root/ is not world readable for very good reasons. If you really need to work with that file, put it someplace else.

3
  • thanks!I won't work like that,just think about how to judge a root's fiile without su to root.And I find the command test,it works fine.
    – user164825
    Apr 18, 2016 at 8:44
  • @user164825 If a non-root user has read access to /root/, something has gone very wrong with your system setup. Apr 18, 2016 at 8:52
  • This doesn't answer the question. It should be a comment, not an answer. It's also wrong that root's home directory shouldn't be world-readable — some systems default to it being world readable, some don't, it's up to the system administrator to decide if they actually put files there. Apr 18, 2016 at 21:12
2

It is apparent that the /root directory on your system does not have (at least) 'execute' permisions for the user 'yu', either through group membership or the 'other' bits. Thus, 'yu' cannot list the contents of the /root directory in order to determine if /root/1.txt exists or not.

For further description of file and directory permissions (in Linux, as you've mentioned Ubuntu), see: Execute vs Read bit. How do directory permissions in Linux work?

If you are unwilling to change the permissions on /root, then you need to escalate your own (well, test's) privileges in order to read the directory. Since [ is a real program (/usr/bin/[) as well as a shell built-in, you can use sudo: sudo [ -e /root/1.txt ] && echo yes || echo no. (Contrast with [[ which is only built-in, not an external program).

If you are willing to change the permissions on /root, one minimal change would be to open up 'other' permissions to add the 'execute' bit (sudo chmod o+x /root, for example). This prevents general listing of the directory, but enables you to check about specific files:

user@host:~$ ls -ld /root
drwx-----x 2 root root 4096 Apr  3 04:55 /root
user@host:~$ [ -e /root/1.txt ] && echo yes
yes
user@host:~$ ls /root
ls: cannot open directory /root: Permission denied

A variation on 'other+execute' would be 'group+execute' where you add execute permissions to the group and change the group of /root to one that 'yu' is a member of.

Long story short, this statement:

When I use the code if [ -e /root/1.txt ] , it returns false. I know I can use su - root to change user and get the right result.

has a misleading use of the word "right". Filesystems permissions dictate how test behaves. It doesn't matter if you know that the file exists; it matters whether the program has the permissions to see the file.

0

If you want to do it with sudo instead of su, you could do this:

sudo sh

This will log you in as root on the terminal, then you can do what you want to do. But I wouldn't advise you mess with /root, because there must be a reason it is not assessable by other users. You should move '1.txt' somewhere else.

0

You must log in to answer this question.

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