How to check if cygwin mintty/bash is run as administrator?

298

Solution 1

I just wrote this function for the same reason. I never know which shell has admin privileges.

function isadmin()
{
    net session > /dev/null 2>&1
    if [ $? -eq 0 ]; then echo "admin"
    else echo "user"; fi
}

It adapted from this answer https://stackoverflow.com/a/11995662/307968 for windows cmd shell. Net Session returns 0 status if you are admin.

Now I just need to change my prompt, or maybe the titlebar color....

Solution 2

The definitive answer to this question comes from the Cygwin mailing list. A process is running with Administrator rights if the user who started it is part of group 544 (Administrators). Also from the comment below by Cromax, it seems that group 114 (Local account and member of Administrator group) is also sometimes present. The test for these two groups is

id -G | grep -qE '\<(114|544)\>'

or in bash without external calls,

[[ "${GROUPS[@]}" =~ (^| )(114|544)( |$) ]]

For example,

id -G | grep -qE '\<(114|544)\>' && echo admin || echo user

In the past you also needed to check for group 0, the root group in /etc/group. But /etc/group is no longer installed in Cygwin, and should usually be removed if it's present, so it's no longer recommended to check for group 0 too.

Solution 3

I use the return value of the Windows program at. I also recreated the functionality of the PROMPTING special character \$.

# Set a white $ initially
eStyle='\[\e[0m\]$'

# If 'at' succeeds, use a red # instead
at &> /dev/null && eStyle='\[\e[0;31m\]#\[\e[0m\]'  # Use # in red

PS1='\n\[\e[0;32m\]\u@\h \[\e[0;33m\]\w\[\e[0m\]\n'"$eStyle "

Examples

Solution 4

id -G | grep -qE '\<(544|0)\>' didn't seem to work for me, as my output had neither <> or 544 even when elevated. However, since elevation is required for writing to %WINDIR%\system32, I used that to test for elevation with a shell function:

is_elevated() { 
   [[ $(uname -o) -eq "Cygwin" ]] || return 1
   touch $WINDIR/system32/.cyg_elevated &> /dev/null
}

When applied to Steven's excellent idea of a red hash character:

is_elevated && PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\[\e[0;31m\]#\[\e[0m\] '

Solution 5

Do something that only admin could do and test for success or fail:-

if touch c:/Users/.x ; then  echo 'ok'  ; fi

or

touch c:/Users/.x && echo ok

or

touch c:/Users/.x && \rm c:/Users/.x && echo ok

or

touch c:/Users/.x  &> /dev/null && \rm c:/Users/.x && echo you are admin
Share:
298

Related videos on Youtube

Bunyk
Author by

Bunyk

Updated on September 18, 2022

Comments

  • Bunyk
    Bunyk over 1 year

    I want to build a bar chart that shows the utilization of some resources. Let's say characters in a text:

    from collections import Counter
    import matplotlib.pyplot as plt
    
    raw_data = 'data to make example bar chart'
    counts = Counter(raw_data)
    keys, values = zip(*counts.most_common())
    plt.bar(keys, values);
    

    This produces the following chart, with absolute counts of characters: Resulting bar chart

    If I transform values before plotting using for example

    values = [v/len(raw_data) * 100.0 for v in values]
    

    I would get exactly same graph, but value for a would be 20.0 (%).

    Question is, could I somehow show two values on y axis?

    I saw recipes on how to show two different functions of the same value and have scale to the left and right, but here I have one function, just different units of measurement. Could I somehow show two scales without plotting two bar charts?

    https://matplotlib.org/gallery/api/two_scales.html

    • Admin
      Admin over 10 years
      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.
    • Admin
      Admin over 10 years
      @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.)
    • Admin
      Admin over 10 years
      @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.
    • Admin
      Admin over 10 years
      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.
    • Admin
      Admin over 10 years
      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.
    • Admin
      Admin over 10 years
      @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 this Adminstrators.) I would like to learn more about this.
    • Admin
      Admin over 10 years
      @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 :-).
    • Admin
      Admin over 10 years
      @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.
    • Admin
      Admin over 10 years
      @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.
    • Admin
      Admin over 10 years
      @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. :-)
    • Admin
      Admin over 10 years
      @FooF - Just think it as sudo which is required even if you are running as a root user.
    • Admin
      Admin over 10 years
      @Ramhound Weird, but charmingly so.
    • Admin
      Admin almost 3 years
      @Foof : If I understand you correctly, you want to see whether you run under elevated privileges, right? In this case, here are a few ways. As far I can tell, the solution which uses OPENFILES should work well from Cygwin bash too.
  • FooF
    FooF about 10 years
    Assuming there is no equivalent of UNIX id command in Windows, I accept this as a correct answer. At least running at command without any arguments does not appear to have any side effects. I am going to use this method in my .bashrc file from now on! Thanks for the hack.
  • zzapper
    zzapper over 9 years
    doesn't seem to work on windows8.1
  • Steven
    Steven over 9 years
    @zzapper Please be more specific. What happens / doesn't happen? Error messages?
  • zzapper
    zzapper over 9 years
    id returns same values, at is deprecated use schtasks.exe instead but this works whether admin or not
  • FooF
    FooF about 9 years
    This seems the same method as @Steven gave in earlier answer (which does not seem to work on Windows 8.1 according to a comment - unless MSYS "at" is somehow). While the question was about Cygwin, it is interesting to compare with similar MSYS. Thanks for the contribution.
  • sparrowt
    sparrowt about 9 years
    Very similar yes. However I ended up on this question trying to use it to check in a script (Win7, Msys) rather than setting the bash prompt style, so I thought this might be of use to someone else doing the same
  • FooF
    FooF about 9 years
    I agree on the usefulness.
  • sparrowt
    sparrowt about 8 years
    Unfortunately this doesn't work in Mingw/Msys. Steven's answer does, although only on Windows 7.
  • FooF
    FooF almost 8 years
    Nice find. Though there seems to be edge cases where this too fail, if you check the stackoverflow comments.
  • FooF
    FooF almost 8 years
    Nevertheless I accepted this answer because it points to a much comprehensive discussions (the stackoverflow question and the accepted answer there currently have scores of 147 and 255, respectively).
  • Cromax
    Cromax over 6 years
    I've just tried that (on W7) and it didn't work, but I've noticed, that if Window's user one is logged as is also an administrator, then when when Cygwin is ran as administrator, there is another group that pops in id's output, namely 114 (Local account and member of Administrator group). So the regexp should be updated to: id -G | grep -qE '\<(114|544)\>' && echo admin || echo user
  • Andrew Schulman
    Andrew Schulman over 6 years
    Thanks. I haven't seen that myself, but I see that group 114 does seem to be an Administrators group, so it seems reasonable to check for it too. I revised the answer.
  • HelloGoodbye
    HelloGoodbye about 5 years
    Thanks! I use this to selectively color the username in my prompt. If the sell is elevated, I color it red, otherwise green. Useful to keep track of which shells are elevated if you have many terminal windows open.