/usr/bin/sudo: Permission denied when calling sudo from sh script via telegra-cli with lua script - lua

Im trying to run my .sh scipt status.sh via a telegram message:
Ubuntu 20.04.1 LTS server
Telegram-cli with a lua script to action status.sh script
when i send the message "status" to my server via telegram it actions the status.sh script, in this script i have a bunch of stuff that gathers info for me and sends it back to telegram so i can see what the status of my server is, however (i recently did a fresh install of the server) for some reason if the script has a line of code starting with sudo i get:
line 38: /usr/bin/sudo: Permission denied
if i run the script from the command line ./status.sh it runs without any problem!? so im thinking its because it is being called from telegram or lua!?
example of code that generates the error: sudo ifconfig enp0s25 >> file
on the other hand this line works without a problem:
sudo echo Time: $(date +"%H:%M:%S") > file
/usr/bin has 0755 permission set
sudo has 4755 permission set

The following command
sudo ifconfig enp0s25 >> file
would not work if file requires root privilege to be modified.
sudo affects ifconfig but not the redirection.
To fix it:
sudo sh -c 'ifconfig enp0s25 >> file'

As mentioned in Egor Skriptunoff's answer, sudo only affects the command being run with sudo, and not the redirect.
Perhaps nothing is being written to file in your case because ifconfig is writing the output you are interested in to stderr instead of to stdout.
If you want to append both stdout and stderr to file as root, use this command:
sudo sh -c 'ifconfig enp0s25 >> file 2>&1'
Here, sh is invoked via sudo so that the redirect to file will be done as root.
Without the 2>&1, only ifconfig's stdout will be appended to file. The 2>&1 tells the shell to redirect stderr to stdout.
If file can be written to without root, this may simplify to
sudo ifconfig enp0s25 >> file 2>&1

Related

Is there any way to run "pkexec" from a docker container?

I am trying to set up a Docker image (my Dockerfile is available here, sorry for the french README: https://framagit.org/Gwendal/firefox-icedtea-docker) with an old version of Firefox and an old version of Java to run an old Java applet to start a VPN. My image does work and successfully allows me to start the Java applet in Firefox.
Unfortunately, the said applet then tries to run the following command in the container (I've simply removed the --config part from the command as it does not matter here):
INFO: launching '/usr/bin/pkexec sh -c /usr/sbin/openvpn --config ...'
Then the applet exits silently with an error. While investigating, I've tried running a command with pkexec with the same Docker image, and it gives me this result:
$ sudo docker-compose run firefox pkexec /firefox/firefox-sdk/bin/firefox-bin -new-instance
**
ERROR:pkexec.c:719:main: assertion failed: (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) > 0)
But I don't know polkit at all and cannot understand this error.
EDIT: A more minimal way to reproduce the problem is with this Dockerfile:
FROM ubuntu:16.04
RUN apt-get update \
&& apt-get install -y policykit-1
And then run:
$ sudo docker build -t pkexec-test .
$ sudo docker run pkexec-test pkexec echo Hello
Which leads here again to:
ERROR:pkexec.c:719:main: assertion failed: (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) > 0)
Should I conclude that pkexec cannot work in a docker container? Or is there any way to make this command work?
Sidenote: I have no control whatsoever on the Java applet that I try to run, it is a horrible and very dated proprietary black box that I am supposed to use at work, for which I have no access to the source code, and that I must use as is.
I have solved my own problem by replacing pkexec by sudo in the docker image, and by allowing passwordless sudo.
Given an ubuntu docker image where a user called developer was created and configured with a USER statement, add these lines:
# Install sudo and make 'developer' a passwordless sudoer
RUN apt-get install sudo
ADD ./developersudo /etc/sudoers.d/developersudo
# Replacing pkexec by sudo
RUN rm /usr/bin/pkexec
RUN ln -s /usr/bin/sudo /usr/bin/pkexec
with the file developersudo containing:
developer ALL=(ALL) NOPASSWD:ALL
This replaces any call to pkexec made in a process running in the container, by a call to sudo without any password prompt, which works nicely.

'bash' : no such file or directory

