remote shell auto completion suddenly off - bash

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

Related

Prevent .bash_profile from executing when connecting via SSH

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

AWS EC2 User Data: Commands not recognized when using sudo

I'm trying to create an EC2 User-data script to run other scripts on boot up. However, the scripts that I run fail to recognize some commands and variables that I'd already declared. I'm running the commands as the "ubuntu" user but it still isn't working.
My user-data script looks something like this:
export user="ubuntu"
sudo su $user -c ". ./run_script"
Within the script, I have these lines:
THIS_PATH="/some/path"
echo "export SOME_PATH=$THIS_PATH" >> ~/.bashrc
source ~/.bashrc
However, the script can't run SOME_PATH/application, and echo $SOME_PATH this returns a blank line. I'm confused because $SOME_PATH/application works when I log into the EC2 using SSH and my debug logs using whoami returns "ubuntu."
Am I missing something here?
Your data script is executed as root and su command leaves $HOME and other ENV variables intact (note that sudo is redundant). "su -" does not help either
So, do not use ~ or $HOME but full path /home/ubuntu/.bashrc
I found out the problem. It seems that source ~/.bashrc isn't enough to restart the shell -- the environment variables worked after I referenced them in another bash script.

How to ssh/sudo su - oracle/run some commands

I have already looked at the following links but didn't managed to make it work:
SSH to server, Sudo su - then run commands in bash
Can I ssh somewhere, run some commands, and then leave myself a prompt?
Run ssh and immediately execute command
I'm trying to automate the following sequence to log in to the database
$ ssh <myserver>
me#myserver$ sudo su - oracle
<enter password>
oracle#myserver$ bash
oracle#myserver$ export ORAENV_ASK=NO
oracle#myserver$ export ORACLE_SID=ORACLEDB
oracle#myserver$ . oraenv
oracle#myserver$ echo $ORACLE_HOME
I tried the command (based on the links above) but that does not work :
ssh -t myserver "echo password | sudo -S su - oracle ; bash ; export ORAENV_ASK=NO"
How can I combine thoses commands in a shell script (or even perl one), execute it and then leave myself at a prompt so I can run sqlplus after? Is that even possible?
Note:
ssh does not need password because we use authorized_keys, BUT Password-less sudo is not an option nor using su directly (I'm not root and cannot change that), the command needs to be "sudo su - oracle" exactly.
Thanks
You can't do that in shell, but you can do it with expect (apt-get install expect if on Debian variants).
This is a very simple expect file. You need to do some research to make it work in your environment but this gives the general idea.
spawn ssh foo#x.x.x.x
expect "~$"
send "sudo bash\r"
expect {
password {send "foobar\r";exp_continue}
"#"
}
send "id\r"
expect "root"
You would run this as expect /path/to/your/expectfile.
This will log in, do sudo with password "foobar", execute id and exit. Would this be of any help?
Hannu

NVM installation per user is not working with automated ssh logins

Normally when I login to my server via putty, I am able to use nvm, grunt, gulp commands but if I connect with php's ssh2 extension or with sshpass through a bash script those commands are not working unless if I execute this commands first:
~/.nvm/nvm.sh
source ~/.profile
nvm current
And this is my ~/.profile file:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
What seems to be issue ? Why I need to execute this commands every time with automation tools such as ssh2 or sshpass ?
My OS is ubuntu 16.04
This has nothing to do with sshpass. You can verify this by running the ssh session without sshpass, and you will see that even if you manually enter the password, it will still not work.
When you run ssh with a command after it, it starts a remote shell that is not connected to a TTY, and that shell is not considered a login shell. For non-login shells, .profile simply isn't run.
The simple solution this problem is to move the environment setup from .profile to .bashrc. .bashrc is run even for non-interactive shells, and so should provide the environment setup you need.
As a side note, why are you using sshpass at all? Why not use the more secure public key authentication?
To clarify, the fact that the shell is not connected to a TTY is unrelated to your problem. At least for openssh, you can tell it to open a TTY even if a command to run is provided by specifying -t on the command line. .profile will still not be run.

CloudFormation userdata to create a RedHat user

How to create a user on redhat instance during the launch? I tried include the following commands in the user data but didn't work. Any idea why? or is there another way of creating a user using userdata?
sudo su
useradd -s /bin/bash -m -d /home/testuser -g root testuser
echo P#ssc0deT3st | passwd testuser --stdin
sed -re 's/^(PasswordAuthentication)([[:space:]]+)no/\1\2yes/' -i.`date -I` /etc/ssh/sshd_config
service sshd restart
Ensure that you use Amazon's ami. Only few ami's support UserData. UserData is run as root.
Try something like this
"UserData": {
"Fn::Base64": {
"Fn::Join": ["", [
"#!/bin/bash -xe\n",
"useradd -d /home/testuser -g root -m -s /bin/bash testuser", "\n",
]]
}
}
Although I'm not sure if you're allowed to add a user to the group root, but that's not here nor there. You can then add the rest of your commands in the same fashion as the useradd command in my example.
Note that sometimes it takes a couple of minutes for the UserData to execute so give it a couple of minutes after the instance is up and running. Also you can you check for errors in /var/log in cmd-init.log I believe.
Make sure to test your commands on a running instance to make sure they work before trying them in the stack it'll save you time in the long run.

Resources