write 命令是Linux系统中用于向其他登录用户的终端发送即时消息的简单工具。它允许系统用户之间进行基本的终端通信,特别适合在多用户环境中进行简短的信息交流。
write [选项] 用户名 [终端名]
| 参数 | 说明 |
|---|---|
用户名 |
指定要发送消息的用户名(必需) |
终端名 |
指定用户登录的终端设备(可选) |
-V, --version |
显示版本信息 |
-h, --help |
显示帮助信息 |
发送消息前,先查看当前登录系统的用户:
who
# 或
w
# 或
users
输出示例:
john tty1 2024-01-15 09:30
alice pts/0 2024-01-15 10:15 (192.168.1.100)
bob pts/1 2024-01-15 10:20 (192.168.1.101)
向用户"john"发送消息:
write john
执行命令后,会进入消息输入模式。输入消息内容,按Enter换行,按Ctrl+D发送:
write john
Hello John,
Please check the server logs when you have time.
Meeting at 2 PM today.
[按 Ctrl+D 发送]
如果用户有多个登录会话,可以指定终端名:
write john tty1
或
write alice pts/0
当用户john收到消息时,他的终端会显示:
Message from alice@server1 on pts/0 at 10:25 ...
Hello John,
Please check the server logs when you have time.
Meeting at 2 PM today.
EOF
用户可以使用mesg命令控制是否接收write消息:
# 允许接收消息(默认)
mesg y
mesg yes
# 禁止接收消息
mesg n
mesg no
# 查看当前消息接收状态
mesg
# 输出示例:is y (表示允许接收)
who -T
# 或
who -w
# 输出示例:
# john + tty1 2024-01-15 09:30
# alice - pts/0 2024-01-15 10:15
# 其中"+"表示允许接收消息,"-"表示禁止接收
系统管理员可以在执行系统维护前通知所有用户:
# 先查看所有登录用户
who
# 逐个发送通知
for user in $(who | awk '{print $1}' | sort -u); do
echo "System maintenance scheduled at 3 PM. Please save your work." | write $user
done
# 向同事询问问题
write bob
Hi Bob,
Could you help me with the database backup script?
I'm getting permission errors.
[Ctrl+D]
$ write john
write: john is not logged in
解决方案:检查用户是否已登录,或改用其他通信方式(如email)。
$ write alice
write: alice has messages disabled
解决方案:请求对方启用消息接收:mesg y
write: cannot open /dev/pts/2: Permission denied
解决方案:确保有访问目标终端的权限,或使用sudo权限。
| 命令 | 说明 |
|---|---|
wall |
向所有登录用户广播消息 |
mesg |
控制终端消息接收功能 |
talk |
双向终端对话工具 |
who |
显示登录用户信息 |
w |
显示登录用户及其活动 |
wall(write all)命令用于向所有登录用户发送广播消息:
# 发送广播消息
wall "System will reboot in 5 minutes. Please save your work."
# 从文件发送广播
wall < announcement.txt
# 交互式输入广播内容
wall
Important: Server maintenance starting now...
[按 Ctrl+D 发送]
mesg n虽然write命令仍然可用,但在现代Linux环境中,有更多先进的通信方式:
notify-send发送桌面通知(GUI环境)