Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This probably has an easy solution.. I want my bash prompt to reflect if I am logged in via ssh or not.
I have tried checking if $SSH_CLIENT is set, but this does not work if I su to root after logging in on a remote machine ($SSH_CLIENT will not be set for root in that case).
A second option I've tried is simply checking if $SCREEN is set, this works well on remote clients. However, when I su to root on my local machine, $SCREEN is no longer set so I get a false alarm.
However, 'w' does not return the word 'ssh' in my case. The 'FROM' field simply states the host from which I am logged in, and that varies.
Also, I just realised that I can avoid the problem (without actually solving it) by using 'su' instead of 'su -' when switching to the superuser as that leaves $SSH_CLIENT intact.
Thanks deesto for the info.
I'm mainly working on Debian GNU/Linux, so do not know much about Darwin.
According to ccargo's profile he is using Slackware, I hope that my solution will work on it.
What to do if you encounter problems with this:
If ps doesn't support "-p" to select a specific pid, then make sure that it lists all pids and grep it as mentioned for Busybox.
If ps doesn't support "-f" to print the executable, then check for other options to do so.
If not possible then reduce the program check to just the name (sshd instead of /usr/bin/sshd). Note that a script called "sshd" could lead to a false positive here.
If /proc is not available, then try to get parent pid via ps and sed.
If all fails maybe you can find other ways/commands to go up the process tree to check for the login method.
Good luck.
Thank you maddes.b! Your script works perfectly on my machines (and yes, they are running Slackware). Thanks also to deesto and chrism01. I'm marking this thread as solved.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.