Create a symbolic link to a file I can’t access

permissionssymlink

[Disclaimer: there's no malicious intent to this question, I'm trying to understand the ln -s command for a school project]

Say I have a file system with my home folder, /home/anna. /home/bob is a folder I can't access, with a file I can't access, foo.txt

Can I successfully run ln -s /home/bob/foo.txt in my home folder? Is it correct to assume that if I can, it will produce a link I can't access (with the same permissions as foo.txt)?

What if I DID have read privileges on foo.txt, just not access to /home/bob?

What about the reverse case, where I could access /home/bob but not read foo.txt?

Best Answer

Yes, you can create a symbolic link to any location.

Can I successfully run ln -s /home/bob/foo.txt in my home folder? Is it correct to assume that if I can, it will produce a link I can't access (with the same permissions as foo.txt)?

Correct. The access restrictions of the target file apply. If you create a symlink to a restricted resource, you simply won't be able to access it. It is not even required that the target file actually exists.

A demo:

$ ln -s /etc/shadow foo
$ file foo
foo: symbolic link to /etc/shadow
$ cat foo
cat: foo: Permission denied

$ ln -s /etc/nonexistent bar
$ file bar
bar: broken symbolic link to /etc/nonexistent

What if I DID have read privileges on foo.txt, just not access to /home/bob?

If you don't have permissions on the parent directory, you can't access the contained file. So with a symlink you still wouldn't be able to access it. Creating a symlink doesn't affect the permissions.

What about the reverse case, where I could access /home/bob but not read foo.txt?

Again, you can create a symlink to it, but not access the file.