How To Fix chmod -R 777 / [or] chown -R username:username /
Well if you are like me, you would have a tough time with the Terminal sometimes. Today, while I was working on my live server, I accidentally ran the following command while I was trying to fix the forbidden access to a file while access from the web browser :
sudo chown -R www-data:www-data /
As the command probably meant to execute, it changed the file permissions of the whole system assigning www-data ‘apache’ as the owner of all the files and removed, killed the ‘root’ users permissions. I was never able to use the sudo command anymore as the root was no longer accepted as the sudoer. I kept getting the error – sudo: must be setuid root. Consequently, all the virtual hosts on my server failed, all my websites were immediately shutdown since mysql server could no longer run because of the improper file permissions. You would same errors when you try to run the following command too :
chmod -R 777 /
Being a ubuntu noob myself, I looked up the Internet to find a solution for it and most of them told me and the others who had faced the same problem to do a full reinstall of the system. I even asked the support of my hosting provider, they immediately replied that I have to reinstall my system to get it fixed. Just like you, the next question in my mind was,
What? Make a Fresh Install of the Server??
Well, this is the obvious and the safest solution to the problem because of the following reasons :
- All your permissions for all the files in your system are messed up, including the special permissions which some software on your system have for its own files.
- There is no way you can fix those permissions safely because you don’t what permissions to set for which files.
- There is a high security threat for your system from the internet, since you have applied read, write, execute permissions to all your files on the server.
However, if you still prefer not to fresh install your system, if your’s is a standalone server you can use the Ubuntu Live CD to fix the root permissions of the system. But re-installing the system is the proper way to fix it.
Solution : Making a Fresh Install and Restoring it through Copying files the System from your Old Damaged System
In my case, I couldn’t restore my system from an earlier backup since I had never taken a backup ever since. If i make a fresh install of the system, I would be losing all my files, my mysql databases, wordpress installations and everything.
Here’s what I did to make everything work perfect again. It took me about an hour to get all my websites up and running again, which I think is fairly fast instead of trying to repairing the system and the permissions.
- Make a checklist of all your databases, users, files in your system. Trust me, this is going to help you a lot while move through.
- Trying to copy your files from the server to your local system is going to take a hell load of time. So BUY a new server from your hosting provider.
- Make a fresh install of your operating system (Ubuntu) in my case and set up the users, permissions, apache and mysql, vsftpd as the way you did in your previous damaged computer.
- Setup the virtual hosts and the create the directories where you will be putting the files.
- Here, comes the hard part, Copying files :
- Copying files from your old server to your new server one by one, is going to take a lot of requests and its going to significantly take a lot of time. Therefore, package the files into compressed archives.
- Use zip command to package the files, if you had installed zip earlier.
- zip -r archive-name.zip directory-to-be-compressed – Compress in your old server
- unzip archive-name.zip – Extract in your new server
- If you don’t have zip, you can use the inbuilt compress command ‘tar’ :
- tar -zcvf archive-name.tar.gz directory-to-be-compressed – Compress in your old server
- tar -zxvf archive-name.tar.gz – Extract in your new server in the current directory
- tar -zxvf archive-name.tar.gz -C /folder/to/extract/to/ – Extract in your new server in the folder you want
- To transfer the files, you need to wget command :
- wget http://youroldwebsite.com/path-of-your-file.zip
- You can package your mysql contents at /var/lib/mysql and move to a folder where you can access from a web browser.
- After you successfully extract all the files, try to access them from the web browser.
- STOP your mysql server instance by running
sudo service mysql stop
and replace the files for mysql at /var/lib/mysql with the one you transferred from your old server. Try to start your mysql server using
sudo service mysql start
and if it fails to start, you need to run
sudo chown -R mysql:mysql /var/lib/mysql/
- If everything went well, you would be having a new server with all your websites running properly just like mine.
All the best, don’t lose hope! If you have any questions, kindly comment them below, I’ll be glad to help or if you have a better way of fixing the damaged system, kindly let me know.