5

I'm using tcsh, and for a specific project every member of my team connects to a server with the same user. (This is something we cannot change).

The situation arises because I want to have some custom environment variables and aliases, and to do so I have my own .tcshrc file (namely .tcshrc_cust) which I load as the first action when connecting to that machine:

source .tcshrc_cust

and even though this works pretty well, I'm experiencing a problem when using vim: if I get to the shell from inside vim (with :sh) I fall into a normal shell, without my custom env. vars. and aliases.

Is there a solution for this, besides using a different user in that machine?


SOLUTION (given by @Shawn):

I prefixed my key into .ssh/authorized_keys with:

command="setenv subuser noz; tcsh"

and wrote at the end of .tcshrc file these lines:

if ($?subuser) then
    source .tcshrc_$subuser;
endif

And everything works fine now.

1

3 Answers 3

4

You can make .tcshrc check a special environment variable (like subuser), and conditionally source .tcshrc_cust. When you log in, run subuser=nozimica tcsh. It will get that enironment variable and execute your custom rc script. In addition, vim's :sh command will work. You can even make it fancy and source .tcshrc_$subuser; that way everyone could do it.

You can skip the part where you run subuser=nozimica tcsh when you login by having ssh run it for you. If you setup ssh key authentication, then in ~/.ssh/authorized_keys on the server, you can prefix your key with command="subuser=nozimica tcsh", and ssh will run that command for you.

1
  • Your answer has been extremely useful. Thank you very much!
    – nozimica
    Commented Oct 19, 2011 at 20:12
2

You can send along environment variables from your local machine using SSH.

For example, put export FOO_USER="$USER" (in tcsh's version of .bash_profile) or FOO_USER=joe ssh remote-host (on the command line) on your local machine. Edit your ~/.ssh/config to include this: SendEnv LANG LC_* FOO_USER.

Then, on the remote server, you can check for that variable in the .bash_profile (or what have you in tcsh), and perform the appropriate action:

if [ "$FOO_USER" = 'joe' ]; then
    export PS1='--[ Joe rules ]-- \u@\h \w \$ ';
    source tcsh.joe;
elif [ "$FOO_USER" = 'jane' ]; then
    alias ll='ls -Al';
fi

By default, the SSH server only accepts LANG and LC_*, so if you cannot edit /etc/ssh/sshd_config on the server to add AccceptEnv LANG LC_* FOO_USER, you could hack around it by abusing the LC_* variables, e.g. LC_FOO_USER=joe. Update your .bash_profiles (or rather, the appropriate local and remote tcsh files) accordingly.

1
  • This may seem like more work than the authorized_keys approach, and that's because it is. :-) However, it also works for non-key authentication or even when —gasp— the private key is shared among several users.
    – janmoesen
    Commented Oct 19, 2011 at 20:26
1

My team and I have been facing a similar issue with a shared account. We came up with basically the same solution, optimizing it a tiny bit.

Here's what we do. (We use ksh, I don't know much about tcsh but I assume it's pretty similar).

  • Strip down .tcshrc to a bare minimum, so that it's barely usable any more.

  • Force every user to use a .tcshrc_cust

And in the original tcshrc add something like that:

 alias u1='source .tcshrc_user1'
 alias u2='source .tcshrc_user2'
 alias u3='source .tcshrc_user3'

The idea is that it's more convenient to enter just 2 letters than a whole command. Also if the whole team is doing it, it becomes more adopted and easier to maintain.

I'm not saying it's a perfect solution, and I've been looking for the same thing as you for a while now, it's the best I could come up with. Let's hope somebody else has a better idea :)

1
  • Indeed I already follow that strategy, so I have my own alias "sourcing" my custom .tcshrc. Thank you anyway! :D
    – nozimica
    Commented Oct 19, 2011 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .