Prevent .bash_profile from executing when connecting via SSH - bash

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/getty#tty1.service.d
sudo nano /etc/systemd/system/getty#tty1.service.d/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?

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

Putty: trying to send multiple commands to remote server but only the first is executed [duplicate]

I want to run multiple commands automatically like sudo bash, ssh server01, ls , cd /tmp etc at server login..
I am using Remote command option under SSH in putty.
I tried multiple commands with delimiter && but not working.
There is a some information lacking in your question.
You say you want to run sudo bash, then ssh server01.
Will sudo prompt for a password in your remote server?
Assuming there is no password in sudo, running bash will open another shell waiting for user input. The command ssh server01 will not be run until that bash shell is exited.
If you want to run 2 commands, try first simpler ones like:
ls -l /tmp ; echo "hi there"
or if you prefer:
ls -l /tmp && echo "hi there"
Does this work?
If what you want is to run ssh after running bash, you can try :
sudo bash -c "ssh server01"
That is probably because the command is expected to be a program name followed by parameters, which will be passed directly to the program. In order to get && and other functionality that is provided by a command line interpreter such as bash, try this:
/bin/bash -c "command1 && command2"
I tried what I suggested in my previous answer.
It is possible to run 2 simple commands in putty separated by a semicolon. As in my example I tried with ls and echo. The remote server runs them and then the session closes.
I also tried to ssh to a remote server that is configured for not asking for a password. In that case, it also works, I get connected to the 2nd server and I can run commands on it. Upon exit, the 2 connections are closed.
So please, let us know what you actually need / want.
You can execute two consecutive commands in PuTTY using a regular shell syntax. E.g. using ; or &&.
But you want to execute ssh server01 in sudo bash shell, right?
These are not two consecutive commands, it's ssh server01 command executed within sudo bash.
So you have to use a sudo command-line syntax to execute the ssh server01, like
sudo bash ssh server01

remote shell auto completion suddenly off

I have created multiple servers via ssh and one of them suddenly has no autocompletion and also doesn't support arrow keys up/down.
Also if I do su username from root my shell looks like this:
$
on the other servers it looks like this:
username#servername:~$
My steps on every server were the same:
ssh root#ip_address
password entry
useradd -m username
passwd username
usermod -a -G sudo username
su username
The systems are all Ubuntu 16.04 Does anybody know whats the issue?
EDIT:
By mistake the last server I have created was an Ubuntu 18.04 machine, which doesn't work correctly. So on the 16.04 machines the bash works fine.
Also, make sure you have the following lines in your .bashrc (these should be there by default):
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi

Why does creating a file inside a docker-machine VM result in permission denied?

I want to create the file /etc/docker/daemon.json in a vm created using docker-machine by SSHing into it as follows
docker-machine ssh manager1 "sudo echo \"{ \"insecure-registries\":[\"192.168.100.99:5000\"] }\" >| /etc/docker/daemon.json"
The above fails with the message
sh: can't create /etc/docker/daemon.json: Permission denied
How can the above be achieved similar to how I've tried to do it since I need this to be placed in a non-interactive bash script.
You command runs the echo as root but the output is handled by the shell redirection which is running as your user. Try instead the following usage of piping to sudo running tee:
docker-machine ssh manager1 "echo \"{ \\\"insecure-registries\\\":[\\\"192.168.100.99:5000\\\"] }\" \
| sudo tee /etc/docker/daemon.json"
Note I also escaped the quotes inside your echo one more time, otherwise the quotes get eaten by the shell/echo command.
For this specific use case, I'd recommend creating your machines with:
docker machine create --engine-insecure-registry 192.168.100.99:5000 ...
in the future.

BASH instead of CSH while running Commands on a Remote Linux Server over SSH

I would like to run a command on a remote server using ssh, under bash, while my default session is csh.
minimal example (true command is more complex and is generated by my IDE remote debugger):
ssh hostname 'ls | head'
I don't have admin privileges. Trying chsh -s /bin/bash results with an error chsh: cannot lock /etc/passwd; try again later.
I tried adding to .cshrc the following
setenv SHELL /bin/bash
exec /bin/bash --login
but it freezes the console when sending the command through ssh (while regular ssh works)
Any idea how to solve that?
NOTE: I must have a solution that would configure the host, because I don't have access to the ssh command which is generated automatically by the debugger of my IDE. On the IDE I can only set the host name and port number. (EDIT) Therefore solutions like ssh hostname '/bin/bash -c "ls | head"' wont apply
EDIT2:
Actual command shown by IDE (again, I can't edit it):
ssh://username#localhost:2213/home/lab/username/anaconda2/envs/tf_011b/bin/python -u /specific/a/home/cc/cs/username/.pycharm_helpers/pydev/pydevd.py --multiproc --qt-support --client '0.0.0.0' --port 41823 --file /home/lab/username/remote_py/nlteach/show_attend_and_tell/train_saat_classifier.py --train_dir=/home/lab/username/nlteach/output/train/d=cub/imSD=11%imSP=rnd%tcSP=cvpr16/CSat/res50%lr0_02LrDTexpLrDc0_938OrmspWDc0/emb=512%ldTrn=0%nU=512%noHid=1%lr=0_02%lrDT=fix%lrDc=1%o=rmsp/
I am not sure why, but on a bash enabled server it works, while it fails on the csh host.
Thanks!
Invoke bash on the remote side, telling it what commands to run:
ssh hostname '/bin/bash -c "ls | head"'
If the command is too complicated (eg because of quotation mark escaping), then write your commands to a script, copy the script, then run the script:
scp script.bash hostname:/tmp/
ssh hostname '/bin/bash /tmp/script.bash'

Automating tasks after vagrant ssh

Wondering if it is possible to automatically run a script or execute a command ONLY after vagrant ssh into the box? I understand that Ansible can provide beforehand installation and set up. But it failed to allow doing things automatically after entering the machine.
I am currently create a file script.sh. The file will be provided to the vagrant via Ansible. After I vagrant ssh into the box, I do bash script.sh to run the script. Is there better way?
Any suggestion would be more appreciated.
Two ways to achieve this,
Say assume your script is in vagrant home directory like,
:~$/home/vagrant/test-me.sh
1) Run command along with ssh
1a) vagrant ssh -- -t '/home/vagrant/test-me.sh; /bin/bash'
**-OR-**
1b) vagrant ssh -c '/home/vagrant/test-me.sh; /bin/bash'
2) Append complete script path in ~/.bashrc file (this should be in vagrant home directory if you are login as user vagrant)
:~$echo '. /home/vagrant/test-me.sh' >> ~/.bashrc

Resources