How to append to /etc/hosts file from the termimal? [duplicate] - linux

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'

Related

Modify protected file with bash alias [duplicate]

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.

sudo jq query permission denied [duplicate]

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

bash: ./mybashfile.sh: Permission denied [duplicate]

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

sudo in bash, permission denied [duplicate]

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'"

Command not found when running swapon with SSH [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to use "swapon -s" remotely but getting "command not found"
$ ssh ns2 swapon -s
bash: swapon: command not found
Using it locally works perfectly, what could be the reason for this?
There are several possible reasons:
You're not root at the remote side. Check this with who am i or id. To make sure you're root, use ssh root#ns2 ...
Your path is wrong. This is often a problem with sudo. Try ssh ns2 sudo /sbin/swapon
Try to ran
which swapon
If it returns something like
no swapon in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:...)
it means that swapon script isn't found anywhere
Maybe you're not in the sudoers file or you need to enter a password.
Be sure that you're really root so try try this and look if it differs:
ssh server.tld id
ssh server.tld sudo id
The second one should give you an output which should be this:
uid=0(root) gid=0(root) groups=0(root)
If this is not the output check your /etc/suders which should have an entry like this:
foo ALL=NOPASSWD: ALL
The command I runned and which worked:
ssh -l foo server.tld sudo swapon -s

Resources