Introduction
This Bash script automates the process of creating a new user on a Linux server, setting up SSH access for the user, and optionally creating a directory for web content. This guide explains each part of the script and provides instructions for its use.
Script Overview
The script performs the following tasks:
- Prompts for the new user's name and creates the user.
- Sets up SSH access for the user by adding the provided SSH key.
- Optionally creates a directory for the user in the web server's root directory.
Script
#!/bin/bash
## example uat user add script
# Prompt for the new user's name
echo "Enter user's name"
read first_name
# Create the new user
sudo adduser $first_name
echo "User added: $first_name"
# Create the .ssh directory in the new user's home directory
mkdir -p /home/$first_name/.ssh/
# Prompt for the user's SSH public key
echo "Enter your SSH key"
read enter_ssh_key
echo
# Add the SSH key to the authorized_keys file
echo $enter_ssh_key >> /home/$first_name/.ssh/authorized_keys
# Set appropriate permissions for the .ssh directory and authorized_keys file
chmod -R 700 /home/$first_name/.ssh/
chmod -R 644 /home/$first_name/.ssh/authorized_keys
# Change ownership of the .ssh directory and its contents to the new user
chown -R $first_name:$first_name /home/$first_name/.ssh
# Optional: Create a directory for the user in the web server's root directory
mkdir -p /var/www/html/$first_name
chown -R $first_name:$first_name /var/www/html/$first_name
Script Details
-
Prompt for User Name
The script asks the user to input the name of the new user. This name will be used to create the user and set up the home directory.
echo "Enter user's name" read first_name
-
Create the New User
The
adduser
command is used to create a new user with the provided name.sudo adduser $first_name
-
Create .ssh Directory
The script creates the
.ssh
directory in the new user's home directory if it does not already exist.mkdir -p /home/$first_name/.ssh/
-
Prompt for SSH Key
The script prompts the user to enter their SSH public key, which will be added to the
authorized_keys
file to enable SSH access.echo "Enter your SSH key" read enter_ssh_key
-
Add SSH Key to Authorized Keys
The entered SSH key is appended to the
authorized_keys
file in the.ssh
directory.echo $enter_ssh_key >> /home/$first_name/.ssh/authorized_keys
-
Set Permissions
The script sets appropriate permissions for the
.ssh
directory and theauthorized_keys
file to ensure security:.ssh
directory:700
(read, write, and execute for owner only)authorized_keys
file:644
(read and write for owner, read-only for others)
chmod -R 700 /home/$first_name/.ssh/ chmod -R 644 /home/$first_name/.ssh/authorized_keys
-
Change Ownership
The ownership of the
.ssh
directory and its contents is changed to the new user.chown -R $first_name:$first_name /home/$first_name/.ssh
-
Optional: Create Web Directory
The script optionally creates a directory for the user in the web server's root directory and sets the appropriate ownership. This step is useful for web applications.
mkdir -p /var/www/html/$first_name chown -R $first_name:$first_name /var/www/html/$first_name
Conclusion
This script streamlines the process of creating a new user, setting up SSH access, and preparing a web directory. Modify the script as needed to fit your specific requirements.
This guide provides a clear explanation of each step in the Bash script, helping users understand and utilize it effectively.