16
5
Problem Statement: What is the most elegant and robust way to test if Cygwin mintty
bash
session is "Run as adminstrator"?
Why, specifically? I have typically several mintty
terminals open when using Windows (mintty
does not have tabs). The most awkward is when I need to find a terminal window that I started by right-clicking "Run as administrator" when for example I want to run ping
or other one-time procedure. I would like to indicate the "run as administrator"-ness of the terminal session visually (by changing the bash
shell prompt variable PS1
in my start-up file ~/.bashrc
).
Some quick potential solutions:
- I can compare the value of some environmental variables. By quick look of
env
output there are quite many differences. It is however hard to tell which is most reliable in terms of portability to another Windows machine (perhaps running different version of Windows). id
, more specificallyid -Gn
shows different groups if run as administrator. In my Windows 7 machine I haveAdministrators
androot
groups added to the list. Again, I am not sure if this is portable.- I could try to write a file to a location that would fail as normal user. But I do not want to write any files to strange places - this could in some imagined scenarios be potentially destructive (e.g. failing storage media) and this seems utterly inelegant to my taste.
- Running some Windows program that will indicate by return status or output if the command is run "as administrator". Best would be some with analogous purpose to that of UNIX
id(1)
command (but natively existing in Windows - or Cygwin, but without too far fetched translation of Windows system concepts to POSIX emulated concepts).
Any better or more elegant suggestions? Perhaps cygwin provides a command utility dedicated for this purpose?
Update: 97% duplicate of https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights/ - the difference is just here using bash
instead of (IMHO weird and archaic) Windows cmd.exe
. Please do check the answers and commentaries there.
use the "run as Administrator" unless you do this then the process is not elevated to Administrator permissions even if an Administrator account starts the process. Just create a shortcut so this is always done. – Ramhound – 2013-10-15T13:17:17.250
@Ramhound Of course. But my problem is not how to run as administrator, my problem is how to detect in scriptable way if I am running as administrator. (I just recently had five terminals open, and needed to find the one that I run as administrator so that I could successfully run
ping
.) – FooF – 2013-10-15T13:24:29.403@Ramhound If you had read my (overly verbose?) question, you would have been shocked already three times! Just I am not sure if the two methods (2 and 3; forget the method 1, attempting to create a file to some more protected location) are portable, and I am craving for something more elegant. – FooF – 2013-10-15T13:35:50.797
I did read your question. I don't consider any of those possible solutions as being acceptable. I would just create a shortcut and call it a day. – Ramhound – 2013-10-15T13:41:02.580
1
I recommend trying ConEmu. It has tabs, can run tab as administrator indicated by different icon in tabbar and most importantly uses console behind the scenes so native windows applications work on it correctly unlike on mintty.
– Jan Hudec – 2013-10-15T13:51:47.733@Ramhound So you imply the presence of
Administrators
group is not standard and portable way to distinguish if running as adminstrator in Windows? (Above when saying "group" I am using the concept of cygwin emulation layer, I am not sure what the Microsoft term is for thisAdminstrators
.) I would like to learn more about this. – FooF – 2013-10-15T14:04:25.1672@Ramhound You are right that my original problem (distinguishing the administrator terminal window) that I already had narrowed down into this question could be solved by making a short cut for
mintty
that 1. runs as administrator, 2. uses different configuration file with differing color settings and 3. uses an alternative icon. If you describe to general public how to create this, I will give you an upvote (unless you say in your answer that it cannot be done within a script :-). – FooF – 2013-10-15T14:07:31.713@JanHudec That
ConEmu
sounds wonderful. Some time ago I tried searching something similar but could not quite find one. Thanks so much for the tip. I am definitively going to try it out. – FooF – 2013-10-15T14:11:59.8431@FooF - Just because you run a process with an Administrator level user does not mean the process itself has been escalated. Windows by default will only esclate a process if the user approves of such action hence the "run as Administrator" feature in Windows. You can also provider ALL permissions to ANY group you want even the
Foo
user group if you wanted. – Ramhound – 2013-10-15T14:12:59.243@Ramhound Interesting. I have a strong Unix/Linux background but I am quite a newbie/stranger in Windows world. But this increasingly interesting conversation is perhaps a little bit out of scope in superuser.com. Maybe I should search/ask stackoverflow.com for advice where I could study the central concepts that the Windows GUI and help files so hard tries to obfuscate with its labyrinths of mouse clicking pathways and step-by-step instructions that make it difficult to see the forest from the trees and branches and leaves. :-) – FooF – 2013-10-15T14:50:49.440
1@FooF - Just think it as
sudo
which is required even if you are running as a root user. – Ramhound – 2013-10-15T14:55:47.840@Ramhound Weird, but charmingly so. – FooF – 2013-10-15T14:57:56.973