Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

NB: Yes, I've read How can I tell if I'm in a tmux session from a bash script?

How can I find out whether I am running inside a Tmux session from my shell or shell script or shell function or alias if I used sudo su - in between?:

The problem

share|improve this question

2 Answers 2

% sudo su -- 2<<STATE
>    TMUX='$TMUX'
>    exec 2>/dev/tty 
> STATE
# . /dev/fd/2
# { 
#   echo "$TMUX"
#   echo "$TMUX" >&2
# }

###OUTPUT

/tmp/tmux-1000/default,23878,1
/tmp/tmux-1000/default,23878,1

sudo will close all file descriptors for its invoked process but 0,1,2 and you need /dev/tty on <&0 or you can't enter a password, but if you can accept blocking >&2 for the span it takes to run a single command then you can do the above.

You can use this technique to bring along whatever else you might like as well.

share|improve this answer
1  
Nice, I need to check how well this works in case of unprivileged users running a Cmnd_Alias, but certainly a very interesting piece of info! –  0xC0000022L Jun 6 at 13:57
1  
@0xC0000022L - it works pretty well. It can be controlled with permissions, but one thing about .dot is it doesn't exec anything - it only requires read rights. So even when su 4<<HEREDOC /dev/fd/4\n...\nHEREDOC\n doesn't work su -c '. /dev/fd/4' 4<<.. generally will. –  mikeserv Jun 6 at 14:04
2  
That's not very convenient for an interactive session though. However, you could do something like exec $SHELL 2>/dev/tty –  Gilles Jun 6 at 23:13
    
@Gilles - thanks for that. It took me a minute to figure out what you meant because I thought the shell would automatically reopen >&2 but I was wrong. You likely can't exec it with a new process like that though without chowning the file created by your last user first, but just exec 2>/dev/tty seems to do the trick anyway. I did it but added an >&2 to echo as above and it worked. –  mikeserv Jun 6 at 23:32

In your /etc/sudoers file you can explicitly allow environment variables to propagate to the sudo environment.

Example

Defaults    requiretty

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY" 

So one method would be to add a variable that is resultant of tmux and let it through.

share|improve this answer
    
Thanks, indeed that's one way (btw: one I know and had contemplated). And in this particular case I am able to modify sudoers, but I'll leave the question open to see whether some better method exists. –  0xC0000022L Jun 6 at 13:38
    
That'll work for sudo -s but not for sudo su - since su - also resets the environment. Not that there's usually a good reason to run su - instead of keeping as much information about your environment as you can @0xC0000022L –  Gilles Jun 6 at 23:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.