The create_user_with_permissions.sh script is designed to:
- Create a new user with:
- A home directory.
- The default Bash shell.
- Set permissions such that:
- The new user cannot access the files or directories of the main user.
- The main user has full access to all files and directories of the new user.
- Synchronize the contents of the
Exampledirectory from the main user's home directory to the new user'sExampledirectory. Any existing files in the new user'sExampledirectory are replaced with those from the main user.
- Flexible User Specification: Allows dynamic specification of the new user and the main user as command-line arguments.
- Permission Management:
- Ensures that the new user's files are private except for access granted to the main user.
- Restricts the new user from accessing any files or directories of the main user.
- Example Directory Synchronization:
- Uses the
rsynccommand to copy files efficiently, preserving file attributes and metadata. - Ensures the new user's
Exampledirectory is an exact replica of the main user'sExampledirectory.
- Uses the
<new_user>: The username for the new user to be created.<main_user>: The existing username with permissions to access the new user's files.
usage: Displays usage information and exits if the arguments are incorrect.create_new_user: Creates the new user, sets up their home directory and bash shell, and configures permissions for file access.sync_Example_directory: Copies or replaces all files in the new user'sExampledirectory with those from the main user'sExampledirectory, ensuring proper ownership and permissions.
- Verifies that both the new user and main user are valid.
- Checks if the main user's
Exampledirectory exists before attempting to synchronize.
-
User Creation:
- Creates the new user using
useraddwith options to generate a home directory and use the bash shell. - Ensures user-specific permissions:
- New user has private access to their home directory (
chmod 755). - Main user is granted full access to the new user's home directory via
setfacl.
- New user has private access to their home directory (
- Creates the new user using
-
Example Directory Synchronization:
- Replaces all files in the new user's
Exampledirectory with those from the main user'sExampledirectory. - Ensures the new user's
Exampledirectory has proper ownership and permissions (chown).
- Replaces all files in the new user's
-
Post-Synchronization State:
- The new user's
Exampledirectory is a replica of the main user'sExampledirectory. - Main user retains full access to the new user's home directory.
- The new user's
- Save the script as
create_user_with_permissions.sh. - Make the script executable:
chmod +x create_user_with_permissions.sh
- Run the script as a superuser (with
sudo), specifying the new user and the main user:Example:sudo bash create_user_with_permissions.sh <new_user> <main_user>
sudo bash create_user_with_permissions.sh newuser mainuser
- Operating System: Ubuntu-based systems.
- Permissions: The script must be executed with superuser privileges (
sudo). - Preconditions:
- The main user must already exist on the system.
- The new user must not already exist on the system.
- Command:
sudo bash create_user_with_permissions.sh developer admin
- Outcome:
- A new user
developeris created. - The
adminuser is granted full access todeveloper's files. - All files from
/home/admin/Exampleare copied to/home/developer/Example, overwriting existing files indeveloper'sExampledirectory.
- A new user
- Ensure that the
Exampledirectory in the main user's home contains the desired files before running the script. - The script performs a complete replacement of files in the new user's
Exampledirectory, so any pre-existing files will be deleted.
If you'd like to improve this script, you can:
- Fork the repository containing the script.
- Make your changes.
- Submit a pull request with a description of the improvements.
This script is licensed under the MIT License. You can view the full text of the MIT License here.