Skip to main content

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:
  1. Prompts for the new user’s name and creates the user.
  2. Sets up SSH access for the user by adding the provided SSH key.
  3. Optionally creates a directory for the user in the web server’s root directory.

Script

Script Details

  1. 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.
  2. Create the New User The adduser command is used to create a new user with the provided name.
  3. Create .ssh Directory The script creates the .ssh directory in the new user’s home directory if it does not already exist.
  4. 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.
  5. Add SSH Key to Authorized Keys The entered SSH key is appended to the authorized_keys file in the .ssh directory.
  6. Set Permissions The script sets appropriate permissions for the .ssh directory and the authorized_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)
  7. Change Ownership The ownership of the .ssh directory and its contents is changed to the new user.
  8. 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.

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.