crontab doesn't work after /etc/crontab is configured [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I added this line to the end of /etc/crontab file:
* * * * * cp /etc /backups -R > /dev/null && tar -Jcf /backups/etc-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.tar.xz /backups/etc > /dev/null && rm -rf /backups/etc > /dev/null
and then I restarted the crond service with systemctl restart crond command
but didn't work
It worked when I run this command cp /etc /backups -R > /dev/n...... in terminal
And my mailx for root is empty.
Could anyone tell me whats wrong with my configuration?
Thank you very much!!!

/etc/crontab is not an ordinary crontab file; it's a system crontab file. Each line has the usual 5 fields specifying the schedule, then a 6th field specifying the account. With the line you show in /etc/crontab, it will attempt to run the command as user cp.
If you're using the Vixie cron implementation (you probably are), run man 5 crontab and search for "EXAMPLE SYSTEM CRON FILE".
I recommend not touching /etc/crontab. Rather use the crontab command to create a user crontab for whichever account you want. Run crontab as root if the command begin run needs root access.

I think now its fine
[root#localhost public]# crontab -e
crontab: installing new crontab
[root#localhost public]# systemctl restart crond
[root#localhost public]# crontab -l
* * * * * cp /etc /backups -R > /dev/null && tar -Jcf /backups/etc-`date +\%Y-\%m-\%d- \%H-\%M-\%S`.tar.xz /backups/etc > /dev/null && rm -rf /backups/etc > /dev/null
[root#localhost public]#

Related

Responding to a sudo password request in a script? [duplicate]

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 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

Linux, BASH launch command with special char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I must implement a deploy script that launches command by special char from specific user.
The command that I must launch is:
cd /path
. ./setantenv.sh
I am trying to launch the command with this syntax:
su - USER -s /bin/bash -c 'cd /PATH/ && . ./setantenv.sh'
su - USER -s /bin/bash -c 'cd /PATH/ && ant clean all'
But I've got a problem with . ./setantenv.sh
In order to configure the environment for your subsequent ant command, you have to include the ". ./setantenv.sh" inside your second call. Both calls result in independent bash processes that dont share their specific environment.
try this:
su - USER -s /bin/bash -c 'cd /PATH/ && . ./setantenv.sh && ant clean all'

sudo sorry, you must have a tty to run sudo [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I have written a bash script that goes to each machine and runs set of command. I am using the user iis that has sudo privlidges on that machine. However, when i run the sudo yum command i get sudo: sorry, you must have a tty to run sudo.
Not sure what is wrong ? I am using the -t command to force but it would still not work
bash script
#!/bin/bash
INPUT=ccidetails.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read privateip password
do
echo $privateip
scp /home/Data/Test.c iis#$privateip:/tmp
sshpass -p$password </dev/null ssh -t -o "StrictHostKeyChecking no" iis#$privateip "
hostname
cd /tmp
gcc Test.c -o TEST
./TEST
sudo yum -y update glibc
gcc Test.c -o TEST
./TEST
exit
" >> output.txt
done < $INPUT
IFS=$OLDIFS
Error:
sudo: sorry, you must have a tty to run sudo
On remote host comment the line below on /etc/sudoers:
grep tty /etc/sudoers
#Defaults requiretty
That will allow you to continue. Make sure you understand the consequences of doing so:
man sudoers | grep -i requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.
If you don't want disable requiretty globally you can disable it for a specific user:
example:
Defaults requiretty
Defaults:your_username_goes_here !requiretty
While this isn't a question for SO, you're almost there ... what you need to change is the number of -t above ... try this:
sshpass -p$password </dev/null ssh -ttt -o "StrictHostKeyChecking no" iis#$privateip "

How to block sudo commands based on arguments [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I have a file with root permissions like this
[root#testbox ~]# ls -l /etc/resolv.conf
-rw-r--r-- 1 root root 113 Feb 21 21:29 /etc/resolv.conf
I have enabled passwordless sudo for my user using /etc/sudoer
%mayur ALL=(ALL) NOPASSWD: ALL
I want some way where if I try to edit this particular file I get blocked.
for eg .. I want to block these commands based on THE FILE NAME
$ echo 123 | sudo tee /etc/resolv.conf ## SHOULD GET BLOCKED
$ touch newfile | echo 123 > newfile | sudo cp newfile /etc/resolv.conf ## SHOULD GET BLOCKED
My Efforts:
%mayur ALL=(ALL) NOPASSWD: ALL,!/* /etc/resolv.conf
This did not help at all.
I also checked sudoers man page but it
seems regex support isnt that great.
Any things that works will help. Thanks.
EDIT: I want to be able to apply the solutions accross multiple servers with multiple users having sudo access
You can use a wrapper:
#!/bin/bash
declare -A EXCLUDE
while IFS= read -r FILE; do
EXCLUDE[$FILE]=.
done < /etc/sudoers.exclude-list
for ARG in "$#"; do
TARGET=$(exec /usr/bin/readlink -m -- "$ARG")
[[ -n $TARGET && -n ${EXCLUDE[$FILE]} ]] && {
echo "sudo: Sorry, target is not allowed: $TARGET"
exit 1
}
done
exec /path/to/real/sudo-in-secured-location "$#"
Where /etc/sudoers.exclude-list is a line-based list of absolute real file paths. Entries must not contain extra spaces and list must not be in DOS format.
This script must have same ownership and permissions as the real sudo.

Use sudo with password as parameter [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 10 years ago.
Improve this question
I would like to run sudo with my password as parameter so that I can use it for a script. I tried
sudo -S mypassword execute_command
but without any success. Any suggestions?
The -S switch makes sudo read the password from STDIN. This means you can do
echo mypassword | sudo -S command
to pass the password to sudo
However, the suggestions by others that do not involve passing the password as part of a command such as checking if the user is root are probably much better ideas for security reasons
You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):
sudo chmod +s myscript
echo -e "YOURPASSWORD\n" | sudo -S yourcommand
One option is to use the -A flag to sudo. This runs a program to ask for the password. Rather than ask, you could have a script that just spits out the password so the program can continue.
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

Resources