Exec script as root in autostart - linux

I need to launch a log on script. This log on script contain several commands like : *cp, ln, and chown.*
But "chown" need to be root to be efficient. So my script is doing well, except the "chown" command.
My environnement :
Debian Jessie, LXDE.
My script (in /usr/local/bin/transfertPOL.sh) :
#!/bin/bash
#transfert de POL au démarrage de la session
groupe=$(id -gn $1)
uid=$(id -u $1)
POL=/var/POL
home=$(eval echo ~$1)
lien=$home/.PlayOnLinux
#changement de propriétaire du dossier
sudo chown -h -R $1:$groupe $POL
ln -s $POL $lien
sudo chown -h -R $1:$groupe $lien
#Copie des raccourcis sur le Bureau
cp --preserve=ownership $POL/shorcuts/*.desktop $home/Desktop
My Desktop Entry in /etc/xdg/autostart/transfertPOL.desktop :
[Desktop Entry]
Type=Application
Exec=sh /usr/local/bin/transfertPOL.sh
The Exec, I tried several things :
Exec=sh /usr/local/bin/transfertPOL.sh #Work, but the chown don't
Exec=/bin/bash/ -c "sudo /usr/local/bin/transfertPOL.sh" #doesn't work
Exec=sudo sh /usr/local/bin/transfertPOL.sh #Work, but the chown don't
For this one, I've created a /etc/sudoers.d/transfert with
%users ALL=NOPASSWD: /usr/local/bin/transfertPOL.sh
The script is a symbolic link to PlayOnLinux local folder. Each users need to access to this specific POL folder. So when a user is logging, the chown is changing folder rights.
Do you have any idea to help me ?
Thanks

You are calling sh in the Exec command:
Exec=sudo sh /usr/local/bin/transfertPOL.sh
So the sudoers entry:
%users ALL=NOPASSWD: /usr/local/bin/transfertPOL.sh
wont run with root privileges.
You probably want to make your script executable:
sudo chmod +x /usr/local/bin/transfertPOL.sh
And change the Exec command to:
Exec=sudo /usr/local/bin/transfertPOL.sh
BUT: We got groups for that in Linux, try setting the correct group permissions on the files you want to read/write. You wont need that script at all then.

Related

How to change directory permissions like made with 'sudo mkdir' in linux

I created the directory with "mkdir" command, after that I need to change permissions as if I made it with "sudo mkdir".
I've searched for the chmod command, but it doesn't seem to be what I'm looking for.
Is it possible to do this with a single command in terminal?
you can use the chown command to change the owner and group of the directory, and the chmod command to set the permissions.
sudo chown root:root /path/to/directory && sudo chmod 755 /path/to/directory

My script file stopped executing when there change in user in script what should i do

My code file looks as follows
Cd testR
Mkdir bin
chmod -R 755 bin
Sudo su - inst1
cp inst1/installable/files/testR.p
...
...
So after sudo su the execution get paused please let me know what should i do
If there is no problem that your commands are executing as root, you can execute your script like this:
sudo ./yourscript
So, you don't have to switch within your shell script.

Remove ".bash_aliases" with bash script

In my .bashrc I'm using .sh script for easily configuring newly installed Debian. But while trying to
rm -f ~/.bash_aliases
wget https://raw.githubusercontent.com/.../.bash_aliases
rm -f ~/.bashrc
wget https://raw.githubusercontent.com/.../.bashrc
it's just omitting those line?
File is with permission chmod +x ./script.sh and run by sudo ./script.sh
What could possibly be wrong?
(In final code there is full link, files are being downloaded as .bashrc.1 and .bash_aliases.1)
Don't use sudo unless you have a good reason.
When you run sudo ./script.sh it runs as root, so ~ refers to root's home directory /root instead of your user's home directory.
Just run ./script.sh instead, so that it runs as you and modifies your own home directory.

How to grant Nagios permissions to run some commands in custom script?

I have been making some custom shell scripts for my nagios machine. I was able to make them run just fine but for some reason some commands in the script don't seem to be working.
For instance commands like echo, cut , ps , grep work fine but commands like touch, useradd dont seem to work, even with sudo. If I run the script from the terminal, all the commands in the script work.
How can I give nagios permissions to run these commands?
I'm running nagios3 on ubuntu 14.04.5 lts
Edit: Added a few lines of code which aren't being run
sudo useradd -m $USERNAME
(echo $PASSWORD; echo $PASSWORD) | sudo smbpasswd -s -a $USERNAME
Standard way is setup permission for Nagios user on monitored server, for instance NRPE, in /etc/sudoers file.
1. method
Try add something like this in your sudoers file.
Defaults:nrpe !requiretty
nrpe ALL= NOPASSWD: useradd -m
nrpe ALL= NOPASSWD: smbpasswd -s -a
PS: For easy editing sudoers file you can use visudo command ;-)
2. method
Or you can try add Nagios user to sudo group via sudo usermod -aG sudo <username>
-a stands for add
G is for group
Tell nagios to run the script as sudo in your .cfg file...
Assuming its permissions problem.
Edit /etc/sudoers file using visudo, this allows automatic file check for errors.
Defaults:nrpe !requiretty
nrpe ALL=(root) NOPASSWD: /path/to/your/command/or/script
Verify sudo has assigned the above permissions to the user in this case nrpe
sudo -U nrpe -l
you should see the command you added listed within the outpul
Edit /etc/nagios/nrpe.cfg
Add your command to the end of the file
e.g.
command[your_command]=/usr/bin/sudo /path/to/your/command/or/script
Restart nrpe
Centos: systemctl restart nrpe (use the command available based on your Operating system)

Adding FTP user via bash script issue

I have a .sh file (lets say adduser.sh) that is executed via a cronjob that contains the commands to create an FTP user.
The adduser.sh file looks like so...
#!/bin/bash
mkdir /var/www/vhosts/domain/path;
useradd -d /var/www/vhosts/domain/path -ou <uid> -g <group> -s /bin/false <username>;
echo <password> | passwd <username> --stdin;
Now here is my problem. If I run it directly through SSH using...
sh adduser.sh
...no problems and it works as intended.
But if I let the cronjob run it the directory is created but the user is not added.
What gives?
As it stands, there is an alternative to useradd known as adduser. In Debian or Ubuntu, adduser is a perl script and performs sequential functions like create the user using adduser, assign it to a group, create home directory etc.
As per adduser man page-
adduser and addgroup are friendlier front ends to the low level tools
like useradd, groupadd and usermod programs, by default choosing
Debian policy conformant UID and GID values, creating a home directory
with skeletal configuration, running a custom script, and other
features.
In Fedora, RedHat, and CentOS, adduser is just a symbolic link to useradd.
[root#hobbit ~]# which /usr/sbin/adduser
lrwxrwxrwx 1 root root 7 2012-09-20 20:20 /usr/sbin/adduser -> useradd
If you are on any on the above OS then you can try adduser redirect 2> to a add_user.log file and check the file to see if something goes wrong.
I have resolved this simply adding /usr/bin/ to the useradd function.
#!/bin/bash
mkdir /var/www/vhosts/domain/path;
/usr/bin/useradd -d /var/www/vhosts/domain/path -ou <uid> -g <group> -s /bin/false <username>;
echo <password> | passwd <username> --stdin;
Thanks everyone for helping me get on the right track. Hope this helps someone out there.

Resources