Understanding the Error
"Permission denied" means the current user lacks the necessary permissions to read, write, or execute a file or directory.
Diagnosis
# Check file permissions and ownership
ls -la /path/to/file
# Check your current user
whoami
idCommon Scenarios
Web Server Cannot Read Files
# Web servers typically run as www-data
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;Script Not Executable
chmod +x /path/to/script.shSSH Key Permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pubSELinux or AppArmor Blocking
# Check SELinux status
getenforce
# Check AppArmor
sudo aa-status
# Check for denials
sudo ausearch -m avc -ts recent # SELinux
sudo journalctl | grep apparmor # AppArmorWhen sudo Doesn't Work
# Check if user is in sudo group
groups username
# Add to sudo group
sudo usermod -aG sudo username