Fix "mesg: ttyname failed: Inappropriate ioctl for device" over SSH, sudo -i or su -
The message comes from a mesg n line in root's .profile running without a terminal. It is harmless; make the line check for a terminal first.
You run a command as root over SSH, with sudo -i, or from a provisioning tool, and the output starts with:
mesg: ttyname failed: Inappropriate ioctl for device
The command itself still runs. The message comes from root's shell startup file, not from your command.
Where it comes from
mesg turns on or off other users' permission to write messages to your terminal (with programs such as write or talk). On Debian and Ubuntu, root's ~/.profile is copied from the system's default, and older versions of that file end with:
mesg n || true
~/.profile is read by login shells, such as sudo -i, su -, or bash -l started by a script. When such a shell runs without a terminal, for example as part of ssh root@host 'bash -l -c ...' or an automated provisioning step, mesg has no terminal to change. Older versions of mesg (util-linux 2.32 and earlier) report that as ttyname failed: Inappropriate ioctl for device. The || true hides the failure from the script, but not the message.
Newer systems print nothing:
- Ubuntu's default from 20.04 on is
mesg n 2> /dev/null || true, which throws the message away. Debian 12's default has the same line. mesgfrom util-linux 2.33 and later exits quietly when there is no terminal, unless you add--verbose.
Fix it
Edit /root/.profile (or the user's ~/.profile) so the line only runs when there is a terminal:
tty -s && mesg n || true
tty -s prints nothing and succeeds only when standard input is a terminal. Or keep the line and discard the message:
mesg n 2> /dev/null || true
The message is only noise, so leaving it is also fine.
Similar startup-file problems
- To check from a script whether it has a terminal or runs over SSH, see Check if a shell is running over SSH, inside tmux, or inside screen.
- If a startup file prints text, SFTP can fail: see SFTP "Received message too long".
Related
- Command not found with "ssh host command" but it works after logging in: PATH and .bashrc
- SSH "PTY allocation request failed on channel 0"
- Shell and tmux basics