Linux permission denied with sudo

Receiving permission denied when using sudo

I have added the following single line to /etc/sudoers via command visudo : www-data ALL=(ALL) NOPASSWD: /var/www/db_backup.sh Upon executing the command as www-data via: sudo -u www-data /var/www/db_backup.sh I receive the following message: sudo: unable to execute /var/www/db_backup.sh: Permission denied Unsure why this would be as I have used this same procedure in the past without issue. There must be something I am overlooking. Does anything point out to anyone. My /etc/sudoers file is listed below:

# # This file MUST be edited with the 'visudo' command as root. # # Please consider adding local content in /etc/sudoers.d/ instead of # directly modifying this file. # # See the man page for details on how to write a sudoers file. # Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # Host alias specification # User alias specification # Cmnd alias specification # User privilege specification root ALL=(ALL:ALL) ALL www-data ALL=(ALL) NOPASSWD: /var/www/db_backup.sh # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL # See sudoers(5) for more information on "#include" directives: #includedir /etc/sudoers.d 

Also as a side note when attempting to run the command sudo /var/www/db_backup.sh from within a php process running under www-data the following message is received (although I gather that’s just from the issue with the permissions not being picked up from the sudoers file?): sudo: no tty present and no askpass program specified EDIT ls -al /var/www/db_backup.sh yields the following result: -rwxrwx— 1 root root 523 Jan 7 2019 /var/www/db_backup.sh

Источник

How to solve «permission denied» when using sudo with redirection in Bash?

When using sudo to allow edits to files, I regularly get ‘permission denied’. For example, my mouse is jittery and sluggish, so I want to disable polling:

sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf 
bash: /etc/modprobe.d/local.conf: Permission denied 
sudo echo N> /sys/module/drm_kms_helper/parameters/poll 
bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied 

7 Answers 7

Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root

Then you can use redirection

echo N> /sys/module/drm_kms_helper/parameters/poll 

Otherwise you can run bash string with sudo

sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll" 

You can’t run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo «Hi» [sudo] password for saji: Hi

Ok, if that’s the case, then please update your answer to reflect that running echo is a problem in that case only.

You can’t simply run the shell builtin echo as sudo, unless you do something like sudo bash -c ‘echo …’ ; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

Читайте также:  Astra linux grub загрузка по умолчанию

If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:

  • shell invokes sudo echo «options drm_kms_helper poll=N» , which executes sudo command with echo «options drm_kms_helper poll=N» command line
  • sudo asks for a password, opens superuser shell and invokes echo «options drm_kms_helper poll=N» , which runs echo command passing it «options drm_kms_helper poll=N»
  • echo, running with root privileges, prints the string to its standard output.
  • echo command terminates, superuser shell exits, sudo terminates
  • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf , which is writeable only by root. It gets «permission denied» error.

For the ways to fix this see @shantanu answer.

(*) — while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo . command. When opening the file fails the shell doesn’t even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).

$ touch ./onlyroot.txt $ sudo chown root:root ./onlyroot.txt $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt bash: onlyroot.txt: Permission denied 

In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word «root». However, when the output redirection fails in the calling shell, «who.txt» file is also missing because the command was not invoked.

Источник

How to Fix Ubuntu Permission Denied When Using sudo Command

Fixing permission denied in Ubuntu when using sudo

Sudo, or superuser do is a very powerful command in Linux. It essentially allows you to run the command as root, the highest level of user in the Linux hierarchy, and analogous to administrator user account in Windows.

Root users can execute very sensitive commands, such as formatting a hard drive and installing new hardware drivers.

Therefore, the usage of the root account and sudo command is limited to prevent damage to your system. However, there are times when you cannot access sudo command or root shell in Ubuntu Linux.

This could prove to be frustrating for you, especially if you need to do routine system maintenance like updating installed software in your system. Fortunately, the permission issue is easy to fix and only requires a few commands in the terminal.

First of all, you need to open a terminal window, and type

$ chmod 755 / $ chmod 755 /bin $ chmod 755 /lib

The command will reset the permission of the file system and only needs to be run once to fix the sudo issue. Permission problem might affect your ability to run superuser command and is common to Ubuntu Linux.

Читайте также:  Установка dr web linux server

