You can use WP-CLI to automate a lot of WordPress related jobs.

Update: 2023 : we have released a cool tool that switches to the user automatically - check it out: owp

Here's a video explanation of this post.

If you run it as root user you'll get this warning.

Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.

If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.

If you'd like to continue as root, please run this again, adding this flag: --allow-root

If you'd like to run it as the user that this site is under, you can run the following to become the respective user:

sudo -u USER -i -- wp <command>

Even though it's super annoying it is a legit attempt to protect you and your server if you run the tool as root.
The reason is if there's a poorly written plugin it can and possibly damage your system because the root user doesn't have any restrictions at all. If you really know what you're doing you can pass --allow-root parameter but I don't recommend this.

It turns out that you can get the WP-CLI info as root though.

# wp --info
OS: Linux 3.10.0-714.10.2.lve1.5.17.1.el7.x86_64 #1 SMP Tue May 22 10:39:25 EDT 2018 x86_64
Shell: /bin/bash
PHP binary: /opt/cpanel/ea-php72/root/usr/bin/php
PHP version: 7.2.20
php.ini used: /opt/cpanel/ea-php72/root/etc/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /var/www/vhosts/qsandbox.com/www
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.2.0

WP-CLI shows you how to run it as a given Linux user but I prefer to use this command instead which I think is more clear.

sudo -H -u YOUR_LINUX_USER bash -c 'wp option get home --path=/var/www/html --skip-plugins --skip-themes'

Related
https://blog.christosoft.de/2017/06/wp-cli-run-as-correct-user/

How do you run wp-cli? Do you switch to that user first and then run WP-CLI commands?


Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer