write Command in Linux

write Command in Linux VPS with 7 Basic Examples

The write command in Linux VPS is a command line tool for communicating directly to other logged-in users by sending messages.

By running the write command with the username of who is currently logged into the same Linux VPS and typing the message, the write command provides a real-time chat-like experience. It sends messages to the specified user’s terminal after reading standard input.

Prerequisites to use write command in Linux

  • The write command is typically used on multiuser environments like on Linux VPS.
  • You need to have sudo privileges to send messages to other users.
  • To run write command you must access the terminal.

Linux write command basic syntax

The general format for using the write command in Linux VPS running various distributions such as Ubuntu, Debian, CentOS, Fedora, Arch Linux, etc for quick communication between two logged-in users through the terminal is as follows:

write user [tty]
  • user: username of the recipient who you want to send a message. (Specifying this parameter is mandatory)
  • tty (teletype):  Recipient’s terminal environment if the user has multiple terminal sessions open. (Specifying this parameter is optional)

The write command allows communication between two different users on the same server by copying lines from one user’s terminal to another.

The recipient can reply by running the write command as well and when you finish, you can end messaging by pressing Ctrl+D or typing an end-of-file character. Therefore, the recipient by getting the message ‘EOF’  which signifies the end of a message in a technical context, will understand that the conversation is over.

When a user is logged in on multiple terminals, to specify the tty name (actual terminal name, e.g., pts/0) in the basic format of the write command in Linux you can run the ‘ w ‘ command.

Also, communication between users in the Linux terminal requires write permissions. Therefore, to allow messages run the following command:

mesg y

This command enables the current user to receive messages from other users.

On the other hand, you can disallow messages with:

mesg n

This command disallows messages from other users.

To control whether the recipient receives messages, you can use:

mesg

Linux write Command Examples

Using the write command in Linux VPS requires at least two logged-in users that each user has a terminal session open.

Therefore, the user who you want to send a message needs to be logged in Linux VPS and have message permissions enabled to receive your message.

So, log in to Linux VPS with sudo privilege and follow the below write command instructions:

1. Sending a simple message to a user

For example, if you want to send a message to a user named suppose ‘liosa’ over the terminal, run the following command:

write liosa

Then write the message to the user and press Enter:

Hello liosa,
How are you today?
Don't forget about the meeting at 3 PM.

After running the write command and typing the message, on the receiving end, the recipient gets information about a connection such as the username of the sender of the message, the pts console where the message is coming from, and the timestamp. Then your message shows on the receiving end.

After sending your message, by pressing the CTRL+D end the session; on the receiving end user also gets an EOF message, indicating the communication has ended.

2. Sending a message to a user logged in on a specific terminal

You can send the message to a specific user named suppose ‘liosa’ on the specific terminal (`pts/2`), by running the following command:

write liosa pts/2

This command write the message to ‘loisa’ who is logged in on ‘pts/2’.

3. Interactive Messaging

You can open an interactive session for continuously communicating between two users with write command:

User1 types:

write user2

Then write the message for sending to User2 and press Enter.

User2 receives the message and replies by running:

write user1

Then type the message and press Enter.

Both users repeat these steps to continue the conversation.

After the conversation, by pressing CTRL+D terminate the communication channel.

4. Sending a message with a single line using echo

You can send a message to another user by combining echo and write commands. For example:

echo " Meeting at 3 PM " | write user1

By using echo you can specify the message and pipe it to write to automate sending predefined messages without interactive typing. while, using the write command without combining echo, allows interactive and real-time communication.

Also, you can combine echo and write command in a script to automate sending predefined messages to notify yourself or another user about a task. For example, take a look at the following Bash script:

#!/bin/bash
# Define users
users=("user1" "user2" "user3")
# Define the message
message="System maintenance will start at 5 PM. Please save your work."
# Loop through each user and send the message
for user in "${users[@]}"; do
if who | grep -q "^$user"; then
echo "$message" | write "$user"
echo "Message sent to $user"
else
echo "$user is not currently logged in."
fi
done

This script sends a message to multiple users (“user1” “user2” and “user3”) that has been defined in the first part of the script. Through this script you send the message “System maintenance will start at 5 PM. Please save your work.” via variable ‘message’.

This script checks if the user is currently logged in using who and grep commands and if the user is logged in server, it sends the message by piping echo and write command. This script also provides feedback on whether the message was sent.

Therefore, combining echo and write in a script allows you to automate sending notifications to multiple users without needing manual input each time.

5. Sending a file content to a user

The write command also allows you to send the contents of a text file as a message. Take a look at the following example to do this:

write liosa< message.txt

This command sends the content of message.txt to liosa.

6. Sending the output of a command to a user

Although the write command is not designed to send the output from other commands to other users but you can achieve this by combining write with other tools. For example, if you want to send the disk usage information to a user named ‘liosa’, run the following command:

df -h | write liosa

You can use the  cat or tee command to send the output of your command to the write command:

your_command | cat - | write username

Or

your_command | tee output_file | write username

Replace ” your_command” with the command you want to run and “username” with the username of the recipient.

The cat command displays the standard output (stdout) of your command and pipes it to the write command.

The tee command sends the standard output (stdout) of your command to a file and user simultaneously which is useful when you need to keep a record of the output.

7. Sending a message to yourself for testing

You can test the write command function by sending a message to yourself via the following command:

write $(whoami)

In this command, whoami retrieves the username of the current user.

This command attempts to send a message to the user currently logged in who types the command.

After typing a message, you can end the program by pressing Ctrl+D.

What is the benefit of using a write command in Linux VPS?

The write command in Linux allows you to direct, quick, and real-time communication through the Linux terminal by providing information about the sender username, terminal device, and timestamp. Using the write command in Linux is a straightforward method for sending messages without needing additional software.

The write command facilitates the alerts or notifications for users about system maintenance updates or critical events and optimizes server management.

How does write() work?

The write() in Linux is used to write data (number of bytes)  to a file associated with the open file descriptor.

Conclusion

The write command in Linux is a simple way for quick communication with other logged-in users through the terminal. However, since messages appear directly on the recipient’s terminal when communicating via write command in Linux, it is not suitable for private conversations.

For better user experience and security, it is recommended to use an email tool or chat application. The write command line tool might provide less control and privacy compared with other methods.

If you need detailed information about the write command usage and options in Linux, refer to the manual page for the write command using the ” man write ” command.

Leave a Reply

Your email address will not be published. Required fields are marked.