After running all three commands, retry using the sudo command. By then, you should be able to run root-only commands through sudo.

However, sometimes the commands above do not solve sudo permission issues. If the problem persists, it might be possible that your account is not authorized to use sudo. In Ubuntu Linux and Linux in general, accounts that can use sudo is kept in the “sudoers” group.

Users that are not listed in this group will not be able to use sudo, and a new user needs to be created. This is designed to prevent malicious users to gain full access to the system through a compromised user account.

To get started, you will need access to the root account or another user account that is able to run sudo command.

Log in with the desired user’s credential and issue this following command:

Change the username to anything you desire, and enter a password for the new user. Make sure that you enter a secure password, and retype your password to continue the user creation process.

You might be prompted to enter additional information about the new user, which you can skip by pressing Enter. After the user has been created, enter the following command to enroll the user into the sudo group. Replace the “newuser” name with your newly created username:

Verify that the user is now enrolled to the sudo group by using this following command:

The system will list the groups of new user. Ensure that “sudo” group is listed, and then you can proceed to use su with the new user’s credentials. Switch users in the terminal by using this following command, and don’t forget to replace “newuser” with the username you created:

If you find this guide useful, don’t forget to bookmark this page. You might also want to read other tutorials from us such as this one on how to restart your Ubuntu X window server right from the command line.

We are a bunch of people who are still continue to learn Linux servers. Only high passion keeps pushing us to learn everything.

Источник

Sudo Permission Denied with Redirect or Pipe [Solutions]

sudo permission denied

In this tutorial, I will show you how to use the sudo command when using it with redirect or pipe. When using sudo with redirection (>) or pipe (|), you will get Permission denied error message in bash output. Further here, I will show you different ways to fix this problem.

Solutions for sudo permission denied

Let’s go through some of the solutions when you encounter sudo permission denied error when using a combination of redirect (>) or pipe (|).

Читайте также:  Открыть терминал astra linux

Use sudo with tee

For instance, to redirect the output of ‘echo 1’ command to ‘ip_forward’ file, run:

$ sudo echo 1 > /proc/sys/net/ipv4/ip_forward bash: /proc/sys/net/ipv4/ip_forward: Permission denied

The above sudo command resulted in permission denied because redirection is done by the shell which doesn’t has write permission.

We can use sudo command with tee command to resolve this error:

$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

Using the above approach, the executed command before the pipe will not run as root (echo 1). That’s useful if you just need the output of a program, which does not require root privileges.

In case the command before pipe requires root, we could use sudo before each command, as:

$ sudo echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward > /dev/null

The similar approach we can use to write «1» to «ip_forward» file, like in the previous examples, would be to elevate the process to write to the file.

An example would be as follows:

$ sudo tee /proc/sys/net/ipv4/ip_forward > /dev/null 

Run shell with sudo -c

Another popular approach would be to run another shell as root using the -c option.

$ sudo sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'

Run shell using sudo -s

Another way would be to launch a shell with sudo -s and then run the command:

$ sudo -s # echo 1 > /proc/sys/net/ipv4/ip_forward # ^D $

By Using a Bash Script

There is yet another way to run sudo with redirect or pipe, and it is by creating a bash script with all your commands, and run that script with sudo.

Let see how we can accomplish that. First, we need to create a new file using any text editor, like nano, vim, gedit, or any other. Let's name it "myscript.sh".

Then paste the following commands to myscript.sh and save the file:

#!/bin/sh echo 1 > /proc/sys/net/ipv4/ip_forward

Now you just need to run the myscript.sh file with sudo command:

Conclusion

We have seen multiple ways to use sudo with redirect or pipe and avoid permission denied errors. If you know of another way to accomplish this or have any questions, please post them in the comment section below.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Like

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin is a seasoned IT professional with over two decades of experience. He has excelled in roles such as a computer science instructor, Linux system engineer, and senior analyst. Currently, he thrives in DevOps environments, focusing on optimizing efficiency and delivery in AWS Cloud infrastructure. Bobbin holds certifications in RHEL, CCNA, and MCP, along with a Master's degree in computer science. In his free time, he enjoys playing cricket, blogging, and immersing himself in the world of music.

Источник

Оцените статью
Adblock
detector