Raspberry Pi errors on VNC login and ssh startup - bash

For some weird reason, this two errors started occurring on ssh connection initiation:
-bash: id: command not found
-bash: [: : integer expression expected
I'm not sure how those errors affect me, but in the last few days my VNC connection to raspberry pi also stopped working (I can see the login screen in the VNC viewer, but after i put my credentials, the screen turns black for a moment and then returns to the same login screen which I'm stuck on...)
I've tried updating my pi version through ssh and use some other commands I've found online, but nothing worked. Any idea how to solve those problems?

It looks like something is trying to load on login.
The places to check are as follows:
~/.bashrc
~/.bash_profile
~/.profile
~/.profile gets ran each time you login to the shell and the others run when running the bash shell.
By the looks of it something is trying to run the command id and as its not installed it's not running.
A quick test to see if this is in any of your files would to run grep in your home area.
# Change to your home area
cd ~/
# Recursively search for a string matching "id"
grep -rsi "id" .
This could explain why VNC is not working, as when you try to login to VNC it tries to load your config from those files and if they error VNC might not launch.

Related

Shell Script Issue Running Command Remotely using SSH

I have a deploy script in which I want to clear the cache of my CDN. When I am on the server and run my script everything is fine, however when I SSH in and run only that file (i.e. not actually getting into the server, cding into the directory and running it) it fails and states the my doctl command cannot be found. This seems to only be an issue with this program over ssh, running systemctl --help works fine.
Please note that I have installed Digital Ocean's doctl using sudo snap install doctl and it is there.
Here is the .sh file (minus comments):
#!/bin/sh
doctl compute cdn flush [MYID] --files [*] # static cache
So I am not sure what the issue is. Anybody have an idea?
Again, if I get into the server and run the file all works, but here is the SSH command I use that returns the error:
ssh root#123.45.678.999 "/deploy/clear_digital_ocean_cache.sh"
And here is the error.
/deploy/clear_digital_ocean_cache.sh: 10: doctl: not found
Well one solution was to change the command to be an absolute path inside my .sh file like so:
#!/bin/sh
/snap/bin/doctl compute cdn flush [MYID] --files [*] # static cache
I realized that I could run my user commands with ssh (like systemctl) so it was either change where doctl was located (i.e. in the user bin) or ensure that the command was called with an absolute path adding the /snap/bin/ in front of the command.

Changing user in bash script

