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?

share|improve this question

migrated from stackoverflow.com Nov 8 '11 at 7:12

This question came from our site for professional and enthusiast programmers.

    
You need to read the section INVOCATION in bash(1) (man bash). – Andrew Schulman Nov 8 '11 at 9:39
up vote 19 down vote accepted

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.

share|improve this answer
    
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"? – Alasdair Nov 8 '11 at 6:45
5  
This 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 Nov 8 '11 at 6:46
    
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? – Alasdair Nov 8 '11 at 6:52
    
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? – Alasdair Nov 8 '11 at 6:52
    
NOTE: actually, it's .profile that gets called if it exists, unless .bash_profile, exists, then it is called instead. – dgrant Nov 8 '11 at 6:52

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
share|improve this answer
    
and how do I lose the session after closing execution? – e-info128 Oct 27 '14 at 18:47
1  
@WHK What do you mean by losing the session? – Llamageddon Oct 28 '14 at 16:16
    
@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 Jan 4 '17 at 13:53
    
This however does not work for ssh session which do not use bash, e.g. when I open an sftp session. – Fabio Jan 28 '17 at 10:49
    
@zondo @e-info128 exec command will replace the current shell with whatever you run. – Llamageddon Jan 29 '17 at 12:37

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.