Today’s article might be useful for system administrators or just for fun, as we learn to send a message to another logged-in user (in the terminal, of course). This shouldn’t be a complicated or lengthy article, though many of my recent articles have been significantly longer than usual.
If you’re just a regular desktop user, this might not be all that interesting, but you can still test it if you want. Besides, you never know when you will want to send a message to another logged-in user! It could happen.
Let’s say you have a server with people logged in via SSH. This could also be a single computer with multiple people logged in, should you wish to test this and play around with sending a message to another logged-in user. Let’s also say that you want to send them a message in the terminal.
Perhaps you’re going to log them off? Maybe you’re going to reboot the server? Who knows, maybe you want to give them some sort of directions and the easiest way to do so is to send them a message that pops up in their terminal. You can do that!
We’ll be using a few tools for this. None of them are all that complicated and these little tools (do one thing and do it well) are tools that make the Linux world go around.
For starters, we’ll be using the ‘who’ command.
who – show who is logged on
We will also be making use of the ‘awk’ command.
gawk – pattern scanning and processing language
Next, we’ll be using the ‘echo’ command.
echo – display a line of text
There will also be the ‘write’ command.
write — send a message to another user
We will also be using a pipe. We will pipe the output from one command to another command. We’ve done that lots of times on this site, so regular readers will already be familiar with a pipe and how it works.
Briefly speaking, a pipe is just one way to take the output from one command for use in another command. It’s a pretty handy tool to add to your Linux toolbox if you haven’t already done so. It’s a simple tool, which is a good thing.
If all of the above looks complicated, don’t be alarmed. It’s not all that complicated and the commands I share will be simple enough for most anyone to follow. You’ll be able to adjust them to your needs quite easily.
Send A Message To Another Logged In User:
As mentioned in the intro, you’ll want an open terminal for this. So, open your default terminal emulator. You can usually just press
With your terminal now open, let’s find out who is logged in. To do that, we only need the following command:
1 | who -u |
However, we only care about the first two fields, so let’s narrow that output with the following command:
1 | who -u | awk '{print $1, $2}' |
The output from that command is all we need for the next part. You use the first column to identify the username. That makes them easy to identify, or at least easier for most folks.
The other column is the 2nd one. That identifies their login method, basically which terminal they’re using, and is also what we will use to specify the recipient of our message. Next, to send a message to another logged-in user, you use a command similar to this:
1 | echo "<message>" | write <$1> <$2> |
Or, take a look at this:
So, in that case, the syntax of the command is easy, it’s just like this:
1 | echo "this is a message" | write kgiii pts/3 |
You’ll notice that the output of the command isn’t on that screen. It was sent to the other screen, the screen where that user was logged in (specifically over SSH). It quite happily sends the message to the user logged in at that location.
You can’t use usernames alone, as it’s possible for more than one person to use the same username. This method identifies the user and the method/location they’ve used to log in. It’s a pretty handy command like that. It might look a bit complex, but it isn’t.
Closure:
So, if you’ve ever wanted to send a message to another logged-in user, you can now do that. It’s easier done than explained, but hopefully, you get the gist of it and can apply it to your personal computer usage.
It’s not always that easy to come up with ideas for articles. I often pull them from my notes, but my notes are a mess, and not all of them would make good articles. If there’s something you’d like covered, and I know the subject, feel free to contact me and let me know. Of course, don’t forget that I take guest articles when they’re about Linux.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.