Welcome to the Linux Foundation Forum!

LFS201 - Lab 32.1. Using chmod - incorrect instructions?

LFS201 - Lab 32.1. Using chmod

Here the original text:
One can use either the octal digit or symbolic methods for specifying permissions when using
chmod. Let’s elaborate some more on the symbolic method.
It is possible to either give permissions directly, or add or subtract permissions. The syntax is pretty obvious. Try the following
examples:
$ chmod u=r,g=w,o=x afile
$ chmod u=+w,g=-w,o=+rw afile
$ chmod ug=rwx,o=-rw afile

Unfortunately u=+... or u=-... etc. don't work as expected, neither on Ubuntu nor on Centos.

What works is this:

chmod u+w,g-w,o+rw

that is without the "=".

The quiz at the end of Chapter 32 is correct.

Here the proof:

heiko@LM20-heiko:~$ ll file
-rw-r--r-- 1 heiko heiko 0 Jul 19 13:41 file
heiko@LM20-heiko:~$ chmod g=+w file
heiko@LM20-heiko:~$ ll file
-rw--w-r-- 1 heiko heiko 0 Jul 19 13:41 file
heiko@LM20-heiko:~$ chmod 644 file
heiko@LM20-heiko:~$ ll file
-rw-r--r-- 1 heiko heiko 0 Jul 19 13:41 file
heiko@LM20-heiko:~$ chmod g+w file
heiko@LM20-heiko:~$ ll file
-rw-rw-r-- 1 heiko heiko 0 Jul 19 13:41 file
heiko@LM20-heiko:~$ 

Comments

  • luisviveropena
    luisviveropena Posts: 1,246

    Hi @heiko_s ,

    I think it works, but the logic is kind of weird. So, when we say u=+something, it adds it up. When we say u=-something, it removes everything, at least we specify other permissions with +something for user in this case. The same applies to "g" and "o".

    Try to apply these rules to the following examples:

    luis@ubuntu18server:~$ touch afile
    luis@ubuntu18server:~$ chmod 777 afile
    luis@ubuntu18server:~$ ls -l afile
    -rwxrwxrwx 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod u=+w,g=-w,o=+rw afile
    luis@ubuntu18server:~$ ls -l afile
    --w----rw- 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod 000 afile
    luis@ubuntu18server:~$ ls -l afile
    ---------- 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod u=+rwx,g=+r,o=+r afile
    luis@ubuntu18server:~$ ls -l afile
    -rwxr--r-- 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod 777 afile
    luis@ubuntu18server:~$ ls -l afile
    -rwxrwxrwx 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod u=-x,g=+rwx,o=rwx afile
    luis@ubuntu18server:~$ ls -l afile
    ----rwxrwx 1 luis luis 0 Kun 19 17:11 afile
    luis@ubuntu18server:~$ chmod 777 afile
    luis@ubuntu18server:~$ chmod u=+r+w-x,g=+rwx,o=rwx afile
    luis@ubuntu18server:~$ ls -l afile
    -rw-rwxrwx 1 luis luis 0 Kun 19 17:11 afile

    Many regards,
    Luis.

  • heiko_s
    heiko_s Posts: 99

    Sorry to say, but this forum software sucks! I have replied in detail and the entire post seems lost now that I tried to edit it.

    So I make my reply short:

    According to the chmod man page, you are NOT supposed to use =, - and + together, but either one of it [=-+].

    • and - should add or subtract the permission(s). Here is the relevant part from the man page:

      The format of a symbolic mode is [ugoa...][[-+=][perms...]...], where
      perms is either zero or more letters from the set rwxXst, or a single
      letter from the set ugo. Multiple symbolic modes can be given, sepa‐
      rated by commas.

      A combination of the letters ugoa controls which users' access to the
      file will be changed: the user who owns it (u), other users in the
      file's group (g), other users not in the file's group (o), or all users
      (a). If none of these are given, the effect is as if (a) were given,
      but bits that are set in the umask are not affected.

      The operator + causes the selected file mode bits to be added to the
      existing file mode bits of each file; - causes them to be removed; and
      = causes them to be added and causes unmentioned bits to be removed ex‐
      cept that a directory's unmentioned set user and group ID bits are not
      affected.

    To use your example, the correct way to do it is this:

    heiko@LM20-heiko:~$ chmod u=w,g=,o=rw file
    heiko@LM20-heiko:~$ ll file
    --w----rw- 1 heiko heiko 0 Jul 19 13:41 file
    

    "=+" and "=-" should NOT be used! If one wants to use "+" and "-" to add or subtract permissions, then here is the way to do it:

    heiko@LM20-heiko:~$ chmod 777 file
    heiko@LM20-heiko:~$ ll file
    -rwxrwxrwx 1 heiko heiko 0 Jul 19 13:41 file*
    heiko@LM20-heiko:~$ chmod u+w,g-w,o+rw file
    heiko@LM20-heiko:~$ ll file
    -rwxr-xrwx 1 heiko heiko 0 Jul 19 13:41 file*
    

    At least that's how I read the man page and how it works on the command line.

  • coop
    coop Posts: 916

    Luis is pointing out to you things you already know as best I can tell and let me address what you said directly.

    You are right in some sense and but in another sense you are reading too much into the wording, more than was intended . In your post you say:

    "Unfortunately u=+... or u=-... etc. don't work as expected, neither on Ubuntu nor on Centos.
    What works is this: ...."

    The problem is with the phrase "as expected" by which you mean "as I expected". There is nothing in the lab that indicates the = sign should not perform as it actually does. The purpose of the exercise is to try different things and see what happens, and voila you learned something. I suppose the lab should not say "The syntax is pretty obvious" as that is the source of the confusion and we will eliminate it in the next version. So thanks for pointing this out. I'm always amazed when someone raises something about a phrasing that has been there for a number of years, and has never caused confusion before. :) Keeps us on our toes.

    As to the forums software suck, the only time I have ever seen a complaint is when someone tried to keep composing for a time that exceeded some built in time limit we have never run into (15 minutes perhaps) as no one apparently as ever tried to edit that long in the years we have had this running. I suggest that if you need to craft some thing over a longer time period (if that is what occurred) you work in an external editor and then paste it in when done. Also note the "save draft" button as that probably will work to make sure things don't get lost. I suppose there is no "autosave" feature (I don't know).

    For email I never ever write a long post in an online platform unless drafts are being saved. For this reason.

    If you were editing and the forum went down or something, that's a different matter. I would hope drafts would get saved but I've never tested it.

  • heiko_s
    heiko_s Posts: 99

    Thanks coop. Of course I meant "I expected...". It was surely a learning exercise, and that is what I'm here for.

    The forum software is not too intuitive. Not sure why there is a 15 minutes edit limit. I'm using frequently and infrequently several dozen different forums. Posting code is where many forums struggle.
    This forum software is quite different from what I'm used to on, for example Ubuntu forum, Manjaro, Linux Mint, bugs launchpad, etc. Limitations of forum software have led me to run my own private website to share some stuff. Mine is far from being perfect, either :wink: .
    In fact, my only problems with this forum is when editing stuff and the 15 minutes grace period.

  • fcioanca
    fcioanca Posts: 2,157

    Hi @heiko_s

    To avoid issues with editing and the 15 minute window, it may be better to prepare your post in a notepad/dox, and post the final outcome when ready.

Categories

Upcoming Training