admin管理员组

文章数量:1122832

Over the past several years, WP-CLI has become pretty popular, but there's still a lot of warnings about the --allow-root flag for security reasons, and instead developers are urged to use non-root users.

But, there's also a lot of cases, like setting up crontab jobs to repeat certain tasks, where --allow-root ensures that the tasks are run properly without interaction.

Which commands might be "safely" run using this flag, esp. in cron jobs?

Over the past several years, WP-CLI has become pretty popular, but there's still a lot of warnings about the --allow-root flag for security reasons, and instead developers are urged to use non-root users.

But, there's also a lot of cases, like setting up crontab jobs to repeat certain tasks, where --allow-root ensures that the tasks are run properly without interaction.

Which commands might be "safely" run using this flag, esp. in cron jobs?

Share Improve this question asked Mar 28, 2024 at 15:30 Jesse NicklesJesse Nickles 7357 silver badges19 bronze badges 3
  • Related: wordpress.stackexchange.com/questions/354247/… – Jesse Nickles Commented Apr 5, 2024 at 9:34
  • Related: wordpress.stackexchange.com/questions/376682/… – Jesse Nickles Commented Apr 5, 2024 at 9:34
  • Related: wordpress.stackexchange.com/questions/236725/… – Jesse Nickles Commented Apr 5, 2024 at 9:35
Add a comment  | 

1 Answer 1

Reset to default 3

No commands are safe when ran as root. Even the help screens aren't safe as root.

The reason the --allow-root flag is considered dangerous is not because of what the CLI commands themselves do, but because your entire sites code is loaded when WP CLI runs, but now as root. This would mean any hidden malware would now have root on your server, and any code that makes a mistake has no guard rails to prevent it destroying the entire machine. For this reason all commands are just as dangerous.

Fundamentally, it should never be necessary to run WP CLI as a root user, and if you're logged in as root you can still run WP CLI as another non-root user to avoid the security issues via sudo, e.g. this is one way to do it:

function noroot() {
  sudo -EH -u "your_web_user" "$@";
}

noroot wp post list # <- runs as your_web_user not root

This is one of many ways to run WP CLI as another user even when in a root shell. Using a shell session with a non-root user is still preferable though.

本文标签: securityWhich WPCLI commands can be safely run with allowroot flag