Linux: How to send message to local user logged into X11?

The xmessage tool lets you do exactly that; present a popup window to users with a text message inside.

Example: user2 logs into a BASH session on the box and issues:

export DISPLAY=':0.0'
xmessage "some message here"

if you log into a TSCH session, it'd be something like this:

setenv DISPLAY ':0.0'
xmessage "some message here'

Alternatively, you can send messages to terminal sessions in one of two ways:

Using wall (=warn all [users])

From the man page:
Wall displays the contents of file or, by default, its standard input, on the terminals of all currently logged in users.

If you want to target a specific user, use write. Again, man tells us:

The write utility allows you to communicate with other users, by copying lines from your terminal to theirs.


As a rule, X11 sessions are usually started in a “private” mode, so you require one of two things: either, coöperation from user2 to allow you to access their screens, or superuser privileges to “break in” to their screens.

DANGER: Do keep in mind, once you have access to their screen, you could also do things like capture keystrokes (potentially including passwords) and take screenshots, so this is a big security risk.

In most cases it's preferable to instead use a messaging tool or chat program for these things.

Caveats aside:

Coöperating

If user2 wants to allow user1 to access their screen, then first user2 must issue a command like:

 xhost +si:localuser:user1

This could potentially be added to a start-up script of some kind; I don't know Fluxbox, but I assume it has a rc or session-start hook.

Once this has been issed, user1 can then write to user2's display, if they can find it.

If user2 is aware of this, they can always send it to user1 in some other way.

Finding another user's Display

On a typical workstation, it's unusual to have more than one or two users logged in; in which case, “guessing” that the display you want is 0, 1, or maybe 2 might be a workable solution.

However, for a more complete solution, let's assume that you have a machine where very many users could be signed in, and try to find the one you want.

for proc in /proc/[0-9]*/
do
     if [ Xorg = $(< $proc/comm ) ] 2>/dev/null && \
        [ $(id -u user2) -eq $(< $proc/loginuid) ] 2>/dev/null
     then
        for subproc in $(
        do
            echo "user2 display=" $(perl -ne 'if (m,DISPLAY=(.*)\0,) { print $1 }' < $subproc/environ)
        done
     fi
done

then, user1 can finally (using the DISPLAY value obtained above; I've just written :14.0 here…)

DISPLAY=:14.0 xmessage "Hello, user2"

Sending a Notification instead

In the interests of completeness, the following works on Gnome desktop, at least. I don't know if Fluxbox has a notification widget.

 DISPLAY=:14.0 notify-send "Hello, user2"

This has a lot more user-friendly options for display, and probably will pop up in a rather friendlier way on the user's screen.

Tags:

Linux

Xorg