67

I need to run the following command (with real parameters) after login:

ssh -f user@example.com -L 54321:example.com:54321 -N

I need it only for my user, not all users on the machine. Also, I need it to run whether I login via the GUI or via SSH to a plain shell terminal. I would even like to know how to do this on machines without a GUI installed.

Where would I put the above line to be run as explained?

Lucio
  • 18,843
Baruch
  • 782

1 Answers1

101

Adding the command to the end of ~/.profile should do the trick (where ~ represents your home directory).

Not ~/.bashrc, which would make it run again every time a bash shell is started. In contrast, .profile is only run by interactive login shells. See man bash and man sh for more information.

Please note that this will run not just for GUI and SSH logins, but for all logins (or at least all interactive logins), including when you log on in a virtual console.

Eliah Kagan
  • 117,780
  • 4
    It could work using ~/.bash_profile? – Lucio Mar 19 '13 at 18:15
  • 8
    @Lucio If .bash_profile exists, then it overrides .profile for bash. I don't think the GUI uses a bash shell, though. I'll edit this to provide a link to man sh too (though I'm not sure it uses sh/dash either). .profile is universal (and typically, when .bash_profile exists, it's written so it sources .profile). – Eliah Kagan Mar 19 '13 at 18:17
  • 2
    Great answer. If you want to find more information you can look this SO post. – Lucio Mar 19 '13 at 18:20
  • 5
    Where would I put it -just- for login to the GUI? Nothing else. – Frank N Nov 17 '16 at 09:22
  • 3
    @FrankNocke For GUI only, you can start a script from a .desktop file in ~/.config/autostart/. Or use the gnome-session-properties GUI. – mivk Feb 12 '17 at 14:48
  • 1
    @EliahKagan I have a little script that is meant to be run from the commandline and print something out. But if I put that in my .profile file, the script will run and I won't see the standard out. How can I have the script open visibly? Say, to open a terminal first and then run the command? – john_science Jun 22 '17 at 04:23
  • Note that a .desktop file under ~/.config/autostart can be created for you by the "Startup Applications" GUI. (Also, it might help in some cases to run the command within bash, e.g.: bash -c "xkbcomp -I$HOME/.xkb ~/.xkb/keymap/mykbd $DISPLAY" In this case, putting in the command without the bash -c part and double quotes did not work.) – Evgeni Sergeev Sep 05 '19 at 02:13
  • I can run added program to ~/.profile by ssh-ing to my localhost with: ssh usename@localhost – EsmaeelE Sep 29 '19 at 12:28
  • @mivk Thanks. I'll try: https://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login#comment2643237_48327 – Ryan Mar 05 '24 at 20:42
  • Damn. It didn't work. I still needed to run espanso restart AGAIN after I logged in (after waking from sleep). I'll log an issue with Espanso. https://github.com/espanso/espanso/issues/1892 – Ryan Mar 06 '24 at 17:27