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

ssh -f [email protected] -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?

  • 1
    Have you looked at this post? Also, see How to run scripts on start up?
    – Lucio
    Commented Mar 19, 2013 at 18:07
  • @Lucio That doesn't address how to make it run for both graphical and non-graphical logins. Commented Mar 19, 2013 at 18:08
  • @EliahKagan Yes, I know that. I'm still looking other post, but those link will be useful to login via GUI.
    – Lucio
    Commented Mar 19, 2013 at 18:09
  • @Lucio I found plenty of explanations how to do it for GUI or for shell or for all users. None of those are what I need.
    – Baruch
    Commented Mar 19, 2013 at 18:11

1 Answer 1


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.

  • 4
    It could work using ~/.bash_profile?
    – Lucio
    Commented Mar 19, 2013 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). Commented Mar 19, 2013 at 18:17
  • 2
    Great answer. If you want to find more information you can look this SO post.
    – Lucio
    Commented Mar 19, 2013 at 18:20
  • 5
    Where would I put it -just- for login to the GUI? Nothing else.
    – Frank N
    Commented Nov 17, 2016 at 9:22
  • 4
    @FrankNocke For GUI only, you can start a script from a .desktop file in ~/.config/autostart/. Or use the gnome-session-properties GUI.
    – mivk
    Commented Feb 12, 2017 at 14:48

Not the answer you're looking for? Browse other questions tagged .