This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 4 years ago.
Im trying to use jq to alter a json file via a script.
in my terminal im trying to run this, but getting a permission denied error. Im able to change it using a sudo nano manually.
sudo jq -c '.interpreterSettings."2ANGGHHMQ"."properties"."zeppelin.pyspark.python" = "python3"' interpreter.json > tmp.$$.json && mv tmp.$$.json interpreter.json
Any ideas on why?
Thanks,
Tim
Simple fix, just gave user permissions on directory.
sudo chmod -R 777 /etc/zeppelin/conf
Related
This question already has answers here:
sudo echo "something" >> /etc/privilegedFile doesn't work [duplicate]
(15 answers)
bash alias using sudo returning command not found
(2 answers)
Closed 1 year ago.
I have the following alias in my .bashrc file,
alias genpass="tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo ''"
alias savepass='echo "$1: $(genpass)" >> .secret'
alias getlastpass='tail .secret -n 1
the intention of this alias is to generate a password and later being able to retrieve the last one. I'm storing the passwords in a file called .secret with the following permissions,
-rw------- 1 root root 92 Sep 17 12:48 .secret
so in a way that only root user can read and write the file.
So the problem that I'm facing here is the following one, when I try to run
sudo savepass
is returning me
bash: .secret: Permission denied
Which I assume is because when this alias is not been executed as root, which is the owner of this file.
I don't know how to solve this, so any help is welcome, and any criticism related to this form of storing password is also welcome. My final goal is to be able to store password from the terminal and be able to retrieve it later, in a save way. If you know a better way to do this, just let me know, it will also be a valid answer. Just keep in mind that I want to do this from the terminal without installing any fancy program, just bash script.
If your unprivileged user can alter the file, why do you store it with root permissions? This does not give you any benefit. Store the file with the user id of the user who needs to read and write it and stop using sudo.
The problem in your solution is, that echo is run with root permissions. But the redirection is still done by the shell running the sudo. And that shell does not have root permissions.
If you still want to keep your approach, you have to run tee -a by sudo. For this you have to put the sudo in the alias. But now it might be better to write a function instead of an alias.
savepass () {
echo "$1: $(genpass)" | sudo tee -a .secret
}
Btw: if you want to store your passwords in clear text files, use the netrc syntax, used by other tools, too.
This question already has answers here:
How to fix 'sudo: no tty present and no askpass program specified' error?
(30 answers)
Closed 6 years ago.
I have a shell script which creates a user and executes another script as that user
sudo useradd -m devops
sudo passwd devops
sudo adduser devops sudo
su - devops -c "sh /path/to/myscript.sh"
This script creates the user,sets the password and adds user to sudo group as expected.
myscript.sh contains commands which uses sudo previlages. (sudo apt-get update, sudo apt-get install software-properties-common etc.). And other commands like ssh-keygen,curl and wget.
All commands except the one's with sudo are executed correctly and producing results as excepted.
But commands having sudo fails by giving the error "no tty present and no askpass program specified"
Why does this happen in this case and how can I overcome this?
I have seen similiar questions but will be thankful if I get a clear explanation in this context,thank you.
Try to replace this:
su - devops -c "sh /path/to/myscript.sh"
with this:
sudo -u devops -H sh -c "sh /path/to/myscript.sh"
The -c option of su doesn't support interactive mode:
-c, --command COMMAND Specify a command that will be invoked by
the shell using its -c.
The executed command will have no controlling terminal. This option
cannot be used to execute interractive programs which need a
controlling TTY.
(man su)
By the way, I wouldn't use sudo within a script everywhere. The script might simply require root permissions. Within the script you might drop privileges where necessary by means of the above-mentioned sudo command.
This question already has answers here:
-bash: ./manage.py: Permission denied
(4 answers)
Closed 7 years ago.
I created a bash file mybashfile.sh containing below commands, but running it in the terminal giving me a Permission Denied Error I tried running the file from su but same Permission Denied Error for su.
I am running the command like this -
$: ./mybashfile.sh
It is in a directory containing mybashfile.sh
My mybashfile.sh file -
#!/usr/bin/env bash
redis-server --save "" &
sleep 1
redis-cli flushall
cd ~/Documents/class-prj/class-prj
npm run app.js
Let me know what I am doing wrong here.
Set the mode to executable:
chmod +x mybashfile.sh
This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 7 years ago.
I want to block usb mass storage in some of the systems in the LAN. I have written a bash script which will modify the file. I am running the script as default admin in ubuntu. But the script is not able to modify the file. I am getting the following error
"bash: /etc/modprobe.d/blacklist.conf: Permission denied"
Below is my bash script
#/bin/bash
password='mypassword' #admin password of remote system
val='blacklist usb-storage' #text which i want to add in the file
for sysName in $(cat systemDetails) #systemDetails is file which stores
do
ssh $sysName 'echo '$password' | sudo -S echo '$val ' >> /etc/modprobe.d/blacklist.conf'
echo
done
#script ends
NOTE: I have configured my system such that no ssh password is required.
Any pointers in this regard will be really helpful.
Try this:
ssh $sysName "echo $password | sudo -S sh -c 'echo $val >> /etc/modprobe.d/blacklist.conf'"
This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 8 years ago.
I'm trying to append to the /etc/hosts file from the termimal:
sudo echo -e "127.0.0.1 localhost-myproject" >> /etc/hosts
Even though I'm doing as sudo, it won't let me. I get permission denied:
bash: /etc/hosts: Permission denied
I've looked at a couple other posts and they instruct like this. But I'm getting this error. How can I do this? Thanks
sudo /bin/bash -c 'echo -e "127.0.0.1 localhost-myproject" >> /etc/hosts'