How to work on a project that needs sudo privilege - makefile

I am trying to add image support to slock, a screen lock utility from suckless.org. But slock needs root privilege to run. I didn't have problem with it when I was using it because I just did make && sudo make install. But now that I constantly need to edit, build and check, I can't figure out how to work on this project.
My workflow has been like
make
sudo chown root:root ./slock
sudo chmod u+s ./slock
./slock
I don't know how else to go about doing this. Any help?

You can add a test recipe to your Makefile
test: ./slock
sudo chown root:root $<
sudo chmod u+s $<
$<
You might even want to make it the default target which is executed when you call make with no target arguments. Your workflow would simply become
make
make
make
...
until you are satisfied with the result

Related

Commands without sudo in bash do not work

I am running a bash script and these commands in the script will not work without sudo in front of them. The script.sh is located in a folder such as /jobs/script.sh
Example of commands I am trying to run in the script.sh -
mv /var/app/myapp /var/app/myapp.old
rm file.tar.gz
tar -xzf /home/ubuntu/file.tar.gz -C /var/app/
All the above work if I add sudo in front of them.
I am trying to figure out what permissions are required for them to work without adding sudo in the script.
I have tried giving the script.sh rwx permissions and changing owner to root.
I'm learning permissions in linux, so I'm new to this. Basically what permission should the script.sh have so that I dont have to use sudo in the bash file? Any insight would greatly help.
When you run sudo <some command>, then <some command> is run by the root user (Super user do). The reason you might need to run any command using sudo is because the permissions on the files that command reads/writes/executes are such that only the "Super user" (root) has that permission.
When executing the command mv fileA fileB, the executing user would need:
Write permission to fileB if fileB already existed
Write permission to the directory containing fileB
From what you said it’s most likely you want read and write permissions you can achieve this with chmod
Chmod +[permission] filename
(+ is used to add permission you can also use - instead to remove it)
Where permissions can be:
r —> read
w—> write
x —>excecute
... and more
FOR EXAMPLE: it seems you write permissions for the first file so :
chmod +w /var/app/myapp
Will fix problem

Meteor will not run without Sudo?

On OSX Yosemite and the latest version of meteor (1.0.1), no matter how many times I uninstall and reinstall it, I can't seem to get it running without sudo. My user account is an administrator account. But meteor refuses to run without sudo. The errors I'm getting are all:
-bash: meteor: command not found
I've seen a few posts on here with similar problems. I've tried repairing disk permissions with disk utility. I've tried:
sudo chown -R $myUsername /usr/local/bin/meteor
I'm not sure what else I can do, because it seems to be a permissions issue. Does anyone have any suggestions?
Additional info that might help:
$ sudo which meteor
/usr/local/bin/meteor
$ sudo ls -l /usr/local/bin/meteor
-rwxrwxrwx 1 root wheel 3528 Dec 18 23:14 /usr/local/bin/meteor
$ ls -ld /usr/local/bin
drwx------ 6 502 wheel 204 Dec 18 23:14 /usr/local/bin
By the way, ls -l /usr/local/bin/meteor only works with sudo.
After we clarified the permissions of the meteor executable and its base directory,
the problem became quite clear:
The Meteor binary is located in /usr/local/bin/meteor
Your user didn't have permission to the directory /usr/local/bin
The steps to resolve:
Add permission on the base directory: sudo chmod +rx /usr/local/bin
If necessary, add the base directory to PATH: PATH=$PATH:/usr/local/bin
For future reference:
When you get this kind of error: -bash: XYZ: command not found
The first thing to check is find the absolute path of XYZ, for example /path/to/XYZ
Try to run with the absolute path /path/to/XYZ
If running with /path/to/XYZ gives -bash: /path/to/XYZ: Permission denied that means you have a problem with permissions on the file and/or directories:
You need read and exec permission on the file itself: sudo chmod +rx /path/to/XYZ
You need exec permission on all path elements leading up to the file: sudo chmod +x /path /path/to
After fixing permission issues, running with /path/to/XYZ should work
After fixing permission issues, if running with XYZ (without full path) still doesn't work, that means /path/to is not on your PATH. Fix with PATH=$PATH:/path/to
Note: the above sudo chmod commands give permissions (read and exec) to all users: owner + group + other. In the case of the OP (and in most common cases), this is perfectly fine.
In situations with more sophisticated permission setup, you might need to be more specific, and use g+rx instead of +rx.
(for the record)
If it works with sudo, and without sudo you get command not found, that means that meteor is on the PATH for root but not for your user. To make it work for your user, you need to find the path to meteor and add it to your user's PATH. For example:
Become root with sudo su -
Find the path of meteor, run command: which meteor
Logout from root (Control-D) to return to your user
Add the base directory to PATH, for example if earlier which meteor gave you /usr/local/bin/meteor, then do this: PATH=$PATH:/usr/local/bin
After this, it should work with your user. To make it "permanent", add the last step in your ~/.bashrc.
If this still doesn't work, then perhaps your user doesn't have the execute permission on the file. Fix that with this command:
sudo chmod +x /usr/local/bin/meteor
From your comments it also seems your user doesn't have permission on the /usr/local/bin directory itself. Fix that with this command:
sudo chmod +rx /usr/local/bin
Shouldn't need an admin account to run it, standard user account works fine. You can locate the meteor file by typing which meteor. It will tell you what file is being used to execute.
Try removing the .meteor folder in your home directory, something like rm -rf ~/.meteor and the script from the bin folder rm /usr/local/bin/meteor or rm 'which meteor' (speech marks there are the ones above ~)
And then reinstall meteor without sudo using the curl https://install.meteor.com/ | sh command.
Should hopefully install with all the correct permissions...

