"Learn and forget" Linux basic commands - 35. Commands for communicating with other machines in the network


1. write command

(1) Basic information of write command

  • Command name: write
  • send a message to another user
  • Path: / usr/bin/write
  • Execution permission: all users.
  • Function Description: send information to other users.

The format of the write command is as follows:

[root@localhost ~]# write user name terminal number

# Send information to the user1 user logged in at pts/1 (remote terminal 1), and save the sent data using the shortcut key "ctrl+D"
[root@localhost ~ ] # write user1 pts/1
hel1o I will be in 5 minutes to restart,please save your data

(2) Login terminal number

  • There are 6 local character terminals under Linux system, which are represented by tty1-6. The shortcut key of switching command of each terminal is: alt+F1-6
  • A local graphics terminal, represented by tty7.
    The shortcut key used for switching is ctrl+alt+F7 (provided that the graphical interface of the system needs to be installed and started, and the shortcut key is pressed and held for more than 3 seconds).
  • The remote terminal is represented by pts/0-255 by default, and the Linux system also supports re expansion.
  • Directly enter w to view all terminals linked to the system, as shown in the following figure:

    Finally, the terminal executing the w command is the current terminal.

Tip: the w command is the same as the ls command. It disappears after the command is executed. It is difficult to catch two terminals executing the w command at the same time.

(3) Practice

1) To view all linked users in a Linux system:

2) Send message to root-tty1:

Press ctrl+d to save and send after entering.
3) To view information on the root-tty1 client:

2. wall command

The write command is used to send information to specified users, and the wall command is used to send information to all logged in users, including yourself.
When executing, add the information to be sent after the wall command.

Examples are as follows:

[root@localhost ~]# wall "I will be in 5 minutes to restart,please save your data"

# Or enter the wall command and enter the information to be sent. Then press enter, ctrl+d to save and send.
[root@DevOps ~]# wall
[root@DevOps ~]#
Broadcast message from rooteDevops (pts/e)(Wed Jan 22 17:20:59 2020):

3. mail command

Mail is a Linux mail client command, which can be used to send mail to other users.

Basic information of mail command:

  • Command name: mail
  • send and receive Internet mail
  • Path: / bin/mail
  • Execution permission: all users.
  • Function Description: send and receive e-mail.

Use 1: send mail

If we want to send mail to other users, we can execute the following command:

# Send mail to user1 user
[root@localhost ~]# mail user1 
Subject:hello  <-Mail title
Nice to meet you!<-Email details
. <-use"."To end the mail entry
(You can also directly ctrl+d Save and send directly (both OK)

The emails we receive are saved in / var/spool/mail / user name, and each user has a mailbox named after his own user name.

Use 2: View received messages

We can directly execute the mail command on the command line and enter the interactive command of mail. You can view the received mail here.

Examples are as follows:

# Unread number sender time title
[root@localhost ~]# mail Heirloom Mail version 12.4 7/29/08.Type?for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root Mon Dec 522: 4568/1777 "test mai1" <-Previously received mail
>N 2 root Mon Dec 523: 0818/602 "hello"

You can see the received mail list. N represents unread mail. If it is a read mail, there will be no N in front of it; The next number is the number of the mail. We mainly use this number to operate the mail. If we want to view the first email, just enter the email number 1.

These interactive commands can simplify input. For example, the headers command can directly enter h, which is the command to list mail headers.

Let's explain the common interactive commands:

  • Execute in the interactive command?, You can view the commands supported by this interactive interface.
  • Headers: list the mail headers, and directly enter the h command.
  • Delete: deletes the specified message. For example, if you want to delete the second email, you can enter d 2.
  • Save: save the message. You can save the specified mail as a file, such as s 2/tmp/test.mail.
  • quit: exit and save the mail that has been operated. For example, remove deleted messages, save read messages, etc.
  • Exit: exit without saving any operations.

Use 3: send file content

If we want to send the contents of a file to the specified user, we can execute the following command:

# Send the contents of the / root/anaconda-ks.cfg file to the root user

[root@localhost ~]# mail -s "test mail" root < /root/anaconda-ks.cfg

  -s: Specify message title

When we write a script, sometimes we need the script to automatically send some information to the specified user. It is a very good choice to write the information to be sent to the file in advance. (this method is commonly used to avoid operations on interactive pages.)

Posted on Wed, 10 Nov 2021 08:43:42 -0500 by ticallian