sudo: command not found - linux

I am trying to execute screen as another user using sudo.
I'm using the command:
echo 'userpassword' | /usr/bin/sudo -u 'myuser' -S '/usr/bin/screen -ls'
Any help found on the internet says that the sudo clears the environment variables (like PATH). So I decided to use the full path to the applications but I'm still getting the command not found error.
Error:
sudo: /usr/bin/screen -ls: command not found
Sudo is installed on the system.
Screen is installed on the system.
For sudo, I have tried the -E and -H flag but it doesn't help.
I tried to set PATH variable using something like this:
... | /usr/bin/sudo -u 'myuser' -S 'env PATH=$PATH; /usr/bin/screen -ls'
Supposedly the $PATH was suppose to expand before the command executes but I was getting other errors...
Can someone provide a command that will let me execute commands as another user and explain what each part of the command does so I can understand it?
Thanks.

Try,
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH
Probably you replaced the path variable while trying to set a new path variable.
Going forward, do 'echo $PATH' before adding a new path variable.

There doesn's seem to be any need to encapsulate command in quotes, without them it even works.
echo 'userpassword' | /usr/bin/sudo -u 'myuser' -S screen -ls

Related

How to change default shell of OpenWrt?

The default shell of OpenWrt is ash, but I would like to change it to fish.
When I ran chsh -s /usr/bin/fish (the absolute path of fish), it returned -ash: chsh: not found.
The I changed the first line of /etc/passwd from:
root:x:0:0:root:/root:/bin/ash
to:
root:x:0:0:root:/root:/usr/bin/fish
I could't login again (wrong password), and the system log showed:
authpriv.warn dropbear[14288]: User 'root' has invalid shell, rejected
Is there any other way to change the default shell?
(By the way, I am using a popular fork of OpenWrt instead of the official, but it doesn't seem to be the reason of this problem)
chsh -s /usr/bin/fish is correct.
But openwrt doesn't have chsh command installed.
You need to run
opkg install shadow-chsh first to install the chsh command.
Then run
chsh -s /usr/bin/fish
Finally, run
echo $SHELL to see if the replacement is successful.
Note that the above operations require root privileges to run.
Sorry for my poor English, hope you can understand.
There are two ways to solve this. You can either:
Add /usr/bin/fish to /etc/shells
This solution is provided by #glenn-jackman above in the comments.
Or:
Replacing dropbear by openssh-server
I've figured out another way: if you happen to have openssh-server installed, I would recommend you to use it as default following this tutorial.
And remember to change the first line of /etc/passwd to:
root:x:0:0:root:/root:/usr/bin/fish

Bash: sourcing file as user from script

I am creating a script meant to be run as superuser that reads a file and runs a number of scripts on behalf of all users. The important bit is this:
sudo -u $user -H source /home/$user/list_of_commands
However, whether I encose the command with quotesor not, this fails with:
sudo: source /home/user/list_of_commands: command not found
I have even tried with the . bash builtin:
sudo: . /home/user/list_of_commands: command not found
Of course running source outside a sudo environment works. I thought there might be a PATH problem, and I tried to bypass it by providing the full path to source. However, I cannot find the executable: which source returns which: no source in (/usr/local/sbin:usr/local/bin:usr/bin). So I'm stuck.
How do I make a script source a file as a user?
source is a builtin not a command, use it with bash -c:
sudo -u $user -H bash -c "source /home/$user/list_of_commands"

What does this command mean?

While installing QT, I met this command:
sudo -s chmod u+x QtSdk-offline-linux-x86_64-v1.2.1.run
I wonder what -s means here.And it seems similar here:
sudo -s ./QtSdk-offline-linux-x86_64-v1.2.1.run -style cleanlooks
sudo -s runs a shell with root privileges
"The -s (shell) option runs the shell specified by the SHELL
environment variable if it is set or the shell as specified in
passwd(5). If a command is specified, it is passed to the shell for
execution. Otherwise, an interactive shell is executed."
From here.
As mentioned in the comments above, check out the man info:
Type man sudo to your command line.
Find -s in the list of commands for a good explanation.

Why this linux command can affect the environment variables?

When I changed my current user to admin using
sudo su admin
I found that the environment variable changed too. What I intend to do is to change my user to admin with the env not changed.
Then I found a command as follows:
sudo bash -c "su - admin"
This command does indeed what I want, but I googled about bash -c, with no clue to why this command can do that for me. Could anyone give me a clear explanation? Thanks a lot.
first you should read the sudo manpage and set theses options in the /etc/sudoers file or you can do it interactively (see second below).
default sudoers file may not preserve the existing $USER environment unless you set the config options to do so. You'll want to read up on env_reset because depending on your OS distribution the sudo config will be different in most cases.
I dont mean to be terse but I am on a mobile device..
I do not recommend using sudo su .. for anything. whomever is sharing sudo su with the public is a newb, and you can accomplish the same cleaner with just sudo.
with your example whats happining is you are starting a subshell owned by the original user ("not admin") . you are starting the subshell with -c "string" sudo has the equivelant of the shell's -c using -s which either reads the shell from the arg passed to -s or the shell defined in the passwd file.
second you should use:
$ sudo -u admin -E -s
much cleaner right ? :)
-u sets the user, obviously
-s we just explained
-E preserves the orig user env
see for yourself just
$ echo $HOME # should show the original users /home/orig_user
$ env
your original env is preserved with none of that sudo su ugliness.
if you were interested in simulating a users login without preserving the env..
$ sudo -u user -i
or for root:
Might require -E depending on distro sudoers file
$ sudo -s
or
$ sudo -i
-i simulates the login and uses the users env.
hopefully this helps and someone will kindly format it to be more readable since im on my mobile.
bash with -c argument defines below.
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
Thanks & Regards,
Alok

Executing db2 command through ssh

trying to ssh to another system then perform db2 commands however using 'su db2admin -c' does not seem to work, although it works for normal system commands ..
#!/bin/bash
sshpass -p 'passw0rd' ssh root#server.com "su db2admin -c 'db2text start'"
this is the output ..
rob#laptop:~/Desktop$ ./script.sh
bash: db2text: command not found
Any ideas?
The PATH is not getting updated to the normal root users PATH. Either specify the full path to db2text or add a dash (-) before the username to reload the environment variables
I'll hazard a guess and say that root doesn't have any of the db2 stuff in hi path.
And since you're using su db2admin rather than su - db2admin db2admin inherits
root's environment. Try with that extra - thrown in.
That all said: why on earth aren't you connecting w/ passwordless keys as db2admin?
Another solution that worked ..
#!/bin/bash
sshpass -p 'passw0rd' ssh root#server.com "su db2admin -c '~/sqllib/bin/db2text start'"
But problem is db2 path may change, better to use Eric's answer.

Resources