Putty: login, execute command/change environment variable, and do NOT close the session
Solution 1
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
.
Solution 2
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:
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.
Related videos on Youtube
Simon Righley
Updated on September 18, 2022Comments
-
Simon Righley over 1 year
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:
- Connection -> Data -> Environment variables [does not work; session looks as if (b) had not been executed] AND
- 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 over 10 yearsI 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.
-
terdon over 10 years@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 over 10 yearsThat 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. -
BowlesCR over 10 yearsIt 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.
-
Simon Righley over 10 yearsI report that, unfortunately, it does not help. The
PS1
value is back to the default one. -
terdon over 10 years@SimonRighley see updated answer for another solution.
-
terdon over 10 years@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.
-
Simon Righley over 10 yearsFantastic! 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?
-
terdon over 10 years@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. -
Simon Righley over 10 yearsOne 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?
-
terdon over 10 years@SimonRighley yes, I had already edited my answer to have it also source the global
/etc/bash.bashrc
file. Try with that added. -
Simon Righley over 10 yearsUnfortunatelly, 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.
-
terdon over 10 years@SimonRighley in any case, I'm being silly, you want the
/etc/profile
since this is a login shell. See updated answer. -
Simon Righley over 10 yearsCool! 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 :)