Ubuntu Support Template
Ubuntu Version:
26.04 LTS
Desktop Environment (if applicable):
none
Problem Description:
I have been running my own Apache PHP application on Ubuntu Server 20.04 LTS and am now trying to get it up and running on a new server running Ubuntu Server 26.04 LTS. I am currently struggling with an issue involving sudo.
I have already switched from sudo-rs to sudo.ws to ensure consistent conditions. Unfortunately, it does not work with either option.
The PHP application calls iptables:
exec('sudo /usr/sbin/iptables -4 -L INPUT -n -v --line-numbers', $out);
To make this possible, I have defined the following in /etc/sudoers.d/nopasswd:
Defaults logfile="/var/log/sudo.log"
User_Alias U_IPTABLES = www-data
Cmnd_Alias C_IPTABLES = /usr/sbin/iptables, /usr/sbin/ip6tables
U_IPTABLES ALL=(root) NOPASSWD: C_IPTABLES
It works perfectly on the old server; the web server can call iptables and I can also see an entry in the log file /var/log/sudo.log:
May 12 10:54:03 : www-data : TTY=unknown ; PWD=/srv/www/admin ; USER=root ;
COMMAND=/usr/sbin/iptables -4 -L INPUT -n -v --line-numbers
However, on the new server, this fails even though the configuration is identical:
There is no entry in /var/log/sudo.log
In /var/log/apache2/error.log, I find:
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
Unfortunately, I’m at a loss now. It looks to me as though sudo, when called by www-data, isn’t taking the sudoers configuration into account. However, I can’t pinpoint the exact cause at the moment. My tests all yield the same result:
new server:
root@speedtest:/# sudo -l -U www-data
Matching Defaults entries for www-data on speedtest:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, logfile=/var/log/sudo.log
User www-data may run the following commands on speedtest:
(root) NOPASSWD: /usr/sbin/iptables, /usr/sbin/ip6tables
root@speedtest:/#
old server:
root@speedtest:/# sudo -l -U www-data
Matching Defaults entries for www-data on speedtest:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, logfile=/var/log/sudo.log
User www-data may run the following commands on speedtest:
(root) NOPASSWD: /usr/sbin/iptables, /usr/sbin/ip6tables
root@speedtest:/#
Unfortunately, it still doesn’t work.
When I switch to root via `sudo -i` from my personal user account, this is correctly logged, so the configuration is generally applied as well.
May 12 11:35:15 : jbr : TTY=pts/0 ; PWD=/root ; USER=root ; COMMAND=/bin/bash
What else can I do? How can I narrow down the problem? I’m slowly running out of ideas, unfortunately.