I just joined the windows insider program so I could install Ubuntu and install Ruby via bash commands. Everytime I open the the ubuntu command prompt it will not allow me to run any commands without throwing errors. examples
Command 'sudo' is not availiable in '/usr/bin/sudo'
At the top of the Ubuntu command prompt it says:
/usr/bin/env: 'bash': no such file or directory
How can I resolve this issue. I need to set up ruby so I can start developing.
You can check content of PATH variable by executing command $ echo $PATH
If you do not find /usr/bin in the output than you can append /usr/bin in PATH variable by executing command
$ export PATH=$PATH:/usr/bin

Where to set LEIN_ROOT?

When using sudo lein run (because some of the files changed by that command need priveleges) I get this message:
WARNING: You're currently running as root; probably by accident.
Press control-C to abort or Enter to continue as root.
Set LEIN_ROOT to disable this warning.
Any idea how or where to set LEIN_ROOT in order to avoid getting this message?
Add LEIN_ROOT=true to the end of /etc/profile. For this change to take effect, enter source /etc/profile to a terminal. Then run the command with sudo -E lein run to preserve environment variables.
If you are doing this over ssh you would need to do all of the above on the server then add source /etc/profile to the start of the ssh command run on the local machine.
ssh user#123.456.789 "source /etc/profile; sudo -E lein run"

Redirection of stdout and stderr to file not working on raspbian

I am trying to redirect STDOUT and STDERR to a log file on raspberry pi.
My .sh script contains this line
sudo ./main.py &> client.log &
The script runs correctly as it transfers data to and from my server, but client.log file remains empty. I tried &>; &>>; >> with 2>&1; and |&. None of them write any data to client.log.
sudo ./main.py
produces both stdout and stderr output. What am I doing wrong?
The syntax you are looking for is:
sudo ./main.py > client.log 2>&1 &
> client.log redirects standard output to the file client.log
2>&1 redirects stderr to stdout
& at the end of the line runs this in the background so you can continue working at the command prompt.
Note: if you log off while the background command is running, it will be killed. You can override this behavior by adding nohup to the beginning of the line. For more information google bash jobs
Edited to add additional information after comment below
revised syntax:
sudo stdbuf -o L -e L ./main.py > client.log 2>&1 &
stdbuf modifies the default linux output buffering
-o L Flushes stdout at the end of every line
-e L Flushes stderr at the end of every line
python -u test.py > output.txt &
Python will buffer your output by default, and simply killing the script doesn't immediately flush that standard output to disk

How do i execute script in supervisor?

I have a start-all.sh I specified it in CMD of docker. It works as expected.
start-all.sh
#!/usr/bin/env bash
echo "It's Me Dear"
/etc/init.d/hadoop-hdfs-namenode start
/etc/init.d/hadoop-hdfs-datanode start
/etc/init.d/hadoop-hdfs-secondarynamenode start
/etc/init.d/hadoop-0.20-mapreduce-tasktracker start
sudo -u hdfs hadoop fs -chmod 777 /
/etc/init.d/hadoop-0.20-mapreduce-jobtracker start
/etc/init.d/flume-ng-agent start
/bin/bash
I can't able to specify the same file in supervisord. I tried
[program:long_script]
command=bash /usr/local/start-all.sh
start-all.sh present in the same file with mode 777. How do i resolve this?
[program:job_tracker]
command=bash -c "/etc/init.d/hadoop-0.20-mapreduce-jobtracker start"
It starts and exits. I checked the log file. It says permission denied. I have to add sudo -u hdfs hadoop fs -chmod 777 / in the same command.
command=bash -c "sudo -u hdfs hadoop fs -chmod 777 /;/etc/init.d/hadoop-0.20-mapreduce-jobtracker start"
It doesn't work. I specified two commands but this also doen't work. Any idea?
EDIT
My file exists at /usr/local/start-all.sh How do i ensure whether supervisor is looking at correct directory?
http://supervisord.org/configuration.html#program-x-section-settings
If you look at the example file (several screens down, they don't have an anchor for that spot), you'll see something like:
directory=/tmp
You can specify which directory to run the command from.
There is also a user option for supervisord.
user=hdfs
If supervisord runs as root, this UNIX user account will be used as the account which runs the program. If supervisord can’t switch to the specified user, the program will not be started.

Resources