When setting up a new Linux server, there are some tasks you perform or carry out almost every time. You may need to update packages, install required tools, create a user, configure permissions, and prepare the server for deployment.
Doing these steps manually every time is repetitive. This is where Bash scripting becomes useful.
Bash allows us to write commands in a file and execute them together. Instead of running each setup command one after another, we can create a script that handles the process for us.
Let's look at a simple Bash script that automates some basic server setup tasks.
The script
#!/bin/bash
set -e
echo "Updating server..."
sudo apt update
sudo apt upgrade -y
echo "Installing required packages..."
sudo apt install -y git curl nginx
echo "Creating deployment user..."
if ! id "deploy" &>/dev/null; then
sudo useradd -m -s /bin/bash deploy
fi
echo "Starting Nginx..."
sudo systemctl enable nginx
sudo systemctl start nginx
echo "Server setup completed!"
Understanding each line
1. #!/bin/bash
This is called a shebang. It tells the operating system to use Bash to interpret and execute the script.
2. set -e
This tells Bash to stop executing the script if a command fails. This helps prevent the script from continuing when an important setup step has gone wrong.
3. echo "Updating server..."
The echo command prints text to the terminal. Here, it tells us which part of the setup process is running.
4. sudo apt update
sudo runs a command with administrator privileges, while apt update refreshes the package lists on the server.
It checks the configured software repositories for information about available packages and their latest versions. It does not install upgrades yet.
5. sudo apt upgrade -y
This installs available updates for packages already on the server.
The -y option automatically answers yes to confirmation prompts, allowing the process to continue without waiting for manual approval.
6. echo "Installing required packages..."
This displays a message before the script begins installing the software needed for the server.
7. sudo apt install -y git curl nginx
This installs three packages:
-
git: Used for version control and working with code repositories. -
curl: Used to make requests to URLs and test network endpoints. -
nginx: A web server that can also act as a reverse proxy.
The -y option automatically confirms the installation.
8. echo "Creating deployment user..."
This displays a message to indicate that the script is moving to the user creation stage.
9. if ! id "deploy" &>/dev/null; then
This checks whether a user named deploy already exists.
-
id "deploy"checks whether the user exists. -
!reverses the result, so the condition is true if the user does not exist. -
&>/dev/nullhides the command's output. -
thentells Bash to execute the following commands if the condition is true.
This check prevents the script from trying to create the same user again.
10. sudo useradd -m -s /bin/bash deploy
This creates a new Linux user called deploy.
-
useraddcreates the user. -
-mcreates a home directory for the user. -
-s /bin/bashsets Bash as the user's default shell. -
deployis the username.
A deployment user can be used to manage application files and run deployment-related tasks without always working as root.
11. fi
This closes the if statement. In Bash, fi marks the end of a conditional block.
12. echo "Starting Nginx..."
This displays a message before the script starts the Nginx service.
13. sudo systemctl enable nginx
This configures Nginx to start automatically whenever the server boots.
14. sudo systemctl start nginx
This starts the Nginx service immediately, without requiring a server restart.
15. echo "Server setup completed!"
This displays a final message to show that the script has reached the end successfully.
Running the script
Save the script in a file called setup.sh.
First, give it permission to execute:
chmod +x setup.sh
The chmod +x command adds execute permission to the file.
Next, run it:
./setup.sh
The ./ tells Linux to execute the script from the current directory.
Why this matters in DevOps
Imagine setting up five different servers. Running the same commands manually on each server takes time and creates room for mistakes. With Bash scripting, you can automate those repeated tasks and make the setup process more consistent.
This is one of the foundations of DevOps: reducing repetitive manual work through automation.
As we progress, we can build on this knowledge with tools like Ansible and Terraform, which help automate more complex server and infrastructure configurations.
Top comments (0)