fbpx

Category: WordPress

Have you seen this error that wp-cli outputs when you start it while logged in as root?

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>

You can't run any wp-cli commands unless you explicitly pass some extra parameters to the command.

This is done for your own good and trying to protect you.

How to fix this error: YIKES! It looks like you're running this as root

Option 1: Unsecure option

pass --allow-root to the command line options.

We do NOT recommend using that option. Software tools have glitches and a script. plugin, theme that has root or admin access can be quite dangerous.

If plugin or theme has a glitch it will have unrestricted access to the WHOLE server!

The problem will be even bigger if your site is infected with a virus. Then nothing can stop the virus from spreading and infecting more sites on the server.

Additionally, if you run the command as root WordPress, plugin and/or theme files will be owned by root:root, which will cause problems later on because all updates will fail because of the different permissions.

Option 2: using sudo

The wp-cli has suggested a fix by running sudo command. Our modified command uses bash.

It works ok for simpler commands. If you need to do some db replacements it can get quite long.

The following command will run wp option get home command as a user: SOME_USER and will set its home dir.

The drawback is that you have to keep checking what's the user that owns a folder and that is unnecessary step.

sudo -H -u 'SOME_USER' bash -c 'time wp option get home'

How do you determine the SOME_USER in the above example.

You can do this by running the following command which will show you the owner of the folder.

ls -lah

The output of the command should be as follows. There you will see the owner of the folder.

drwx------ 3 some-user some-user 4.0K Mar  9  2023 htdocs
drwx------ 1 some-user some-user 4.0K Apr 20  2023 tmp
drwx------ 3 some-user some-user 4.0K Aug  1  2023 logs

Option 3: using owp

Switching to the user's account. If you're root and if the user has a shell set you can run this.

Then if the wp-cli is installed in the default location e.g. /usr/local/bin/wp the user can access it.

su some-user

Option 4: using owp

We have a created a wrapper tool to wp-cli that automatically detects the current user and makes running wp-cli commands a lot easier. Our tool will automatically determine the user depending on the folder you're currently in.

Check out owp tool that we've created. You don't need to pass --allow-root.

owp user list

Join our mailing list

Get important news about cool products we release and invitations to beta test new products

Find out the Social networks we're on