Question

How to fix permissions, I accidentally entered "sudo chown -R username /"?

How to fix permissions, I accidentally entered “sudo chown -R username /”?

I got the sudo to working again by logging into the Root account however the WordPress website shows a database error and I’m positive it’s because of the wrong permissions now… I can’t seem to connect to the database and I’m not sure which permissions to fix.

I get this error when I try to start the msql:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

Also, I have a snapshot that I have, would that fix my mistake? It’s my last resort if so.

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@csc

You’ll have to forgive me for dropping directly in to the comments instead of re-reading the original on this one. I was following the tags and got to the point about MySQL.

The issue is you ran chown on / and at this point, changing the ownership of the MySQL directory isn’t going to be enough as files that need to be owned by root and other users are now setup with ownership by a user that – best case is a sudo user – worst case, is just a normal user (thus software that needs root won’t function).

Your system won’t function properly in that state. Even if you do manage to get MySQL up and running, something else is very likely to go wrong and you’ll be back in the same situation with a different piece of software.

Given the numerous files and directories that were affected by chown -R user /, your absolute best bet is going to be deploying another Droplet and building it over – then migrating your site over to it.

Note, I’m not saying rebuild from a snapshot (unless that snapshot is before you ran this command), I mean literally start from scratch so that all files and directories are properly owned by their respective users.

I know that’s not what you want to hear right now, but you’re going to spend more time trying to fix the files/directories and their ownership/permissions than you will moving the site over. There’s no way to just undo a command either, so starting from scratch is going to be your best bet.

One thing though – don’t beat yourself up over it – we all make mistakes. I’ve made my fair share in the last 16-17 years. Moving forward, I find it best to write your commands in a text editor, and then copy -> paste in to terminal. That way you’re physically looking at the command at least twice before you hit enter.

@hansen

this is what it shows now after running chown root /var/log/btmp

Mar 17 04:28:47 walter-wraith systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE
Mar 17 04:28:49 walter-wraith sshd[605]: Failed password for root from 61.177.172.46 port 52472 ssh2
Mar 17 04:28:49 walter-wraith sshd[605]: Excess permission or bad ownership on file /var/log/btmp
Mar 17 04:28:50 walter-wraith sshd[583]: Failed password for root from 223.99.60.42 port 52118 ssh2
Mar 17 04:28:50 walter-wraith sshd[583]: Excess permission or bad ownership on file /var/log/btmp
Mar 17 04:28:50 walter-wraith sshd[583]: error: maximum authentication attempts exceeded for root from 223.99.60.42 port 52118 ssh2 [preauth]
Mar 17 04:28:50 walter-wraith sshd[583]: Disconnecting: Too many authentication failures [preauth]
Mar 17 04:28:50 walter-wraith sshd[583]: PAM 5 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=223.99.60.42  user=root
Mar 17 04:28:50 walter-wraith sshd[583]: PAM service(sshd) ignoring max retries; 6 > 3
Mar 17 04:28:50 walter-wraith sshd[605]: Received disconnect from 61.177.172.46 port 52472:11:  [preauth]
Mar 17 04:28:50 walter-wraith sshd[605]: Disconnected from 61.177.172.46 port 52472 [preauth]
Mar 17 04:28:50 walter-wraith sshd[605]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.177.172.46  user=root
Mar 17 04:28:53 walter-wraith sshd[644]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=223.99.60.42  user=root

@csc Starting a new thread, since it’s maxed out.

Can you run tail -100 /var/log/mysql/error.log because we need to see the lines just before InnoDB complaining about rights.

This comment has been deleted

Yes, use the snapshot. Remember that will reverse all changes back to whenever you made the snapshot. Meaning changes in WordPress/etc will be lost. It will almost be an impossible task to manually revert every file and directory to the correct owner.