Skip to content

Instantly share code, notes, and snippets.

@kasunbg
Created April 23, 2019 13:37
Show Gist options
  • Save kasunbg/5502cb630429819d07b5dc0cfa26813c to your computer and use it in GitHub Desktop.
Save kasunbg/5502cb630429819d07b5dc0cfa26813c to your computer and use it in GitHub Desktop.
How to run a GUI application as a different user?

How to run a GUI application as a different user:

Suppose you are logged into your Linux box as user1 using some X window system already (with GNOME/Unity/..). Now you want to run some gui app as another user, user2.

Run:

  1. xhost + - Run as the user1. This enables access to user1's X window system from other users. You should see following output: 'access control disabled, clients can connect from any host'
  2. sudo su - user2 - Using your favorite terminal, log-in to second user.
  3. export DISPLAY=:0.0 - optional for some apps.
  4. # sudo mount --bind /home/user1 /home/user2 - optional. This mounts the /home/user1 dir into /home/user2. This is useful if you want to reuse user1's application configs. This can break some other apps sometimes, so only do this if this is really necessary. You can unmount at the end via sudo umount /home/user2.
  5. execute the gui command:

firefox

@IOKernel
Copy link

i got "cannot open display :0.0" error

That means the value of step #3 is not correct for your system. I believe you can find the correct value for DISPLAY by running echo $DISPLAY as user1.

THANK YOU! this helped

@intgr
Copy link

intgr commented Mar 7, 2021

I wrote ego (Alter Ego) for this use case, it automatically handles xhost and Wayland and PulseAudio socket sharing: https://github.com/intgr/ego

So you just run ego firefox or ego -u user2 firefox

If you run into problems, please open an issue on GitHub. I may be the only user of it, so it hasn't gotten much testing yet.

@dement6d
Copy link

dement6d commented Mar 9, 2022

hey this works for some apps but for example FL Studio through Lutris doesnt work, ALSA throws errors

ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Permission denied

do you know how to fix this? @kasunbg

@ricardobalk
Copy link

Hey, thanks for this. Instead of xhost +, which disables access control (clients can connect from any host), I would recommend using xhost +SI:localuser:user2. And personally, instead of sudo su - user2, I prefer using plain su user2, which asks for user2's password instead of my own one.

But I'm happy that it works!

@ricardobalk
Copy link

hey this works for some apps but for example FL Studio through Lutris doesnt work, ALSA throws errors

ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Permission denied

do you know how to fix this? @kasunbg

Tried this with Spotify:

Be sure to add your user to the audio group before using su
sudo usermod -G audio -a user2

su user2

Then, as the substituted user,

  • unset XDG_RUNTIME_DIR would be smart, to prevent writing to the wrong home directories, as some GUI apps will use this variable.
  • Run an app that uses audio

Let me know if it works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment