Skip to content

Instantly share code, notes, and snippets.

@Loopshape
Last active March 10, 2024 03:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Loopshape/2c8cd9648261e803d335b096eac0dd5e to your computer and use it in GitHub Desktop.
Save Loopshape/2c8cd9648261e803d335b096eac0dd5e to your computer and use it in GitHub Desktop.
Debian Linux Root Basefile-System Repair
#!/bin/bash
# Prompt for root folder
read -p "Enter the root folder path (e.g., /): " root_folder
# Prompt for desired user and group
read -p "Enter the desired user: " desired_user
read -p "Enter the desired group: " desired_group
# Function to recursively correct ownership and permissions
correct_permissions() {
local dir="$1"
local user="$2"
local group="$3"
# Correct ownership recursively
sudo chown -R $user:$group "$dir"
# Correct permissions recursively for files
find "$dir" -type f -exec sudo chmod 644 {} \;
# Correct permissions recursively for directories
find "$dir" -type d -exec sudo chmod 755 {} \;
}
# Correct permissions for root folder
correct_permissions "$root_folder" "$desired_user" "$desired_group"
# Correct permissions for user's home directories
for user_home in "$root_folder"/home/*; do
user=$(basename "$user_home")
if [ -d "$user_home" ]; then
correct_permissions "$user_home" "$user" "$user"
fi
done
# Validate permissions for sudo command
sudo -l
# Harden SSH configuration
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart sshd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment