setup_mail_forwarding.sh 897 B

1234567891011121314151617181920212223242526
  1. #!/bin/bash
  2. # run this as root/sudo on the Ubuntu VM
  3. echo "Setting up centralized mail forwarding to lanfr144@gmail.com..."
  4. # 1. Update the skeleton directory so all NEW users created automatically forward mail
  5. echo "lanfr144@gmail.com" | sudo tee /etc/skel/.forward
  6. sudo chmod 644 /etc/skel/.forward
  7. # 2. Add forwarding to all dynamically created home directories
  8. for user_dir in /home/*; do
  9. if [ -d "$user_dir" ]; then
  10. user_name=$(basename "$user_dir")
  11. echo "lanfr144@gmail.com" | sudo tee "$user_dir/.forward"
  12. sudo chown "$user_name":"$user_name" "$user_dir/.forward"
  13. sudo chmod 644 "$user_dir/.forward"
  14. echo "Configured for user: $user_name"
  15. fi
  16. done
  17. # 3. Add forwarding for root manually
  18. echo "lanfr144@gmail.com" | sudo tee /root/.forward
  19. sudo chmod 644 /root/.forward
  20. echo "Configured for root."
  21. echo "✅ All system mail will now forward to lanfr144@gmail.com"