Putty: login, execute command/change environment variable, and do NOT close the session

4

I would like to configure my PuTTY session in such a way that I could login to a remote host, and (a) start bash (b) change the PS1 variable (prompt content) (c) continue work in the session as normal.

I tried:

  1. Connection -> Data -> Environment variables [does not work; session looks as if (b) had not been executed] AND
  2. Connection -> SSH -> Remote command: PS1="some stuff" [the window with the session opens for a fraction of sec, and then it closes]

Any ideas? I had had a look to some similar questions in the forum, but did not find anything helpful.

More generally: I log in into a host where many users log in as a single user, and thus they all share the 'settings' of the single user. I look for a method of forcing PuTTY into changing some of these settings temporarily (i.e., such that they would be time limited to the session that I open using PuTTY). Sort of running personal version of .bashrc within the session. Hope this makes sense.

Simon Righley

Posted 2014-01-29T17:09:11.810

Reputation: 143

Answers

2

The target server needs to be configured to accept setting environment variables for the 1st option to work. The second is actually working fine, the problem is that it is designed to mimic

ssh user@foo command

which will just connect, run command and exit. You can have it remain open by giving it command; bash but that won't work for setting your variables sice a new shell will be started after the variable has been set.

So, short of having root access to the server so that you can enable the setting of environmental variables, the only way I can think of for you to do this is to edit ~/.bashrc the server and define your PS1 there. Add this line to ~/.bashrc:

PS1="some stuff"

Now, every time you log into that server, the prompt will be set for you.


Another way to do this would be to use a different rcfile for your bash session. Create a new file with these lines:

source /etc/profile
source ~/.bashrc
PS1='some stuff'

Save it as, for example, ~/.myps1, then in your putty settings, set the command to run on the remote server to:

bash --rcfile ~/.myps1

This will open a new shell session o the remote server and read in the file above which first reads .bashrc and then sets PS1.

terdon

Posted 2014-01-29T17:09:11.810

Reputation: 45 216

I added a note to the original question. Problem is that the .bashrc is shared among all the users who log in, and I cannot modify it... I could create a personalized .bashrc-my, but then I still don't know how could I call it when logging in with PuTTY. – Simon Righley – 2014-01-29T17:42:04.477

@SimonRighley OK, can you set the server to allow setting of env variables as described in the post I linked to? You could always just add an alias to the shared .bashrc: alias ps1="PS1='some stuff'" and then run the alias each time you log in. – terdon – 2014-01-29T17:54:15.400

@SimonRighley see updated answer for another solution. – terdon – 2014-01-29T18:04:37.040

Fantastic! That did the trick :) Thank you so much! Spent so much time trying to figure this out... Can I now define all sort of aliases, variables and what not in the ~/.myps1 (I called it ~/.bashrc-my_name), and the protocol you described will respect the settings from the default .bashrc, and after that (on top of it?) it will read in all that I'll define in bashrc-my_name? – Simon Righley – 2014-01-29T18:17:10.757

1@SimonRighley yup. As long as you source the global .bashrc first, anything you do after that will supersede any settings you have in .bashrc. Settings in .bashrc that are not overwritten by commands in .bashrc-my_name will persist. – terdon – 2014-01-29T18:18:03.570

One more problem... Some of the environment variables are lost on the way somehow. That is to say that when I 1. login as before, run 'bash', and see for the 'env' I see a much shorter list of variables there as compared to the procedure that you suggested (that is login in with --rcfile, and seeing for 'env' subsequently). Any ideas? – Simon Righley – 2014-01-29T18:37:13.043

@SimonRighley yes, I had already edited my answer to have it also source the global /etc/bash.bashrc file. Try with that added. – terdon – 2014-01-29T18:39:56.980

Unfortunatelly, there is no such file there... Might be related to the fact that's solaris? I also sourced .kshrc and .cshrc but it didn't help. – Simon Righley – 2014-01-29T19:01:40.373

@SimonRighley in any case, I'm being silly, you want the /etc/profile since this is a login shell. See updated answer. – terdon – 2014-01-29T19:03:12.900

Cool! sourcing /etc/profile, together with .kshrc and .cshrc produces the same result for 'env' as in the standard way. I still see some error messages (failed to remove/create some files), but maybe it won't cause any malfunctioning. Thanks a million, man! Have a great day :) – Simon Righley – 2014-01-29T19:38:02.037

0

For those, that cannot modify sshd config for various reasons and/or have +2000 servers (and no access to mass-configuration tools or can't/don't want to change settings for other users), here's a solution I came up with:

enter image description here

In PuTTY load the desired session, go to Connection > SSH. In the "Data to send to the server" section, in "Remote command" field use:

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] bash

Example:

env -u PS1 PS1="[\u@\h]\\$ " bash

I unset the variable first, because it didn't work otherwise.

yahol

Posted 2014-01-29T17:09:11.810

Reputation: 71

0

Try setting the remote command to something like export PS1='foo'; /usr/bin/bash... that should spawn a bash session after setting PS1.

BowlesCR

Posted 2014-01-29T17:09:11.810

Reputation: 2 607

That won't because PS1 will be reset when the new shell is started since PS1 is normally set in one of the global config files. – terdon – 2014-01-29T17:58:50.793

It worked in my environment, but everyone's setup is potentially different. For reference I tested against HP-UX, which is admittedly not very common anymore. – BowlesCR – 2014-01-29T18:02:01.603

I report that, unfortunately, it does not help. The PS1 value is back to the default one. – Simon Righley – 2014-01-29T18:04:29.387

@BowlesCR it worked probably because your PS1 is not being set in any of the default rcfiles. In most cases, starting the new shell will overwrite the exported PS1 with whatever is in the default startup files. – terdon – 2014-01-29T18:05:59.437