Default user for files and directories created in bash under sudo

I'm writing a bash script that creates directories and copy files under Mac OSX. Some of these directories and files need to be placed in folders owned by the system such as /Library/Audio/Plug-Ins, and so I run the script under sudo. Such script might look like:
copy-plugins.sh:
#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins
and called:
$ sudo ./copy-plugins.sh
However when running under sudo, all created directories and copied files are owned by root.
I would like to be able to run the script under sudo and have the files be owned by my user.
I could call chown after each file/directory is created or copied
copy-plugins-cumbersome.sh:
#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins
cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins/plugin-A.dylib
cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins/plugin-B.dylib
but I'm hoping for a more general solution.
As far as I can tell there is no setuid for bash.
Use cp -p option to preserve file attributes.
Note this will preserve user, group permissions and the modification and access times of the files.
As you need sudo to copy to the directories you are copying to in script, it means you need to be root to copy anything in those directories.
When you do sudo you are root for that particular command or script, so whatever will be created or executed will have root permissions. Till the time you specify.
The possible ways to come out of it without changing anything:
The one you are using, and
Other one to use -p or -a with cp
rsync -go <source file> <destination file>
-g for preserving group and
-o for preserving ownership.
Note If you do a chown out of script, you will have to specifically do sudo chown since files you would be touching belong to root.

Bash script says command not found

I try to run the following bash script to create a bunch of users, groups, home dirs for the users and correct permissions for all of these. The OS is CentOS.
When I try to run the following, which I though should work, it returns "command not found" when running via terminal. it only gets as far as creating the /homedirs directory, nothing more. I'm a total noob at bash scripting so forgive me if this looks ugly.
mkdir /homedirs; chmod 775 /homedirs;
for iYear in {1..3} do
sYear = $iYear"ti"
sYearDir = "/homerirs/"$sYear
groupadd $sYear; mkdir $sYearDir; chgrp $sYear $sYearDir; chmod 750 $sYearDir
for sClass in {a,b} do
sClassDir = $sYearDir/$sClass
mkdir $sClassDir
sClassGrp = $sYear$sClass
groupadd $sClassGrp; chgrp $sClassGrp $sClassDir; chmod 750 $sClassDir
for iUser in {1..3} do
sUserName = "i"$iYear$sClass"g"$iUser
sUserDir = $sClassDir/$sUserName
useradd -d $sUserDir -g $sClassGrp -G $sYear -m $sUserName
chown $sUserName $sUserDir; chmod 750 $sUserDir
done
done
done
The error message is caused by the spaces around the equals signs. A token with whitespace after it is interpreted as a command name; so what you intended as variable names causes the Command not found errors.
You may need to set your PATH and you really should read the advanced bash scripting guide. See also this answer.
I also suggest to debug your script by starting it with #!/bin/bash -vx as its first line. And you should make it executable with chmod u+x at least.
Perhaps groupadd might not be available on your system.
best thing to do is add the full path before your executables:
change useradd to /usr/sbin/useradd
change groupadd to /usr/sbin/groupadd
will cure the command not found.
remember this programs will probably need to run as root to work.

Spotlight won't turn on: Console says: mds[pid] (Fatal) Server: mds must be run as root. exiting

I turned off spotlight and was very thorough about it. So thorough that I can't get it turned back on. I need to install a program from the Mac App Store (& App Store relies on mds), so if anyone has information on how to install an App Store program without mds at all (spotlight), that would be even better.
So my problem is that spotlight is complaining it can only run as root, but I can't for the life of me figure out why it isn't. From what I understand, launch daemons in /System/Library/LaunchDaemons are automatically launched as root as long as they have the correct permissions. Every file that directory is rw-r-r. Here are the commands I've been using to unload/load spotlight from launchd:
sudo mdutil -a -E -i off
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist
sudo rm -rf "/.Spotlight-V100"
sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search
I also rename the mds executables in /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support, adding a .bak extension.
To reload it, I essentially undo those steps. However it's not working right now. I've searched for the error on google, but no dice. I reloaded the mds binaries from the Snow Leopard install image, didn't do anything. I've repaired permissions 4-5 times and manually looked up the tree to see if I noticed any permissions were wrong, but everything is root:wheel 755 like I assume it should be in the Frameworks folder, and rw-r-r in the LaunchDaemons folder. However mds is constantly spitting messages to the console that say
mds[pid] (Fatal) Server: mds must be run as root. exiting.
Anyone have any clue what is going on? I'd rather not reformat right now since I've put quite a long time into setting up this computer and I just want to install one program from the App Store.
Thanks!
Well, wouldn't you know as soon as I break down and post on SO I find the solution: I found someone's spotlight-enable script and pasted it into my terminal. Rebooted, spotlight working, installed program, disabled spotlight again. This is what I used:
sudo chmod 775 /Library/Spotlight;
sudo chmod 755 /System/Library/Spotlight;
sudo chmod 755 /System/Library/PreferencePanes/Spotlight.prefPane;
sudo chmod 755 /System/Library/Services/Spotlight.service;
sudo chmod 755 /System/Library/CoreServices/Spotlight.app/Contents/MacOS/Spotlight;
sudo chmod 555 /usr/bin/mdimport;
sudo chmod 555 /usr/bin/mdcheckschema;
sudo chmod 555 /usr/bin/mdfind;
sudo chmod 555 /usr/bin/mdls;
sudo chmod 555 /usr/bin/mdutil;
sudo chmod 555 /usr/bin/md;
As Thilo pointed out, this isn't really programming related so it can be moved to Super User or whatever else anyone things is better.

Resources