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 6 years ago.
Following this tutorial
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04
and trying to use the command
echo 'prefix=/usr/local' > node/etc/npmrc
however I get a permission denied error, even when using sudo.
Any ideas?
echo 'prefix=/usr/local' > node/etc/npmrc
however I get a permission denied error, even when using sudo.
You haven't shown us the failing command using sudo. Please update your question and show us the exact command that failed, along with the exact error message.
Meanwhile, I can guess that the failing command was:
sudo echo 'prefix=/usr/local' > node/etc/npmrc
That runs the echo command with root privileges (which is not particularly useful, since you can runecho as an ordinary user). The redirection is handled by your current shell process, and is subject to the permissions of the current user.
Since > is handled by the shell, you need a shell running as root to handle it:
sudo sh -c "echo 'prefix=/usr/local' > node/etc/npmrc"
Related
I have the following bash script to restart the network manager in Debian. The script works as is it should, but not as I would like it to. When the script asks for the sudo password I am able to pass it along using echo, but it displays the password in terminal while the script executes, making it less asthetically pleasing than I would like. Is there anyway to have the script enter the password, but not display the password text while the script calls for the sudo password?
I have tried as many suggestions on Stack Overflow as i could find, well as Stack Exchange before submitting this question.
Script is as follows:
#!/bin/bash
clear
echo "Restarting service Network Manager"
echo""
sleep 1
echo -e "\033[0;31m......................................\033[0m"
echo -e "\033[0;31m......................................\033[0m"
sleep 1
echo""
sudo service network-manager restart
sleep 2
echo <Password>
sleep 2
echo "Service Network Manager Restarted"
sleep 1
echo ""
echo "Relinquishing control of terminal to user..."
sleep 7
clear
Remove the echo <Password> line? I am pretty sure it does nothing other than display the password, as sudo apparently (through an appropriate entry in /etc/sudoers) works without you having to give a password. (What you write to terminal with echo does not get passed to any other process.)
Generally speaking, you can use sudo -S to make sudo expect the password on stdin. But also generally speaking, if you have to hardcode a password in a script, you're doing it wrong in some way.
Is there anyway to have the script enter the password
Putting password in script is not a good idea. First, from security point of view, password may be recovered from script from anyone with access to script. Second, from maintenance view, once you change your password, scripts suddenly stop working and you have to update them all.
Fortunately, as you are already using sudo there is better solution. You can configure sudo to allow running certain command without password, by using NOPASSWD rule in /etc/sudoers.
myuser ALL=(ALL) NOPASSWD: service network-manager restart
See:
How do I run specific sudo commands without a password?
How to run a specific program as root without a password prompt?
Warning: Always edit /etc/sudoers with visudo, never directly. It prevents you from breaking /etc/sudoers. Once you break your /etc/sudoers, you won't be able to use sudo, including using sudo to fix /etc/sudoers.
try this /bin/echo -e "password\n" | sudo apt-get update
or see this Use sudo with password as parameter
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 5 years ago.
I wish to write text from stdin to a file that is owned by root.
I'm doing this programmatically across an SSH connection - essentially from a script - so using a text editor is out of the question. There is no terminal. The process is entirely automated.
Given that [a] root elevation can be obtained via sudo, and [b] files can be written to using cat with redirection, I assumed the following would work:
ssh user#host sudo cat >the-file
Unfortunately, the redirection is applied to sudo, not to cat. How can I apply redirection to cat in this example?
The normal pattern to do this is:
ssh user#host 'cat | sudo tee the-file'
tee redirects output to a file and can be run with sudo.
If you want to mimic >> where the file is appended-to, use sudo tee -a.
You'll also need to be sure to quote your command as above, so that the pipe isn't interpreted by the local shell.
Edit
The tee command is part of POSIX, so you can rely on it existing.
To redirect Standard Error as well:
ssh user#host 'some_command 2>&1 | sudo tee output-file'
This question already has answers here:
Stop being root in the middle of a script that was run with sudo
(2 answers)
Closed 7 years ago.
I developed a script.
This script should called with sudo:
$ sudo ./script
In my script I have a command in the middle which I want to execute it as a non super user (like if it's executed without sudo)
Is it posssible to do that?
Technical it's possible with sudo -u plain_user command in your script. This way of scripting is somehow strange to me though.
I am using php to execute a script on my server.
PHP: exec("screen -dmS testingscreen /home/username/otherfolder/test.sh")
For a while I was unable to do this till I did some digging and found a few things about how to do this. I also found some items about other things relating to screen / minor issues I was having at the same time.
I will show each issue I had in detail up to the last issue which I can not seem to figure out a fix.
Issue one (Resolved): Trying to run script outside of webroot.
Topic: Excute script outside of web root with PHP.
Solution: Used sudo chown www-data:www-data filename.sh giving my web user access to the script.
Issue two (Resolved): Script does nothing when php file invokes it.
Solution: Make script executable with chmod +x filename.sh
Notes: It was painfully obvious what I did wrong when I used ls in my script dir.
Issue three (Resolved): Unable to su to www-data to check if screens were running.
Error: This account is currently not available.
Problem: www-data was set to /bin/nologin in /etc/passwd
Solution: chsh -s /bin/bash www-data to make it so I can switch to it as user.
Issue four (Resolved): While in su www-data could not use screen -r testingscreen
Error: Cannot open your terminal '/dev/pts/0' - please check.
Problem: Permissions, not opening screen under login account and trying to use su account.
Solution: Using script /dev/null in terminal before trying screen -r testingscreen.
Issue five (Not Resolved):
While executing screen -dmS testingscreen /home/username/anotherfolder/filename.sh
With php's exec() does make screen testingscreen
I have a line in the script file that says
screen -dmS scriptscreen mono '/home/username/otherfolder/myprogram.exe' 'programargs1' 'programargs2' 'args3' 'args4'
This screen fails to run when executed with the script "filename.sh"
However executing this same line as the www-data user (Using sudo su www-data to be that user.) works fine.
Also of note: Running screen -dmS testingscreen /home/username/otherfolder/test.sh works absolutely fine and starts up like it should including scriptscreen. This works while using sudo su www-data but not if I start the script with php.
On the more bizarre side of this, using screen -dmS testingtop top works fine when inside the script file that is started by php. Am I missing something obvious here?
I have checked file permissions and everything is fine, it should work. Being able to run my program as www-data means that the perms are set up fine. It just seems like it's something to do with php or my server in general. Any help or possible tips are very welcome and I hope my detailed issue+solution write up may help others in the future all in one post.
Since you are envoking sudo as part of your question i assume this is an attempt at getting PHP to execute a script with root permissions?
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if I misunderstood your question and the script does not require
//root permissions you can change the
//second argument on getShell() to false. That will return a bash shell
//with permissions from www-data.
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('/home/username/otherfolder/test.sh');
This question already has answers here:
Use sudo to change file in root directory [duplicate]
(2 answers)
Closed 6 months ago.
VLC is running. Got the PID from pgrep vlc.
I want now to pause it manually since I would like it to run "submerged" (right now from another tty but probably as a daemon)
I tried to simply do sudo "pause" > /usr/bin/vlc/ having got the path by doing sudo ls -l /proc/<PID>/exe.
The answer is, even running the sudo command, that the permission is denied.
For my surprise, if I enter root mode sudo bash and just type the same command, the answer is not that the permission is denied, but rather that the "text file is busy". I'd like to guess what text file. I thought that command (in that case) inputted data to the command input manually (apart from writing to a text file)
This is probably what you want to do.
Write to /proc/pid of the program/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).
Example
Terminal 1:
[ciupicri#hermes ~]$ cat
Xxx
Terminal 2:
[ciupicri#hermes ~]$ pidof cat
7417
[ciupicri#hermes ~]$ echo xxx > /proc/7417/fd/0
Taken from another stack overflow answer