Getting Permission Denied running perl executables - perl

I am running OS X and, basically, I wanted to set up a directory where I could drop some Perl executables and use them without having to type
$perl /this/is/the/path/name.pl
every time I wanted to use one. So I modified my .bash_profile to add a filepath for a directory that sits on my desktop that I want to drop executables in. I modified my .bash_profile by adding
PATH=$PATH:/Users/Wes/Desktop/Perl_dir; export PATH
Now, it finds the Perl files I want but denies me permission to them like so
-bash: /Users/Wes/Desktop/Perl_dir/phylo.pl: Permission denied
How can I fix this? Thank you in advance.

Make sure that the perl script itself is set to be executable. You can do this with a command like this:
chmod +x /Users/Wes/Desktop/Perl_dir/phylo.pl
And then make sure the first line of the script has an appropriate "hash-bang" line to invoke perl, something like:
#!/usr/bin/perl -w
With both of those in place, I think your scripts should start working. (Note: The -w isn't strictly necessary, and may cause warnings in your script. I do suggest it, though, for developing new perl code, since it encourages a certain brand of perl hygiene.)

Apart from setting PATH correctly, you also need to:
make sure files are executable
$ chmod +x /this/is/the/path/name.pl
check your "shebang line", i.e. first line of the script must contain
absolute path to perl interpreter on your machine, like this:
#!/usr/bin/perl
# this will work in Linux distros
To find out the path, use which command:
$ which perl
/path/to/perl

you may need to authorize the chmod using sudo, try this:
sudo chmod u+x program_name
./program_name
If not sudo while chmod, you need to run like this:
sudo chmod u+x program_name
sudo ./program_name

Related

Azure Pipelines is it possible to run bash command with sudo? [duplicate]

I am trying to compile some sources using a makefile. In the makefile there is a bunch of commands that need to be ran as sudo.
When I compile the sources from a terminal all goes fine and the make is paused the first time a sudo command is ran waiting for password. Once I type in the password, make resumes and completes.
But I would like to be able to compile the sources in NetBeans. So, I started a project and showed netbeans where to find the sources, but when I compile the project it gives the error:
sudo: no tty present and no askpass program specified
The first time it hits a sudo command.
I have looked up the issue on the internet and all the solutions I found point to one thing: disabling the password for this user. Since the user in question here is root. I do not want to do that.
Is there any other solution?
Granting the user to use that command without prompting for password should resolve the problem. First open a shell console and type:
sudo visudo
Then edit that file to add to the very end:
username ALL = NOPASSWD: /fullpath/to/command, /fullpath/to/othercommand
eg
john ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop
will allow user john to sudo poweroff, start and stop without being prompted for password.
Look at the bottom of the screen for the keystrokes you need to use in visudo - this is not vi by the way - and exit without saving at the first sign of any problem. Health warning: corrupting this file will have serious consequences, edit with care!
Try:
Use NOPASSWD line for all commands, I mean:
jenkins ALL=(ALL) NOPASSWD: ALL
Put the line after all other lines in the sudoers file.
That worked for me (Ubuntu 14.04).
Try:
ssh -t remotehost "sudo <cmd>"
This will remove the above errors.
After all alternatives, I found:
sudo -S <cmd>
The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.
Source
Above command still needs password to be entered. To remove entering password manually, in cases like jenkins, this command works:
echo <password> | sudo -S <cmd>
sudo by default will read the password from the attached terminal. Your problem is that there is no terminal attached when it is run from the netbeans console. So you have to use an alternative way to enter the password: that is called the askpass program.
The askpass program is not a particular program, but any program that can ask for a password. For example in my system x11-ssh-askpass works fine.
In order to do that you have to specify what program to use, either with the environment variable SUDO_ASKPASS or in the sudo.conf file (see man sudo for details).
You can force sudo to use the askpass program by using the option -A. By default it will use it only if there is not an attached terminal.
Try this one:
echo '' | sudo -S my_command
For Ubuntu 16.04 users
There is a file you have to read with:
cat /etc/sudoers.d/README
Placing a file with mode 0440 in /etc/sudoers.d/myuser with following content:
myuser ALL=(ALL) NOPASSWD: ALL
Should fix the issue.
Do not forget to:
chmod 0440 /etc/sudoers.d/myuser
Login into your linux. Fire following commands. Be careful, as editing sudoer is a risky proposition.
$ sudo visudo
Once vi editor opens make the following changes:
Comment out Defaults requiretty
# Defaults requiretty
Go to the end of the file and add
jenkins ALL=(ALL) NOPASSWD: ALL
If by any chance you came here because you can't sudo inside the Ubuntu that comes with Windows10
Edit the /etc/hosts file from Windows (with Notepad), it'll be located at: %localappdata\lxss\rootfs\etc, add 127.0.0.1 WINDOWS8, this will get rid of the first error that it can't find the host.
To get rid of the no tty present error, always do sudo -S <command>
This worked for me:
echo "myuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
where your user is "myuser"
for a Docker image, that would just be:
RUN echo "myuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
In Jenkins:
echo '<your-password>' | sudo -S command
Eg:-
echo '******' | sudo -S service nginx restart
You can use Mask Password Plugin to hide your password
Make sure the command you're sudoing is part of your PATH.
If you have a single (or multi, but not ALL) command sudoers entry, you'll get the sudo: no tty present and no askpass program specified when the command is not part of your path (and the full path is not specified).
You can fix it by either adding the command to your PATH or invoking it with an absolute path, i.e.
sudo /usr/sbin/ipset
Instead of
sudo ipset
Command sudo fails as it is trying to prompt on root password and there is no pseudo-tty allocated (as it's part of the script).
You need to either log-in as root to run this command or set-up the following rules in your /etc/sudoers
(or: sudo visudo):
# Members of the admin group may gain root privileges.
%admin ALL=(ALL) NOPASSWD:ALL
Then make sure that your user belongs to admin group (or wheel).
Ideally (safer) it would be to limit root privileges only to specific commands which can be specified as %admin ALL=(ALL) NOPASSWD:/path/to/program
I think I can help someone with my case.
First, I changed the user setting in /etc/sudoers referring to above answer. But It still didn't work.
myuser ALL=(ALL) NOPASSWD: ALL
%mygroup ALL=(ALL:ALL) ALL
In my case, myuser was in the mygroup.
And I didn't need groups. So, deleted that line.
(Shouldn't delete that line like me, just marking the comment.)
myuser ALL=(ALL) NOPASSWD: ALL
It works!
Running shell scripts that have contain sudo commands in them from jenkins might not run as expected. To fix this, follow along
Simple steps:
On ubuntu based systems, run " $ sudo visudo "
this will open /etc/sudoers file.
If your jenkins user is already in that file, then modify to look like this:
jenkins ALL=(ALL) NOPASSWD: ALL
save the file
Relaunch your jenkins job
you shouldnt see that error message again :)
This error may also arise when you are trying to run a terminal command (that requires root password) from some non-shell script, eg sudo ls (in backticks) from a Ruby program. In this case, you can use Expect utility (http://en.wikipedia.org/wiki/Expect) or its alternatives.
For example, in Ruby to execute sudo ls without getting sudo: no tty present and no askpass program specified, you can run this:
require 'ruby_expect'
exp = RubyExpect::Expect.spawn('sudo ls', :debug => true)
exp.procedure do
each do
expect "[sudo] password for _your_username_:" do
send _your_password_
end
end
end
[this uses one of the alternatives to Expect TCL extension: ruby_expect gem].
For the reference, in case someone else encounter the same issue, I was stuck during a good hour with this error which should not happen since I was using the NOPASSWD parameter.
What I did NOT know was that sudo may raise the exact same error message when there is no tty and the command the user try to launch is not part of the allowed command in the /etc/sudoers file.
Here a simplified example of my file content with my issue:
bguser ALL = NOPASSWD: \
command_a arg_a, \
command_b arg_b \
command_c arg_c
When bguser will try to launch "sudo command_b arg_b" without any tty (bguser being used for some daemon), then he will encounter the error "no tty present and no askpass program specified".
Why?
Because a comma is missing at the end of line in the /etc/sudoers file...
(I even wonder if this is an expected behavior and not a bug in sudo since the correct error message for such case shoud be "Sorry, user bguser is not allowed to execute etc.")
I was getting this error because I had limited my user to only a single executable 'systemctl' and had misconfigured the visudo file.
Here's what I had:
jenkins ALL=NOPASSWD: systemctl
However, you need to include the full path to the executable, even if it is on your path by default, for example:
jenkins ALL=NOPASSWD: /bin/systemctl
This allows my jenkins user to restart services but not have full root access
If you add this line to your /etc/sudoers (via visudo) it will fix this problem without having to disable entering your password and when an alias for sudo -S won't work (scripts calling sudo):
Defaults visiblepw
Of course read the manual yourself to understand it, but I think for my use case of running in an LXD container via lxc exec instance -- /bin/bash its pretty safe since it isn't printing the password over a network.
Using pipeline:
echo your_pswd | sudo -S your_cmd
Using here-document:
sudo -S cmd <<eof
pwd
eof
#remember to put the above two lines without "any" indentations.
Open a terminal to ask password (whichever works):
gnome-terminal -e "sudo cmd"
xterm -e "sudo cmd"
I faced this issue when working on an Ubuntu 20.04 server.
I was trying to run a sudo command from a remote machine to deploy an app to the server. However when I run the command I get the error:
sudo: no tty present and no askpass program specified
The remote script failed with exit code 1
Here's how I fixed it:
The issue is caused by executing a sudo command which tries to request for a password, but sudo does not have access to a tty to prompt the user for a passphrase. As it can’t find a tty, sudo falls back to an askpass method but can’t find an askpass command configured, so the sudo command fails.
To fix this you need to be able to run sudo for that specific user with no password requirements. The no password requirements is configured in the /etc/sudoers file. To configure it run either of the commands below:
sudo nano /etc/sudoers
OR
sudo visudo
Note: This opens the /etc/sudoers file using your default editor.
Next, Add the following line at the bottom of the file:
# Allow members to run all commands without a password
my_user ALL=(ALL) NOPASSWD:ALL
Note: Replace my_user with your actual user
If you want the user to run specific commands you can specify them
# Allow members to run specific commands without a password
my_user ALL=(ALL) NOPASSWD:/bin/myCommand
OR
# Allow members to run specific commands without a password
my_user ALL=(ALL) NOPASSWD: /bin/myCommand, /bin/myCommand, /bin/myCommand
Save the changes and exit the file.
For more help, read the resource in this link: sudo: no tty present and no askpass program specified
That's all.
I hope this helps
The solution to the problem is
If you came across this issue anywhere else apart from the Jenkins instance follow this from the 2nd step. The first step is for the user who is having issue with the Jenkins instance.
Go to Jenkins instance of Google Cloud Console.
Enter the commands
sudo su
visudo -f /etc/sudoers
Add following line at the end
jenkins ALL= NOPASSWD: ALL
Checkout here to understand the rootcause of this issue
No one told what could cause this error, in case of migration from one host to another, remember about checking hostname in sudoers file:
So this is my /etc/sudoers config
User_Alias POWERUSER = user_name
Cmnd_Alias SKILL = /root/bin/sudo_auth_wrapper.sh
POWERUSER hostname=(root:root) NOPASSWD: SKILL
if it doesn't match
uname -a
Linux other_hostname 3.10.17 #1 SMP Wed Oct 23 16:28:33 CDT 2013 x86_64 Intel(R) Core(TM) i3-4130T CPU # 2.90GHz GenuineIntel GNU/Linux
it will pop up this error:
no tty present and no askpass program specified
Other options, not based on NOPASSWD:
Start Netbeans with root privilege ((sudo netbeans) or similar) which will presumably fork the build process with root and thus sudo will automatically succeed.
Make the operations you need to do suexec -- make them owned by root, and set mode to 4755. (This will of course let any user on the machine run them.) That way, they don't need sudo at all.
Creating virtual hard disk files with bootsectors shouldn't need sudo at all. Files are just files, and bootsectors are just data. Even the virtual machine shouldn't necessarily need root, unless you do advanced device forwarding.
Although this question is old, it is still relevant for my more or less up-to-date system. After enabling debug mode of sudo (Debug sudo /var/log/sudo_debug all#info in /etc/sudo.conf) I was pointed to /dev: "/dev is world writable". So you might need to check the tty file permissions, especially those of the directory where the tty/pts node resides in.
I was able to get this done but please make sure to follow the steps properly.
This is for the anyone who is getting import errors.
Step1: Check if files and folders have got execute permission issue.
Linux user use:
chmod 777 filename
Step2: Check which user has the permission to execute it.
Step3: open terminal type this command.
sudo visudo
add this lines to the code below
www-data ALL=(ALL) NOPASSWD:ALL
nobody ALL=(ALL) NOPASSWD:/ALL
this is to grant permission to execute the script and allow it to use all the libraries. The user generally is 'nobody' or 'www-data'.
now edit your code as
echo shell_exec('sudo -u the_user_of_the_file python your_file_name.py 2>&1');
go to terminal to check if the process is running
type this there...
ps aux | grep python
this will output all the process running in python.
Add Ons:
use the below code to check the users in your system
cut -d: -f1 /etc/passwd
Thank You!
1 open /etc/sudoers
type sudo vi /etc/sudoers. This will open your file in edit mode.
2 Add/Modify linux user
Look for the entry for Linux user. Modify as below if found or add a new line.
<USERNAME> ALL=(ALL) NOPASSWD: ALL
3 Save and Exit from edit mode
I had the same error message when I was trying to mount sshfs which required sudo : the command is something like this :
sshfs -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user#my.server.tld:/var/www /mnt/sshfs/www
by adding the option -o debug
sshfs -o debug -o sftp_server="/usr/bin/sudo /usr/lib/openssh/sftp-server" user#my.server.tld:/var/www /mnt/sshfs/www
I had the same message of this question :
sudo: no tty present and no askpass program specified
So by reading others answer I became to make a file in /etc/sudoer.d/user on my.server.tld with :
user ALL=NOPASSWD: /usr/lib/openssh/sftp-server
and now I able to mount the drive without giving too much extra right to my user.
Below actions work for on ubuntu20
edit /etc/sudoers
visudo
or
vi /etc/sudoers
add below content
userName ALL=(ALL) NOPASSWD: ALL
%sudo ALL=(ALL:ALL) NOPASSWD:ALL
I'm not sure if this is a more recent change, but I just had this problem and sudo -S worked for me.

How can I simply redirect text containing $HOME into /etc/zshenv with sudo?

I want a script to set up a better (but non-default) location for my .zshrc file:
% sudo echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv
but this fails:
zsh: permission denied: /etc/zshenv
The problem is related to the redirection being done first, but there is no obvious way to solve this. I can hear you saying 'use zsh -c' but that fails:
sudo zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
results in $HOME being evaluated inside the shell launched with sudo, so my /etc/zshenv file contains
ZDOTDIR=/var/root/.config/zsh
Trust me: my home directory is not /var/root.
I tried --preserve-env but that did not work:
sudo --preserve-env zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
Obviously I can find a workaround for this, such as saving $HOME to a local variable in the script, but it seems like there must be a more direct and simple and straightforward way to do it.
I just realized I was thinking about the problem all wrong. I want to prevent $HOME from being evaluated. Of course escaping a single quote inside a single quote is a new kind of PITA, but here it is:
sudo zsh -c 'echo '"'"'ZDOTDIR=$HOME/.config/zsh'"'"' >> /etc/zshenv'

Why is "-H" command not found?

I have a strange error with linux somehow interpreting sudo -H as two separate commands.
I'm on Cent OS 7, and I get the following:
/var/tmp/<random string>: line 8: -H: command not found
This is very vexing to me. Why would it not know this uption of sudo?
My guess would be that you have an alias or bash function that is suppressing your call to sudo. Try running the command with the full pathname for sudo (/usr/bin/sudo) on both systems, and type type sudo to see if there is an alias or bash function that is being called instead of the executable.
If there is, check the usual places like ~/.bashrc for where it is being defined so you can remove it.
Alternatively, it could be unrelated to sudo, and instead be related to whatever script you are calling with sudo.

What's the recommended way to use a non-system perl for a web app?

I want to run a Catalyst application on my web server, which has system perl v5.10. I want to use at least v5.12 for the app., and I don't want to meddle with the system perl.
Our sysadmin insists that the app. be run by a non-shell user (such as "nobody")
I know I can use perlbrew to use non-system perl for development, but I'm not sure what the best way to then run the live version is. How do people advise handling this situation?
Since you're familiar with perlbrew, you could still use that to install Perl.
perlbrew install 5.16.1 --as=5.16.1t -Dusethreads
You just make sure you give proper permissions. Say your $PERLBREW_ROOT is /home/djh/perl5/perlbrew (the default):
chmod a+x /home/djh/
chmod a+x /home/djh/perl5/
chmod a+x /home/djh/perl5/perlbrew/
chmod a+x /home/djh/perl5/perlbrew/perls/
chmod -R a+rX /home/djh/perl5/perlbrew/perls/5.16.1t/ # Capital "X"!!!
Then use the following shebang line in your script:
#!/home/djh/perl5/perlbrew/perls/5.16.1t/bin/perl
But maybe you don't want it in your home. If so, this is what you can do:
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/R/RJ/RJBS/perl-5.16.1.tar.bz2
tar xvjf perl-5.16.1.tar.bz2
cd perl-5.16.1
sh Configure -des -Dprefix=/opt/perls/5.16.1t -Dusethreads
make test
sudo mkdir /opt/perls/5.16.1t
sudo chown djh:djh /opt/perls/5.16.1t
make install
The installer will setup the permissions correctly. All you have to do is set the shebang to
#!/opt/perls/5.16.1t/bin/perl
("t" is my convention for threaded builds. Remove -Dusethreads if you don't want thread support.)
Give nobody (i.e., everybody) read and execute permission to the perl executable(s) and all libraries (everything under the #INC directories).
Change the shebang line in all your applications scripts (including at least everything under your ./scripts directory) to the instance of perl that you want to use. Or if you want to be flexible about out, point the shebang line to a symbolic link that points to a desired perl executable. Make sure this link is also accessible to nobody.
Restart your application whenever you point your symbolic link to a different version of Perl.

Subversion svn update - Not working

I'm trying to setup subversion, so everytime someone commits a change, it updates a working directory that we'll use on a dev box as the 'test' site.
I've setup post-commit, and added the line:
#!/usr/bin/perl /usr/bin/svn update /home/administrator/sites/checkmyid --username root --password xxx
Can anyone tell me why this doesn't work when run automatically, but when I run it at the command prompt:
sudo ./post-commit
/home/administrator/sites/svn
It works fine?
I've tried chaning the owner of the working directory to www-data but it doesn't seem to want to work?
FIXED IT MYSELF
Basically, it was a permissions problem. I used the command
sudo chown -R www-data /home/administrator/sites/checkmyid
And now it works perfectly!
Your post-commit script contains bash code, but the shebang is saying to use Perl to run it.
Plus, shebang lines should be on their own line; put the actual commands to run on another line.
sudo chown -R www-data /home/administrator/sites/checkmyid