Introduction
In this tutorial, delve into using the mesg
command within Linux environments to manage permissions related to sending messages to a user's terminal. Discover how to send messages to other users actively logged in and explore methods for restricting message receipt for specific users. This guide includes understanding the mesg
command's functionality, sending messages effectively, and managing message reception permissions.
The mesg
command is an essential tool for systemadmin tasks on a Linux system, enabling control over user message permissions. It allows users to accept or reject messages from others on the same system. This tutorial offers practical examples and clear instructions to help you effectively utilize the mesg
command in your Linux environment.
Understand the mesg Command
In this section, we'll explore the mesg
command in Linux. This command manages message-sending permissions to a user's terminal, allowing users to accept or reject messages sent by others on the same system.
First, let's determine the current message permission status using the mesg
command:
mesg
Example output:
is y
The is y
output indicates the user is currently able to receive messages.
To disable message receiving, use the mesg n
command:
mesg n
Now, verify the permission change:
mesg
Example output:
is n
The is n
output indicates the user is now unable to receive messages.
To re-enable message receiving, use the mesg y
command:
mesg y
Verify the permission change:
mesg
Example output:
is y
The is y
output confirms that the user can now receive messages.
Send Messages to Terminal Users
In this section, learn how to send messages to other users connected to the same Linux system.
First, see a list of users currently logged in using the who
command:
who
Example output:
labex pts/0 2023-04-18 10:15 (172.17.0.1)
To send a message to the user labex
, use the write
command followed by the username:
write labex
This starts an interactive message session. Type your message and press Ctrl+D to send.
Example message:
Hello, this is a test message.
The message will appear on the recipient's terminal.
To send a message to all logged-in users, use the wall
(write all) command:
wall "This is a broadcast message to all users."
This displays the message on the terminals of all logged-in users.
Restrict Message Receiving Permissions
In this section, learn how to restrict message receiving permissions for specific users on the system. This is often a key task for any systemadmin when managing user access and communication.
First, create a new user named "guest" to demonstrate permission restriction:
sudo useradd guest
Now, switch to the "guest" user:
sudo su - guest
As the "guest" user, try to send a message to the "labex" user:
write labex
You will encounter an error message indicating that the "guest" user is not authorized to send messages.
To restrict message receiving permissions for the "guest" user, the mesg
command needs to be used by the "root" user:
sudo mesg -g guest n
This command sets the message receiving permission for the "guest" user to "n" (no), preventing the user from receiving messages.
Let's confirm the permission change:
sudo mesg -g guest
Example output:
guest is n
The output confirms that the "guest" user is now unable to receive messages.
To allow the "guest" user to receive messages again, use the following command:
sudo mesg -g guest y
Verify the permission change:
sudo mesg -g guest
Example output:
guest is y
The output confirms that the "guest" user can now receive messages.
Summary
In this lab, we explored the mesg
command in Linux, which is used to manage permissions for sending messages to a user's terminal. We showed how to check the current message permission status, deny message receiving, and allow message receiving again. We also detailed sending messages to other users on the same system using the write
and wall
commands, as well as restricting message receiving permissions for specific users. This command is powerful and should be used with caution, particularly by the root user or by those with systemadmin privileges.
This tutorial has provided a complete overview of managing message permissions and inter-user communication within a Linux environment.