Linux Administration for DevOps

Project Goal The project involves setting up a Linux environment, creating and managing users/groups, setting appropriate permissions, and automating common tasks using shell scripts. This is a practical guide for DevOps engineers who manage Linux servers. Step 1: Set Up the Linux Environment Launch an Ubuntu EC2 Instance Create an EC2 instance with the following configuration: Instance type: t2.micro AMI: Ubuntu Server 20.04 LTS Storage: 20 GB SSH into the instance: ssh -i key.pem ubuntu@ Step 2: Manage Users and Groups Create Users Add three users: devuser1, devuser2, and devopsadmin. sudo useradd -m -s /bin/bash devuser1 sudo useradd -m -s /bin/bash devuser2 sudo useradd -m -s /bin/bash devopsadmin Set passwords for the users: echo "devuser1:password123" | sudo chpasswd echo "devuser2:password123" | sudo chpasswd echo "devopsadmin:adminpass" | sudo chpasswd Create Groups Create two groups: developers and admins. sudo groupadd developers sudo groupadd admins Add users to the groups: sudo usermod -aG developers devuser1 sudo usermod -aG developers devuser2 sudo usermod -aG admins devopsadmin Verify Group Membership groups devuser1 groups devuser2 groups devopsadmin Step 3: Set Permissions Create a Shared Directory Create a directory for developers: sudo mkdir /shared/developers Set group ownership and permissions: sudo chown :developers /shared/developers sudo chmod 770 /shared/developers Restrict Access Create a directory accessible only to admins: sudo mkdir /shared/admins sudo chown :admins /shared/admins sudo chmod 750 /shared/admins Verify permissions: ls -ld /shared/* Step 4: Automate Tasks with Shell Scripts Create a User Management Script Create a script to add users, assign them to groups, and set default passwords. Script: user_management.sh !/bin/bash Check for root privileges if [ "$EUID" -ne 0 ]; then echo "Please run as root" exit 1 fi Function to create a user and assign to a group create_user() { local username=$1 local group=$2 local password=$3 # Create user useradd -m -s /bin/bash "$username" echo "$username:$password" | chpasswd # Add user to group usermod -aG "$group" "$username" echo "User $username created and added to group $group." } Create users and assign groups create_user devuser1 developers password123 create_user devuser2 developers password123 create_user devopsadmin admins adminpass echo "User creation completed." Create a Backup Script Create a script to back up the /shared directory daily. Script: backup_shared.sh !/bin/bash Variables SOURCE_DIR="/shared" BACKUP_DIR="/backup" TIMESTAMP=$(date +"%Y%m%d_%H%M%S") Create backup directory if it doesn't exist mkdir -p "$BACKUP_DIR" Create a backup tar -czf "$BACKUP_DIR/shared_backup_$TIMESTAMP.tar.gz" "$SOURCE_DIR" echo "Backup completed: $BACKUP_DIR/shared_backup_$TIMESTAMP.tar.gz" Automate Scripts with Cron Jobs Edit the crontab: crontab -e Add the following entries: Run the backup script daily at 2 AM 0 2 * * * /path/to/backup_shared.sh Step 5: Validate the Setup Test User and Group Management Log in as devuser1 and ensure they can access /shared/developers. Log in as devopsadmin and ensure they can access /shared/admins. Test Backup Script Run the backup script: sudo bash /path/to/backup_shared.sh Verify the backup file in /backup. Step 6: Documentation and Cleanup Document All Steps Include commands and configurations in a markdown file for future reference. Cleanup (Optional) Remove test users, groups, and directories if no longer needed: sudo userdel -r devuser1 sudo userdel -r devuser2 sudo userdel -r devopsadmin sudo groupdel developers sudo groupdel admins sudo rm -rf /shared /backup Extensions Add Monitoring: Use tools like htop, top, or prometheus-node-exporter to monitor resource usage. Centralized Logging: Configure rsyslog to collect and forward logs. Advanced Automation: Use Ansible to manage users, groups, and permissions across multiple servers. This project provides a complete Linux administration setup, essential for DevOps workflows. Let me know if you need assistance with any specific part!

Jan 18, 2025 - 16:51
Linux Administration for DevOps

Project Goal
The project involves setting up a Linux environment, creating and managing users/groups, setting appropriate permissions, and automating common tasks using shell scripts. This is a practical guide for DevOps engineers who manage Linux servers.

Step 1: Set Up the Linux Environment

  1. Launch an Ubuntu EC2 Instance Create an EC2 instance with the following configuration: Instance type: t2.micro AMI: Ubuntu Server 20.04 LTS Storage: 20 GB SSH into the instance:

ssh -i key.pem ubuntu@
Step 2: Manage Users and Groups

  1. Create Users Add three users: devuser1, devuser2, and devopsadmin.

sudo useradd -m -s /bin/bash devuser1
sudo useradd -m -s /bin/bash devuser2
sudo useradd -m -s /bin/bash devopsadmin
Set passwords for the users:

echo "devuser1:password123" | sudo chpasswd
echo "devuser2:password123" | sudo chpasswd
echo "devopsadmin:adminpass" | sudo chpasswd

  1. Create Groups Create two groups: developers and admins.

sudo groupadd developers
sudo groupadd admins
Add users to the groups:

sudo usermod -aG developers devuser1
sudo usermod -aG developers devuser2
sudo usermod -aG admins devopsadmin

  1. Verify Group Membership

groups devuser1
groups devuser2
groups devopsadmin
Step 3: Set Permissions

  1. Create a Shared Directory Create a directory for developers:

sudo mkdir /shared/developers
Set group ownership and permissions:

sudo chown :developers /shared/developers
sudo chmod 770 /shared/developers

  1. Restrict Access Create a directory accessible only to admins:

sudo mkdir /shared/admins
sudo chown :admins /shared/admins
sudo chmod 750 /shared/admins
Verify permissions:

ls -ld /shared/*
Step 4: Automate Tasks with Shell Scripts

  1. Create a User Management Script Create a script to add users, assign them to groups, and set default passwords.

Script: user_management.sh

!/bin/bash

Check for root privileges

if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi

Function to create a user and assign to a group

create_user() {
local username=$1
local group=$2
local password=$3

# Create user
useradd -m -s /bin/bash "$username"
echo "$username:$password" | chpasswd

# Add user to group
usermod -aG "$group" "$username"

echo "User $username created and added to group $group."
}

Create users and assign groups

create_user devuser1 developers password123
create_user devuser2 developers password123
create_user devopsadmin admins adminpass

echo "User creation completed."

  1. Create a Backup Script Create a script to back up the /shared directory daily.

Script: backup_shared.sh

!/bin/bash

Variables

SOURCE_DIR="/shared"
BACKUP_DIR="/backup"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")

Create backup directory if it doesn't exist

mkdir -p "$BACKUP_DIR"

Create a backup

tar -czf "$BACKUP_DIR/shared_backup_$TIMESTAMP.tar.gz" "$SOURCE_DIR"

echo "Backup completed: $BACKUP_DIR/shared_backup_$TIMESTAMP.tar.gz"

  1. Automate Scripts with Cron Jobs Edit the crontab:

crontab -e
Add the following entries:

Run the backup script daily at 2 AM

0 2 * * * /path/to/backup_shared.sh
Step 5: Validate the Setup

  1. Test User and Group Management Log in as devuser1 and ensure they can access /shared/developers. Log in as devopsadmin and ensure they can access /shared/admins.
  2. Test Backup Script Run the backup script:

sudo bash /path/to/backup_shared.sh
Verify the backup file in /backup.
Step 6: Documentation and Cleanup

  1. Document All Steps Include commands and configurations in a markdown file for future reference.
  2. Cleanup (Optional) Remove test users, groups, and directories if no longer needed:

sudo userdel -r devuser1
sudo userdel -r devuser2
sudo userdel -r devopsadmin
sudo groupdel developers
sudo groupdel admins
sudo rm -rf /shared /backup
Extensions
Add Monitoring:
Use tools like htop, top, or prometheus-node-exporter to monitor resource usage.
Centralized Logging:
Configure rsyslog to collect and forward logs.
Advanced Automation:
Use Ansible to manage users, groups, and permissions across multiple servers.
This project provides a complete Linux administration setup, essential for DevOps workflows. Let me know if you need assistance with any specific part!