Linux mesg Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mesg command in Linux to control the permission to send messages to the user's terminal. You will also learn how to send messages to other terminal users and how to restrict message receiving permissions for specific users. The lab covers understanding the mesg command, sending messages to terminal users, and restricting message receiving permissions.

The mesg command is a useful tool for managing message permissions on a Linux system. It allows users to receive or reject messages sent by other users on the same system. This lab provides practical examples and step-by-step instructions to help you effectively utilize the mesg command in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/UserandGroupManagementGroup -.-> linux/useradd("User Adding") linux/UserandGroupManagementGroup -.-> linux/su("User Switching") subgraph Lab Skills linux/echo -.-> lab-422799{{"Linux mesg Command with Practical Examples"}} linux/cat -.-> lab-422799{{"Linux mesg Command with Practical Examples"}} linux/useradd -.-> lab-422799{{"Linux mesg Command with Practical Examples"}} linux/su -.-> lab-422799{{"Linux mesg Command with Practical Examples"}} end

Understand the mesg Command

In this step, we will learn about the mesg command in Linux. The mesg command is used to control the permission to send messages to the user's terminal. It allows users to receive or reject messages sent by other users on the same system.

First, let's check the current message permission status using the mesg command:

mesg

Example output:

is y

The output is y indicates that the user is currently able to receive messages.

To deny message receiving, use the mesg n command:

mesg n

Now, let's verify the permission change:

mesg

Example output:

is n

The output is n indicates that the user is now unable to receive messages.

To allow message receiving again, use the mesg y command:

mesg y

Verify the permission change:

mesg

Example output:

is y

The output is y confirms that the user can now receive messages.

Send Messages to Terminal Users

In this step, we will learn how to send messages to other terminal users on the same system.

First, let's check the list of users currently logged into the system 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 will open an interactive message session. Type your message and press Ctrl+D to send it.

Example message:

Hello, this is a test message.

The message will be displayed 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 will display the message on the terminals of all logged-in users.

Restrict Message Receiving Permissions

In this step, we will learn how to restrict message receiving permissions for specific users on the system.

First, let's create a new user named "guest" to demonstrate the permission restriction:

sudo useradd guest

Now, let's switch to the "guest" user:

sudo su - guest

As the "guest" user, let's try to send a message to the "labex" user:

write labex

You will see an error message indicating that the "guest" user is not allowed to send messages.

To restrict message receiving permissions for the "guest" user, we need to use the mesg command as the "root" user:

sudo mesg -g guest n

This command sets the message receiving permission for the "guest" user to "n" (no), effectively restricting the user from receiving messages.

Let's verify 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 learned about the mesg command in Linux, which is used to control the permission to send messages to the user's terminal. We explored how to check the current message permission status, deny message receiving, and allow message receiving again. Additionally, we learned how to send messages to other terminal users on the same system using the write and wall commands, and how to restrict message receiving permissions for specific users.

The lab provided a comprehensive understanding of managing message permissions and communication between users in a Linux environment.

Linux Commands Cheat Sheet