How to automatically run commands on SSH login?

17

7

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?

Alasdair

Posted 2011-11-08T06:37:52.343

Reputation: 545

You need to read the section INVOCATION in bash(1) (man bash). – Andrew Schulman – 2011-11-08T09:39:45.257

Answers

31

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.

David Z

Posted 2011-11-08T06:37:52.343

Reputation: 5 688

I'm not logging in as root, it's an AWS server so I have to login as "ec2-user" then change to root using "sudo su -". So should I put the commands in "/home/ec2-user/.bashrc"? – None – 2011-11-08T06:45:14.523

5This assumes ~/.bashrc is sourced from your ~/.bash_profile. ~/.bash_profile will be the script that is invoked for a login shell like ssh. I believe ~/.bashrc will get called if you open up a Gnome Terminal, for example, AFTER having already logged in. – dgrant – 2011-11-08T06:46:11.260

You've confused me, let me explain more: This is an Amazon Web Services instance, which is a virtually server, so no one will ever physically log in (I'm not even sure if it physically exists as one machine). I login using PUTTY with the username ec2-user. This drops me in /home/ec2-user directory. I then change to root user by typing "sudo su -", which then drops me into "/root", then I have to type a series of commands, including changing me back to "/home/ec2-user" and setting some variables, aliases, etc. So... how would I get it to do this? – None – 2011-11-08T06:52:04.023

Also, since I am changing user, I expect that in the ec2-user bash profile I should put only "sudo su -", and then the rest of the commands in the root bash file? – None – 2011-11-08T06:52:48.577

NOTE: actually, it's .profile that gets called if it exists, unless .bash_profile, exists, then it is called instead. – dgrant – 2011-11-08T06:52:59.603

First of all, I would recommend putting "sudo" in front of commands that require it, rather than switching to root. – dgrant – 2011-11-08T06:54:36.203

let us continue this discussion in chat

– dgrant – 2011-11-08T06:55:33.363

When you run "su - " it provides a "login shell" a shell as if root had logged in directly. I assume this means it will run the .profile/.bash_profile/.bashrc scripts for root, and remove any of the stuff that was set in the user's .profile/.bash_profile/.bashrc. I haven't used "su - " before, but people use it a lot, and that's what the man page implies it does. – dgrant – 2011-11-08T06:58:51.180

Might be worth noting that you can also put the commands in /etc/profile to run it for any user. – Steen Schütt – 2013-07-04T11:34:07.853

13

Just put this in ~/.bashrc or /etc/bash.bashrc if you want this for all users:

if [[ -n $SSH_CONNECTION ]] ; then
    echo "I'm logged in remotely"
fi

Llamageddon

Posted 2011-11-08T06:37:52.343

Reputation: 1 267

and how do I lose the session after closing execution? – e-info128 – 2014-10-27T18:47:31.820

1@WHK What do you mean by losing the session? – Llamageddon – 2014-10-28T16:16:39.543

@Llamageddon: I think he's wondering the same thing I am. If I wanted to put my entire SSH session into screen, I might use your answer and put screen there instead of the echo. The problem with that is that finishing the screen session will return to the SSH prompt instead of logging out. Just writing out this comment gave me the answer, though: add the logout command after the screen command. – zondo – 2017-01-04T13:53:47.520

This however does not work for ssh session which do not use bash, e.g. when I open an sftp session. – Fabio – 2017-01-28T10:49:53.133

@zondo @e-info128 exec command will replace the current shell with whatever you run. – Llamageddon – 2017-01-29T12:37:14.807

5

Alternatively, you can specify a command to be run during the invocation of ssh:

$ 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.

Dmitry Grigoryev

Posted 2011-11-08T06:37:52.343

Reputation: 7 505

1THIS is very useful. Not sure if it's exactly the answer the questioner asked, but people should know about it, since it may not be obvious. For example "ssh user@host "export x=5; bash" Then in terminal that pops up echo $x displays 5. – clearlight – 2019-07-26T12:00:07.870

3

Actually ~/.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.

brook hong

Posted 2011-11-08T06:37:52.343

Reputation: 279

Not necessarily. It is a location where commands can be placed. – Daniel B – 2018-11-09T06:44:31.300

https://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch08_04.htm – clearlight – 2019-07-26T12:26:27.767

That could REALLY get someone into trouble if they didn't know what they are doing. It's potentially useful in corner cases, but that could REALLY mess someone up if they forgot they put commands there or the name and the path of that file. Gennerally it would be highly preferred to put the startup login commands in the expected place, .bashrc on the remote side and test for $SSH_CONNECTION, as other answers here have suggested. – clearlight – 2019-07-26T12:29:33.353

1

Logging into remote host after executing commands passed to remote host:

  1. Executes .bashrc on remote host
  2. Executes commands passed on command line (using env from remote .bashrc)
  3. Leaves you at bash prompt on remote host (if no errors)

    $ ssh user@host "bash -lc cmd; cmd; ..."

    -l option makes it a login shell (e.g. execute .bashrc)

    -c option provides the explicit command bash will run

clearlight

Posted 2011-11-08T06:37:52.343

Reputation: 499