I wanted to create an installation script for my raspberry pi which secures the default installation by configuring/hardening ssh, installing a firewall and fail2ban and finally to get rid off the default user of Raspbian. All other parts work but the final part always shows me an error.
The new user is created and added to the sudo group. After that I want to delete the old user 'pi'. As the script runs with sudo in the user context of 'pi' I thought I could solve this by switching to 'su' but I just get an error that the user couldn't be deleted as it is used by a process:
echo "Enter the new user name? Only lower case letters allowed!"
read user
sudo adduser $user && adduser $user sudo
echo "default user 'pi' will now be deleted"
su -c "deluser -remove-home pi"
If I check with 'users' the user 'pi' is gone but I can still log in with this account. How can I solve this problem inside the script?
I tried the answers I found here: How do I use su to execute the rest of the bash script as that user? and here: https://unix.stackexchange.com/questions/361327/how-to-login-as-different-user-inside-shell-script-and-execute-a-set-of-commands but nothing seem to work. I searched Google but I can't find any solution that works. Is it even possible what I'm trying to?
I usually add set -eux at the beginning of the bash script. This allows to debug and find typos and errors.
Try to switch user inside the script with
sudo -i -u ${user} $(command to delete pi here)
Think i found the cause of the problem. 'set -eux' was a great help:
deluser pi
Removing user `pi' ...
Warning: group `pi' has no more members.
userdel: user pi is currently used by process 445
/usr/sbin/deluser: `/usr/sbin/userdel pi' returned error code 8. Exiting.
I tried ps -fu pi to find the process which causes the trouble: it's /lib/systemd/systemd --user Is there a way to stop this process inside the script?

Raspberry:lauch a webbrowser(or any DISPLAY) on startup

I've been searching on net but no proper solution for the moment.
Raspberry Pi: Launch Python Script on Startup
This guide tells the way to lauch a python on startup.
The key of this guide is the following crontab command:
#reboot sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1
I tried it.It works well only when there is NO operation to open a web or any other display within the python script.
If I want to lanuch a webbrowser using python,this crontab does not work well.
I checked the log and it says:
xhost: unable to open display ":0.0"
no protocol specified
test.py :cannot connect to X server :0.0
So this is a DISPLAY problem.
This is my shell(named laucher.sh) ,mainly used to lauch python:
#!/bin/bash
xhost +local:root
export DISPLAY=:0.0
python /home/pi/test.py
Anyone knows about this DISPLAY problem? Please help....
Thanks a lot!
Sincerely,Helen
I think crontab is not a very useful way to open a DISPLAY on startup because when you open a DISPLAY it requires X server.AND you don't know whether X sever is enabled before the command "#reboot" running or after it running during the process of booting.
I solved this problem by doing the following configuration.
1.Enter these command in terminal:
sudo cd ~./config/lxsession/LXDE-pi
sudo nano autostart
2.Add one command in autostart:
#python /home/pi/test.py
For this example I'm opening a python program that will open a display on startup.
you may replace the above code with any application that opens a display.There might be some slight difference with the grammar but this whole thing is like a conception proving this is also a possible way.

SCP file from ssh session to localhost

I have a headless file server on which I store and manage downloads and media, but occasionally I have to transfer small files back to my computer (Mac, using bash shell). The problem is that some files have more user-friendly names and commonly have spaces in them, and they are buried in the file directory hierarchy I have set up on my server.
When I'm using scp from my local machine, I don't have tab completion, so I have to manually type out the entire path and name with spaces escaped. When I ssh into the server first, the command:
scp /home/me/files/file\ name\ with\ spaces.png Me#localhost:/Users/Me/MyDirectory
fails with the error "Permission denied, please try again" even though I'm entering my local machine user password properly.
I've learned a little bit of sftp since I've been told that may be a better tool for file transfer. However, the utility seems outdated and I still don't have tab completion after establishing a connection to the server (on my Terminal when pressing Tab I just get a tab character).
My question is this: what can I do to allow tab completion while using scp from my Mac? Or am I using incorrect syntax for scp while in an ssh session, and is there something in that command I should fix? Or, is there a (better? newer?) tool other than sftp that would offer tab completion on a server?
Finally, if none of these problems have simple solutions, is there some package I could install (e.g. a completion package from Homebrew or the like) that would facilitate better tab-completion with any of these commands?
Looks to me like this is some incorrect scping.
This is the format of the command
scp ./localFile.txt remoteUser#remoteHost:/remoteFile.txt
You were so close, but you have localhost set where you should have your remoteHost.
localhost is the name that resolves to the machine that you are currently on - so in your workflow, you are sshing to a machine, and then trying to scp that file to the same machine you are already sshd into.
What you need to do, is figure out the IP address, or the physical host name of the computer that you are trying to connect to, and use that instead.
scp ./localFile.txt remoteUser#192.168.1.100:/remoteFile.txt
# where 192.168.1.100 would be the IP of your Mac
I am assuming the reason you were getting permission denied, was because you were using your the login credentials for you mac, but unknowingly trying to login again to your headless machine.

Quitting out of Bash with Putty

I'm trying to ssh into the server. I enter my password (which is accepted) and then it takes me into this:
using username "####".
##hostname## password: ##This is correct
Last lgin: Sun Feb 10 11:47:27 2013 from ####
-bash-3.2$
How do I quit out of bash? I've tried logout and exit and both of them quit out of Putty. When I open Putty again and login it takes me right to -bash-3.2$ again. Thanks.
What you are seing is the prompt, meaning the way the server let's you interconnect with it.
If you want to "navigate" through the server, you will have to use commands like cd (change directory), ls (list), cp (copy) and so on. Try for example to type pwd and you will see the current directory in which you are (pwd stands for print working directory).
There are millions of tutorials of this, google it to find someone. I for example found this one: http://www.gamexe.net/other/beginner-guide-ssh/

Resources