How to Use the 'write' Command to Send Messages to Logged In Users (with examples)
The write
command is a utility in Unix-like operating systems that enables users to send messages to other users currently logged into the system. This command can be particularly helpful for direct communication without the need for external messaging software, providing a quick and straightforward way for system users to collaborate or share information. By leveraging the write
command, you can communicate with users on specific terminal sessions and ensure that your message reaches the intended recipient immediately.
Use case 1: Send a message to a given user on a given terminal ID
Code:
write username terminal_id
Motivation:
This basic use case illustrates the primary function of the write
command: sending a text message directly to another user who is logged into the same system. This capability can be invaluable in environments where users are sharing the same server, such as in educational institutions or development teams working on shared projects. By targeting a specific terminal, a user can ensure their message gets to the intended recipient without any ambiguity.
Explanation:
write
: This is the command that initiates the message transfer process.username
: This argument specifies the recipient’s system username. It’s essential to correctly identify this user to ensure the message is delivered to the correct individual.terminal_id
: This is the identifier of the terminal session the recipient is using. Using thewho
command can assist in determining the appropriate terminal ID.
Example Output:
Upon execution, the sender’s terminal will prompt to enter a message. As soon as a line is typed and enter is pressed, it appears on the recipient’s terminal. The recipient will see something like:
Message from username@host on terminal_id [time] ...
<Your message here>
Use case 2: Send message to “testuser” on terminal /dev/tty/5
Code:
write testuser tty/5
Motivation:
In scenarios where a user needs to communicate with another user known by name and terminal location, this use case is highly relevant. For example, if you know your colleague “testuser” is working on a particular terminal, you can send a quick message without needing to ask for their current terminal identifier each time. This can streamline communication in an environment where constant back-and-forth is common.
Explanation:
write
: Starts the command to send a message.testuser
: This is the specific system username of the person you want to communicate with.tty/5
: Refers to the physical terminal associated with the user. This traditional terminal naming is common in older setups or when working directly with server hardware.
Example Output:
After initiation and typing a message, the testuser
will see something like:
Message from sender_username@host on tty/5 at [time] ...
<Your message here>
Use case 3: Send message to “johndoe” on pseudo terminal /dev/pts/5
Code:
write johndoe pts/5
Motivation:
Many modern systems, especially those accessed via SSH or other networking tools, use pseudo terminals (pts
) instead of traditional ttys. This example is relevant in such modern networked environments where software-based terminals are more prevalent. Sending a message to a psuedo terminal like /dev/pts/5
is typical in server management where remote users are logged in.
Explanation:
write
: Indicates the command for sending inter-terminal messages.johndoe
: The username of the terminal session’s owner.pts/5
: Specifies the pseudo terminal, which is often used for virtual sessions like those created through SSH.
Example Output:
Upon writing and sending the message, the target user, “johndoe,” will see the message appear while logged in:
Message from sender_username@host on pts/5 at [time] ...
<Your message here>
Conclusion:
The write
command is a powerful tool within multi-user Unix-like operating systems designed to facilitate straightforward message-based communication. By specifying the appropriate username and terminal, users can effortlessly converse in real time, bridging gaps in collaborative efforts or urgent troubleshooting scenarios. Whether in legacy systems using physical terminals or modern setups with virtual ones, mastering the write
command can significantly enhance inter-user communication efficiency.