1

My school asks me to create files and directories in such a way that the ls -l output looks like that (XX are not taken into account):

enter image description here

I've done everything right, except for the symbolic link at the end. I thought you could not change symbolic link's permissions which would always have to be 777, but as you can see my school explicitly requests a configuration of 755.

"chmod go-w test6" obviously doesn't work, so what am I supposed to do ?

My OS is Ubuntu 20.04.

Shoopi
  • 31

1 Answers1

3

Sounds like a trick question. On several systems, you can change the permissions of symlinks with the fchmodat(2) system call with the AT_SYMLINK_NOFOLLOW flag or with a lchmod(2) system call and with the chmod command with the -h option, but not on Linux-based OSes such as Ubuntu.

On many systems that allow changing those permissions, they are ignored anyway (macos being an exception IIRC), so there's little point changing them. In any case, I don't expect there'd be any system where the w permission would be significant as symlinks cannot be modified (to change the target of a symlink, you need to recreate it with the new target for which you need write+search permission to the parent directory¹).

Here, you could cheat and redefine ls as:

ls() (
  set -o pipefail
  command ls "$@" | sed 's/^lrwxrwxrwx/lrwxr-xr-x/'
)

(bearing in mind that as ls's stdout is then a pipe, it might affect the formatting as most ls implementations often give a different (more human friendly) output when it goes to a terminal device).


¹ That's own case where the ownership of the symlink may matter as if the t (restricted deletion) bit is set on the directory and you don't own that directory, then you can delete the symlink unless you own it.