What is a chmod equivalent for mesg y and mesg n?

  • It is unclear what you mean. Do you want to make mesg settings depend on user(s) and group(s)? – Ned64 May 5 '18 at 22:29

Note: As JdeBP points out in comments, it is non-trivial to reproduce the behaviour of mesg with chmod.

The Rationale section in the POSIX specification for mesg says

The terminal changed by mesg is that associated with the standard input, output, or error, rather than the controlling terminal for the session. This is because users logged in more than once should be able to change any of their login terminals without having to stop the job running in those sessions. This is not a security problem involving the terminals of other users because appropriate privileges would be required to affect the terminal of another user.

The method of checking each of the first three file descriptors in sequence until a terminal is found was adopted from System V.

The naive approach would be to look at the changes in permissions on the shell's controlling terminal (the device that the current interactive shell's standard input is attached to) when mesg is used.

The controlling terminal of the shell is returned by the tty command. To figure out what mesg n and mesg y are doing with the permissions on the device, we can use ls -l on it and see what changes:

$ ls -l "$(tty)"
crw--w----  1 myself  tty    5,   1 May  6 00:21 /dev/ttyp1

$ mesg n
$ ls -l "$(tty)"
crw-------  1 myself  tty    5,   1 May  6 00:21 /dev/ttyp1

$ mesg y
$ ls -l "$(tty)"
crw--w----  1 myself  tty    5,   1 May  6 00:21 /dev/ttyp1

So it looks like mesg n removes the group write permission for the device (rw--w---- changes to rw-------) and mesg y adds the group write permission again.

The chmod equivalent would, in my case, be

$ chmod g-w "$(tty)"     # for "mesg n"
$ chmod g+w "$(tty)"     # for "mesg y"

Add salt and pepper appropriately for you particular flavour of Unix.

Note, though, that if the shell's standard input is not a terminal, tty would return the string not a tty and the chmod would fail. In this scenario, mesg would go on to try with the terminal device for the shell's standard output and then standard error (in that order). This is why the above is a naive approach.

| improve this answer | |
  • You could change "$(tty)" to "`tty`" to make it more portable to other shells than bash (e.g. the code does not work in tcsh). – Ned64 May 5 '18 at 22:33
  • 2
    @Ned64 Um, no. I'm running this in /bin/sh (not bash) which is a shell that exists on all Unix systems. /bin/sh understands $(...) as a command substitution (unless the system is very old indeed, and has skipped more than a few updates). For more about that, see unix.stackexchange.com/questions/126927/… – Kusalananda May 5 '18 at 22:37
  • @Kusalananda what is the difference between running chmod g-w and chmod g-w "$(tty)"? (New to UNIX and trying to make sure I understand) – MandyLB May 5 '18 at 22:39
  • 1
    @Ned64 I believe I used $(tty), not ($tty). My /bin/sh happens to be ksh in POSIX mode. – Kusalananda May 5 '18 at 22:40
  • 1
    A subtlety of mesg not reflected in this answer is that mesg does not operate upon the controlling terminal. Depending from which operating system's mesg it is, it operates upon the standard error or the first of standard input/output/error that is a terminal device. This is non-trivial to duplicate, and would not involve the tty command. One example in one mesg manual page even demonstrates standard input redirected from some other terminal and not the controlling terminal. – JdeBP May 5 '18 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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