Home > database >  Prevent .bash_profile from executing when connecting via SSH
Prevent .bash_profile from executing when connecting via SSH

Time:11-21

I have several servers running Ubuntu 18.04.3 LTS. Although it's considered bad practice to auto login, I understand the risks.

I've done the following to auto-login the user:

sudo mkdir /etc/systemd/system/[email protected]
sudo nano /etc/systemd/system/[email protected]/override.conf

Then I add the following to the file:

[Service]
ExecStart=
ExecStart=-/sbin/agetty --noissue --autologin my_user %I $TERM
Type=idle

Then, I edit the following file for the user to be able to automatically start a program:

sudo nano /home/my_user/.bash_profile

# Add this to the file:
cd /home/my_user/my_program
sudo ./program

This works great on the console when the server starts, however, when I SSH into the server, the same program is started and I don't want that.

The simplest solution is to SSH with a different user but is there a way to prevent the program from running when I SSH in using the same user?

CodePudding user response:

The easy approach is to check the environment for variables ssh sets; there are several.

# only run my_program on login if not connecting via ssh
if [ -z "$SSH_CLIENT" ]; then
  cd /home/my_user/my_program && sudo ./program
fi
  • Related