For /etc/profile.d script- need a test for whether user is logging in via terminal or gui

Hi-
On Rocky 9.2-
I have .sh and .csh scripts in /etc/profile.d/ that need a test for when user logs in via ssh or via the GUI interface. Target host is running a GUI at all times. I don’t want users who are logging in via SSH to apply the logic in the two scripts. Would anyone happen to have any ideas re:test syntax? Need for both .sh and .csh (tcsh) profile.d scripts. Thank you.

You could test for the presence of SSH-related environment variables like $SSH_CONNECTION or $SSH_CLIENT.

if [ -n "$SSH_CONNECTION" ]; then echo connected via ssh; fi

Or, to invert that and make it multi-line:

if [ ! -n "$SSH_CONNECTION" ]; then
  echo not connected via ssh
fi

I can’t give you the csh version as it’s been too long. I wouldn’t bother with it, myself. Does anybody still use it?

Personally, I wouldn’t put anything that needs a GUI in the /etc/profile.d/ directory

For GUI logins, create a .desktop file in /etc/xdg/autostart/ directory - for example, see here for more info - the Exec argument can be a script that does whatever is needed for the GUI logins

2 Likes

Indeed.

Since the thing has to be done only on GUI sessions, it makes sense that it is initiated by the GUI session, rather than the for-all-shells machinery.


There might still be some third-party applications that depend on tcsh.

The environment modules that are used, particularly on HPC clusters, are for convenient modification of shell environment. There are at least two implementations, one based on Tcl and the other on Lua. Both apply settings from their respective module syntax into current shell, whichever it is. That is naturally different from each shell sourcing their own set of files. Point is that HPC user may use bash or tcsh, but the admins maintain only one set of modules.

Thank you- I will try this.

@lerroot there’s also a fancier way - use pstree -Alsu "$$" with (first what came to my mind) grep -o ssh or grep -c ssh. Just an idea, I know it’s over-complicated :slight_smile:

Similar to @mrd83’s example, I have the following in a script, though it’s only for the purpose of logging info about types of logins.

login_parent=$(pstree -laAps $$ | sed -n '2{s/,.*$//;s/^[^-]*-//;p}')
case ${login_parent} in
  lightdm)
    login_type="linux-gui"
    ;;
  login)
    login_type="linux-console"
    ;;
  sshd)
    login_type="linux-ssh"
    ;;
  xrdp*)
    login_type="linux-xrdp"
    ;;
  *)
    login_type="linux"
esac
1 Like

@linde
Yeah, I recently managed to configure saving username in .bash_history for standard bash history, using bash one-liner (will probably do a tutorial some day) - and utilizing pstree -Alsu "$$" guarantees that users doing sudo su - (instead of sudo su) will also be logged :slight_smile:

For those curious: full command uncovering actual user is:
pstree -Alsu "$$" | sed -n "s/^[^\(]*(\([^)]*\)).*($USER)[^(]*$/\1/p"

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.