Whenever I login to my Linux server I'd like to have several commands run automatically (set some variables, change location, etc.)
This needs to be done on user login, not on system start.
How can I set it to do this?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
Put the commands in
~/.bashrc. Anything in there is executed each time you log in.
If you need commands to only run when logging in via ssh (but not when logging in physically), you could probably test for the presence of the
SSH_CONNECTION environment variable, and only run the commands if you find it exists.
Alternatively, you can specify a command to be run during the invocation of
$ ssh -t server 'cmd; exec bash -l'
The last command in the list should start an interactive session in your preferred shell. If you have a lot of commands to run, consider creating a script file on your SSH server.
~/.ssh/rc is a right place for you to add command to run when you log in, rather than any user of the system.
~/.ssh/rc Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started. See the sshd(8) manual page for more information.
To have a script that will execute for any user on login you can add an additional script in:
/etc/profile.d/Hello.sh Hello `whoami`
On login would produce:
$ ssh MyTestUser@localhost Hello MyTestUser
The profile script is executed as the user that is logging in. This may or may not be a problem depending on what you want the script to do.