What is a chmod
equivalent for mesg y
and mesg n
?
1 Answers
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.

- 333,661
-
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 05 '18 at 22:33 -
2@Ned64 Um, no. I'm running this in
/bin/sh
(notbash
) 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 https://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated – Kusalananda May 05 '18 at 22:37 -
@Kusalananda what is the difference between running
chmod g-w
andchmod g-w "$(tty)"
? (New to UNIX and trying to make sure I understand) – MandyLB May 05 '18 at 22:39 -
ll ($tty)
yieldsBadly placed ()'s.
in my shell. Anyway, pleasell /bin/sh
- in GNU/Linux often a link tobash
, they share a syntax.ksh
,tcsh
, ... vary in syntax. – Ned64 May 05 '18 at 22:39 -
1@Ned64 I believe I used
$(tty)
, not($tty)
. My/bin/sh
happens to beksh
in POSIX mode. – Kusalananda May 05 '18 at 22:40 -
Sorry.
ll $(tty)
yieldsIllegal variable name.
. Interesting, so it does work inksh
. It does not incsh
+tcsh
. – Ned64 May 05 '18 at 22:41 -
@MandyLB The
tty
command returns the current terminal device. This is the file that changes permission when you usemesg
. The commandchmod g-w "$(tty)"
removes the write permission for the current terminal device.chmod g-w
by itself is nonsensical as the command is not given a filename to change permission on. – Kusalananda May 05 '18 at 22:43 -
@Ned64
csh
andtcsh
uses a totally different shell dialect from thesh
shells (bash
,ksh
,dash
,zsh
, etc.). They can't really be compared, and trying to write polyglot shell code that works (without errors) in bothsh
andcsh
shells is really something you should avoid. – Kusalananda May 05 '18 at 22:45 -
1A subtlety of
mesg
not reflected in this answer is thatmesg
does not operate upon the controlling terminal. Depending from which operating system'smesg
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 thetty
command. One example in onemesg
manual page even demonstrates standard input redirected from some other terminal and not the controlling terminal. – JdeBP May 05 '18 at 23:11 -
@JdeBP You are absolutely correct. I will amend the answer by referring to your comment. – Kusalananda May 05 '18 at 23:16
mesg
settings depend on user(s) and group(s)? – Ned64 May 05 '18 at 22:29