Why does sudo chmod <file> inside my shell script not work? The permissions stay as they were.
#!/bin/bash
sudo chmod 757 Folder
Remove the sudo from the file. Just write
chmod 757 folder
And give your script file +x permission. It will work.
In sudo visudo add this line to disable the request password when you write the sudo command:
www-data ALL=(ALL:ALL) NOPASSWD:ALL
Launch the script with the exec function and put sudo inside this with user root only. Now there's no need to enter a password.
exec("sudo -u root -S ./create_folders.sh)
Related
I am using https://laravel.com/docs/5.4/envoy as a deployment tool.
In the Envoy.blade.php, I have command that requires sudo access for example:-
chmod 777 -R storage/
chmod 777 -R bootstrap/cache
These commands fails with an error saying operation not permitted. How can we resolve this?
To run those commands as sudo try the following:
echo "{{ $password }}" | sudo -S chmod 777 -R storage/
echo "{{ $password }}" | sudo -S chmod 777 -R bootstrap/cache
Obviously you'll need to pass the sudo password into the envoy run command.
envoy run mytask --password=mypass
Tested on Ubuntu server 16.04 & 17.04
I changed the following configuration on the server
sudo visudo
and add
username ALL=(ALL) NOPASSWD: ALL
All commands can now be executed without entering a password
You can also specify individual commands
username ALL=(username) NOPASSWD:/etc/init.d/apache2 reload
I am trying to chown a directory via an NPM script. The script looks like the following:
chown -R 755 www-data root#XXX.XXX.XXX.XX:/var/www/test.com
But the message I get back is: chown: www-data: No such file or directory even though this exists. Any ideas much appreciated.
chown operates locally, not on remote servers. In your example, chown is attempting to operate on ./www-data and ./root#XXX.XXX.XXX.XX:/var/www/test.com, which don't exist in the directory of wherever you were when you executed the command.
You will need to execute chown as a command through ssh:
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
Fixed this with following script.
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
(I needed to login to the server first).
I used the following command in my script
sudo su - user -c bash <<EOF
cp /home/test.txt /opt/
EOF
If I use the sudo su - user on terminal, Unix don't ask me the Password but if I try to run the script the terminal ask me the Password and if I delete the EOF part the rest of code run when I quit the session.
I want to run the command in user mode sudo but the terminal don't Need ask me the Password.
If I use
sudo su - user <<EOF
code
EOF
I have an error in .bash_profile: too many argument
I want to run the command in user mode sudo but the terminal don't
Need ask me the Password.
The scenario you are experiencing is caused by the users cached credentials for sudo, which allow sudo to maintain a session and any further sudo command will not prompt for passwords.
Check this:
Open a new terminal and run sudo whatever, then close it and open another new terminal and run sudo whatever, you will see that sudo asks for password every time...
If you still need to do that, then you have the following options:
Prevent sudo to ask for password permanently:
run sudo visudo and look for the line root ALL=(ALL) ALL, then add a line
username ALL=(ALL) NOPASSWD: ALL
then save and exit.
Note: This is a security risk
Or Prevent sudo to ask for password permanently only for specific script:
run sudo visudo and look for the line root ALL=(ALL) ALL, then add a line
username ALL=NOPASSWD: path_to_the_script
then save and exit
Provide password inside the script, by running your sudo command like this:
sudo -S <<< "password" command
Note: This is a security risk too.
I guess you need to execute your command as a different user. This might be your answer: Run a shell script as another user that has no password
sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"'
It is a quote from the link. Probably the following is better for you:
sudo -H -u otheruser bash <<EOF
cp /home/test.txt /opt/
EOF
UPDATE: You may wish to create a specific sudo rule to run a specific command without password (inside /etc/sudoers file -remember to use visudo to edit it-):
otheruser ALL=NOPASSWD: /full/path/to/your_command.sh
(of course you need root access to edit sudoers, I hope you can do it).
And create the script called your_command.sh that contains your logic. You'll then be allowed to run it without password:
sudo -H -u otheruser your_command.sh
I know, it's not a "single line command" but it is safe as it allows only one specific command without password. And it doesn't require a password, of course!
Then don't use sudo, then it won't ask for password, but you will to be have logged in as root!
So I made a tiny script to change my mac address but it requires me to enter my password to make the switch... what can I add to have it enter it for me?
sudo ifconfig en1 ether 00:11:22:33:44:55
you may add your ifconfig command for your id in your /etc/sudoers sudo config file
EDIT: I've realized by now that you can't actually do this unless you wrap these commands in a binary. For example, using the system() function from a C program. I guess that setuid shell scripts are such a bad idea that linux doesn't use them at all :).
While it can be dangerous in some circumstances, you can make your script setuid. To do this, put that command into a file like so:
#!/bin/bash
ifconfig en1 ether 00:11:22:33:44:55
Then set the permissions so that it's owned by root but only you can touch the file:
$sudo chown root file.sh
$sudo chgrp username file.sh
$sudo chmod 070 file.sh
Then make it suid:
$sudo chmod +s file.sh
Now you should be able to execute this file as your user, but have it run with root privileges.
You don't want to put your root password in an accessible file.
Better would be to make just that command sudoable without password.
Try entering in /etc/sudoers
<your username> ALL=(ALL) NOPASSWD: ifconfig
This says that your user shouldn't need to enter a password for sudo as long as the command is ifconfig.
You should probably read about how the sudoers file works in general first though. Don't know what is you're on, but here's docs for Ubuntu:
https://help.ubuntu.com/community/Sudoers
Here are docs for OSX http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/sudoers.5.html
I have some files to upload. Usually to edit anything while logged in the server I must precede the command with sudo. That is known.
How do I send a file then as "admin" instead of "root" when I have disabled root login.
scp path\to\file admin#myaddress.com:/var/www/sitename/public/path/
PERMISSION DENIED
In my opinion, either you should give permissions to the admin user or scp your file to /tmp/ and then sudo mv /tmp/yourfile /var/www/sitename/public/path/.
There is no sudo option when we are using scp command from local to server.
Each user will have upload permission to its own folder in home directory ex. home/xxxxuser so use as below:
scp file_source_here xxxuser#yourserver:/home/xxxuser/
Now you can move file from this folder to your destination.
I suggest these two commands as it works in a bash script.
Move the file to tmp as suggested.
scp path\to\file admin#myaddress.com:/tmp
Assuming admin user can do sudo. The ssh option -t allow you to do sudo command.
ssh -t admin#myaddress.com 'sudo chown root:root /tmp/file && sudo mv /tmp/file /var/www/sitename/public/path/'