Restoring Ubuntu default file permissions

From Joe's Boredom

Made a big mistake and ran the following as root (do NOT do this) while logged into my 8.04 server via SSH.

Terminal
name@machine:/$ chmod -R 0777 /*

Searched the net trying to find a way to restore all the files and dir's to there default permissions only to find the common "re-install" the operating system responce, which i did not want to do. The idea to install Ubuntu as a Virtual machine using VirtualBox, creating a script to log the fresh installs file permissions and then use that "permissions" log to change the broken servers permissions back to normal came to mind, so that is what this article is about.


So here is what i did...


1) Downloaded and installed the correct version (matching the version of the Broken Machine) of Ubuntu to a Virtual machine (* make sure you create a large enough Virtual hard-drive to install Ubuntu and all the programs you have installed on your broken machine ).


2) Ran the following on the broken machine to get a list of all programs installed.
This will be used to install the same programs on the Virtual machine to retrieve the correct file permissions for those programs.

Terminal
name@machine:/$ dpkg --get-selections > /path/to/installed-software.log


3) Ran the following on the Virtual machine.
This will freshly install all the programs which are currently installed on the broken machine to the Virtual machine.

Terminal
name@machine:/$ sudo apt-get install dselect
name@machine:/$ sudo dpkg --set-selections < /path/to/installed-software.log
name@machine:/$ sudo dselect


4) Created a new folder on the Virtual machine and named it /filepermissions/
The first script below will create a separate log file for each "top level" dir on the drive and save them in the new /filepermissions/ folder.


5) In 'root', ran this script on the Virtual machine...
This is the script which will create the "permission log file".
Create a new file, /filepermissions/log_file_permissions.sh
Make your newly created file executable

Terminal
name@machine:/$ sudo chmod +x /path/to//filepermissions/log_file_permissions.sh

Copy the code posted here to /filepermissions/log_file_permissions.sh then save.
And the good'ol Disclaimer: This script worked for me and i hold no responsibility for what it does for you. <source lang="bash">

  1. !/bin/bash

folders[0]='bin'; folders[1]='boot'; folders[2]='dev'; folders[3]='etc'; folders[4]='initrd'; folders[5]='lib'; folders[6]='opt'; folders[7]='proc'; folders[8]='root'; folders[9]='sbin'; folders[10]='srv'; folders[11]='sys'; folders[12]='tmp'; folders[13]='usr'; folders[14]='var';

  1. Add to the 'folders' array if you need to...

folders_count=${#folders[@]}; for (( i=0;i<$folders_count;i++)); do

folder="${folders[${i}]}"; if [ -d /$folder ] && [ $folder != '/' ]; then echo "File permission search for folder /$folder"; sudo find "/$folder" -exec stat --format="chmod %a %n" "{}" \; > "$folder.permissions" fi

done </source>


6) In 'root', copied the /filepermissions/ folder with all its log files from the Virtual machine to the Broken Machine and then ran this script on the Broken machine...
Create a new file, /filepermissions/change_file_permissions_from_log.sh
Make your newly created file executable

Terminal
name@machine:/$ sudo chmod +x /path/to//filepermissions/change_file_permissions_from_log.sh

Copy the code posted here to /filepermissions/change_file_permissions_from_log.sh then save.
And the good'ol Disclaimer: This script worked for me and i hold no responsibility for what it does for you. <source lang="bash">

  1. !/bin/bash

permFiles="*.permissions";

for f in *.permissions; do

echo "Setting permissions for '$f'"; if [ "$f" != "$permFiles" ] ; then while read line do $line; done < "$f" fi

done

</source>


While running that last script on the Broken Machine, there where some errors about files which where missing, but in the end everything seamed to be working fine. All known problems where corrected.

There are a couple of programs, viewvc, rtorrent and a few others which i have re-installed manually since they where originally installed via a download from the devs site.

This process too a long time but, to me, it seamed eazyer then having to re-configure the server from scratch.


_