❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayRadurga Rajendran

Building a Secure Web Application with AWS VPC, RDS, and a Simple Registration Page

31 December 2024 at 09:41

Here, we will see how to set up a Virtual Private Cloud (VPC) with two subnets: a public subnet to host a web application and a private subnet to host a secure RDS (Relational Database Service) instance. We’ll also build a simple registration page hosted in the public subnet, which will log user input into the RDS instance.

By the end of this tutorial, you will have a functional web application where user data from a registration form is captured and stored securely in a private RDS instance.

  1. VPC Setup: We will create a VPC with two subnets:
  • Public Subnet: Hosts a simple HTML-based registration page with an EC2 instance.
  • Private Subnet: Hosts an RDS instance (e.g., MySQL or PostgreSQL) to store registration data.
  1. Web Application: A simple registration page on the public subnet will allow users to input their data (e.g., name, email, and password). When submitted, this data will be logged into the RDS database in the private subnet.

  2. Security:

    • The EC2 instance will be in the public subnet, accessible from the internet.
    • The RDS instance will reside in the private subnet, isolated from direct public access for security purposes.
  3. Routing: We will set up appropriate route tables and security groups to ensure the EC2 instance in the public subnet can communicate with the RDS instance in the private subnet, but the RDS instance will not be accessible from the internet.

Step 1: Create a VPC with Public and Private Subnets

  1. Create the VPC:

    • Open the VPC Console in the AWS Management Console.
    • Click Create VPC and enter the details:
      • CIDR Block: 10.0.0.0/16 (this is the range of IP addresses your VPC will use).
      • Name: Eg:MyVPC.
  2. Create Subnets:

    • Public Subnet:
      • CIDR Block: 10.0.1.0/24
      • Name: PublicSubnet
      • Availability Zone: Choose an available zone.
    • Private Subnet:
      • CIDR Block: 10.0.2.0/24
      • Name: PrivateSubnet
      • Availability Zone: Choose a different zone.
  3. Create an Internet Gateway (IGW):

    • In the VPC Console, create an Internet Gateway and attach it to your VPC.
  4. Update the Route Table for Public Subnet:

    • Create or modify the route table for the public subnet to include a route to the Internet Gateway (0.0.0.0/0 β†’ IGW).
  5. Update the Route Table for Private Subnet:

    • Create or modify the route table for the private subnet to route traffic to the NAT Gateway (for outbound internet access, if needed).

Step 2: Launch EC2 Instance in Public Subnet for Webpage Hosting

  1. Launch EC2 Instance:

    • Go to the EC2 Console, and launch a new EC2 instance using an Ubuntu or Amazon Linux AMI.
    • Select the Public Subnet and assign a public IP to the instance.
    • Attach a Security Group that allows inbound traffic on HTTP (port 80).
  2. Install Apache Web Server:

    • SSH into your EC2 instance and install Apache:
     sudo apt update
     sudo apt install apache2
    
  3. Create the Registration Page:

    • In /var/www/html, create an HTML file for the registration form (e.g., index.html):
     <html>
       <body>
         <h1>Registration Form</h1>
         <form action="/register" method="post">
           Name: <input type="text" name="name"><br>
           Email: <input type="email" name="email"><br>
           Password: <input type="password" name="password"><br>
           <input type="submit" value="Register">
         </form>
       </body>
     </html>
    
  4. Configure Apache:

  • Edit the Apache config files to ensure the server is serving the HTML page and can handle POST requests. You can use PHP or Python (Flask, Django) for handling backend processing.

Step 3: Launch RDS Instance in Private Subnet

  1. Create the RDS Instance:

    • In the RDS Console, create a new MySQL or PostgreSQL database instance.
    • Ensure the database is not publicly accessible (so it stays secure in the private subnet).
    • Choose the Private Subnet for deployment.
  2. Security Groups:

    • Create a Security Group for the RDS instance that allows inbound traffic on port 3306 (for MySQL) or 5432 (for PostgreSQL) from the public subnet EC2 instance.

Step 4: Connect the EC2 Web Server to RDS

  1. Install MySQL Client on EC2:

    • SSH into your EC2 instance and install the MySQL client:
     sudo apt-get install mysql-client
    
  2. Test Database Connectivity:

    • Test the connection to the RDS instance from the EC2 instance using the database endpoint:
     mysql -h <RDS-endpoint> -u <username> -p
    
  3. Create the Database and Table:

    • Once connected, create a database and table to store the registration data:
     CREATE DATABASE registration_db;
     USE registration_db;
     CREATE TABLE users (
       id INT AUTO_INCREMENT PRIMARY KEY,
       name VARCHAR(100),
       email VARCHAR(100),
       password VARCHAR(100)
     );
    

Step 5: Handle Form Submissions and Store Data in RDS

  1. Backend Processing:

    • You can use PHP, Python (Flask/Django), or Node.js to handle the form submission.
    • Example using PHP:
      • Install PHP and MySQL:
       sudo apt install php libapache2-mod-php php-mysql
    
 - Create a PHP script to handle the form submission (`register.php`):
   ```php
   <?php
   if ($_SERVER["REQUEST_METHOD"] == "POST") {
       $name = $_POST['name'];
       $email = $_POST['email'];
       $password = $_POST['password'];
       // Connect to RDS MySQL database
       $conn = new mysqli("<RDS-endpoint>", "<username>", "<password>", "registration_db");
       if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
       }
       // Insert user data into database
       $sql = "INSERT INTO users (name, email, password) VALUES ('$name', '$email', '$password')";
       if ($conn->query($sql) === TRUE) {
           echo "New record created successfully";
       } else {
           echo "Error: " . $sql . "<br>" . $conn->error;
       }
       $conn->close();
   }
   ?>
   ```
 - Place this script in the public_html directory and configure Apache to serve the form.




Step 6: Test the Registration Form

  1. Access the Webpage:

    • Open a browser and go to the public IP address of the EC2 instance (e.g., http://<EC2-Public-IP>).
  2. Submit the Registration Form:

    • Enter a name, email, and password, then submit the form.
  • Check the RDS database to ensure the data has been correctly inserted.

MY OUTPUT:

Image description

Image description

By following these steps, we have successfully built a secure and scalable web application on AWS. The EC2 instance in the public subnet hosts the registration page, and the private subnet securely stores user data in an RDS instance. We have ensured security by isolating the RDS instance from public access, using VPC subnets, and configuring appropriate security groups.

Building a Highly Available and Secure Web Application Architecture with VPCs, Load Balancers, and Private Subnets

31 December 2024 at 09:29

Overview

1. Single VPC with Public and Private Subnets

In this architecture, we will use a single VPC that consists of both public and private subnets. Each subnet serves different purposes:

Public Subnet:

  • Hosts the website served by EC2 instances.
  • The EC2 instances are managed by an Auto Scaling Group (ASG) to ensure high availability and scalability.
  • A Load Balancer (ALB) distributes incoming traffic across the EC2 instances.

Private Subnet:

  • Hosts an RDS database, which securely stores the data submitted via the website.
  • The EC2 instances in the public subnet interact with the RDS instance in the private subnet via a private IP.
  • The private subnet has a VPC Endpoint to access S3 securely without traversing the public internet.

2. Route 53 Integration for Custom Domain Name

Using AWS Route 53, you can create a DNS record to point to the Load Balancer's DNS name, which allows users to access the website via a custom domain name. This step ensures that your application is accessible from a friendly, branded URL.

3. Secure S3 Access via VPC Endpoint

To securely interact with Amazon S3 from the EC2 instances in the private subnet, we will use an S3 VPC Endpoint. This VPC endpoint ensures that all traffic between the EC2 instances and S3 happens entirely within the AWS network, avoiding the public internet and enhancing security.

4. VPC Peering for Inter-VPC Communication

In some cases, you may want to establish communication between two VPCs for resource sharing or integration. VPC Peering or Transit Gateways are used to connect different VPCs, ensuring resources in one VPC can communicate with resources in another VPC securely.

Step 1: Set Up the VPC and Subnets

  1. Create a VPC:

    • Use the AWS VPC Wizard or AWS Management Console to create a VPC with a CIDR block (e.g., 10.0.0.0/16).
  2. Create Subnets:

  • Public Subnet: Assign a CIDR block like 10.0.1.0/24 to the public subnet. This subnet will host your web servers and load balancer.
  • Private Subnet: Assign a CIDR block like 10.0.2.0/24 to the private subnet, where your RDS instances will reside.
  1. Internet Gateway:
  • Attach an Internet Gateway to the VPC and route traffic from the public subnet to the internet.
  1. Route Table for Public Subnet:
  • Ensure that the public subnet has a route to the Internet Gateway so that traffic can flow in and out.
  1. Route Table for Private Subnet:
  • The private subnet should not have direct internet access. Instead, use a NAT Gateway in the public subnet for outbound internet access from the private subnet, if required.

Step 2: Set Up the Load Balancer (ALB)

  1. Create an Application Load Balancer (ALB):

    • Navigate to the EC2 console, select Load Balancers, and create an Application Load Balancer (ALB).
    • Choose the public subnet to deploy the ALB and configure listeners on port 80 (HTTP) or 443 (HTTPS).
    • Assign security groups to the ALB to allow traffic on these ports.
  2. Create Target Groups:

    • Create target groups for the ALB that point to your EC2 instances or Auto Scaling Group.
  3. Add EC2 Instances to the Target Group:

    • Add EC2 instances from the public subnet to the target group for load balancing.
  4. Configure Auto Scaling Group (ASG):

    • Create an Auto Scaling Group (ASG) with a launch configuration to automatically scale EC2 instances based on traffic load.

Step 3: Set Up Amazon RDS in the Private Subnet

  1. Launch an RDS Instance:

    • In the AWS RDS Console, launch a RDS database instance (e.g., MySQL, PostgreSQL) within the private subnet.
    • Ensure the RDS instance is not publicly accessible, keeping it secure within the VPC.
  2. Connect EC2 to RDS:

    • Ensure that your EC2 instances in the public subnet can connect to the RDS instance in the private subnet using private IPs.

Step 4: Set Up the S3 VPC Endpoint for Secure S3 Access

  1. Create a VPC Endpoint for S3:

    • In the VPC Console, navigate to Endpoints and create a Gateway VPC Endpoint for S3.
    • Select the private subnet and configure the route table to ensure traffic to S3 goes through the VPC endpoint.
  2. Configure Security Group and IAM Role:

    • Ensure your EC2 instances have the necessary IAM roles to access the S3 bucket.
    • Attach security groups to allow outbound traffic to the S3 VPC endpoint.

Step 5: Set Up Route 53 for Custom Domain

  1. Create a Hosted Zone:

    • In the Route 53 Console, create a hosted zone for your domain (e.g., example.com).
  2. Create Record Set for the Load Balancer:

    • Create an A Record or CNAME Record pointing to the DNS name of the ALB (e.g., mywebsite-1234567.elb.amazonaws.com).

Step 6: Set Up VPC Peering (Optional)

  1. Create VPC Peering:
    • If you need to connect two VPCs (e.g., for inter-VPC communication), create a VPC Peering Connection.
  • Update the route tables in both VPCs to ensure traffic can flow between the peered VPCs.
  1. Configure Routes:
    • In both VPCs, add routes to the route tables that allow traffic to flow between the VPCs via the peering connection.

With the use of public and private subnets, Auto Scaling Groups, Application Load Balancers, and VPC Endpoints, We can build a resilient infrastructure. Integrating Route 53 for custom domain management and VPC Peering for inter-VPC communication completes the solution for a fully managed, secure web application architecture on AWS.

Automating RDS Snapshot Management for Daily Testing

18 December 2024 at 06:07

Creating a snapshot ensures you have a backup of the current RDS state. This snapshot can be used to restore the RDS instance later.Β 

Steps to Create a Snapshot via AWS Management Console:Β 

  1. Navigate to the RDS Dashboard.Β 
  2. Select the RDS instance you want to back up.Β 
  3. Click Actions > Take Snapshot.Β 
  4. Provide a name for the snapshot (e.g., rds-snapshot-test-date).Β 
  5. Click Take Snapshot.Β 

Automating Snapshot Creation with AWS CLI:

Β 

aws rds create-db-snapshot \
Β Β Β Β --db-snapshot-identifier rds-snapshot-test-date \
Β Β Β Β --db-instance-identifier your-rds-instance-id

Step 2: Use the RDS Instance for TestingΒ 
Once the snapshot is created, continue using the RDS instance for your testing activities for the day. Ensure you document any changes made during testing, as these will not persist after restoring the instance from the snapshot.Β 

Step 3: Rename and Delete the RDS InstanceΒ 
At the end of the day, rename the existing RDS instance and delete it to avoid unnecessary costs.Β 

Steps to Rename the RDS Instance via AWS Management Console:Β 

  1. Navigate to the RDS Dashboard.Β 
  2. Select the RDS instance.Β 
  3. Click Actions > Modify.Β 
  4. Update the DB Instance Identifier (e.g., rds-instance-test-old).Β 
  5. Save the changes and wait for the instance to update.Β 

Steps to Delete the RDS Instance:Β 

  1. Select the renamed instance.Β 
  2. Click Actions > Delete.Β 
  3. Optionally, skip creating a final snapshot if you already have one.Β 
  4. Confirm the deletion.Β 

Automating Rename and Delete via AWS CLI:

Β 

# Rename the RDS instance
aws rds modify-db-instance \
Β Β Β Β --db-instance-identifier your-rds-instance-id \
Β Β Β Β --new-db-instance-identifier rds-instance-test-old

# Delete the RDS instance
aws rds delete-db-instance \
Β Β Β Β --db-instance-identifier rds-instance-test-old \
Β Β Β Β --skip-final-snapshot

Step 4: Restore the RDS Instance from the SnapshotΒ 
Before starting the next day’s testing, restore the RDS instance from the snapshot created earlier.Β 

Steps to Restore an RDS Instance via AWS Management Console:Β 

  1. Navigate to the Snapshots section in the RDS Dashboard.Β 
  2. Select the snapshot you want to restore.Β 
  3. Click Actions > Restore Snapshot.Β 
  4. Provide a new identifier for the RDS instance (e.g., rds-instance-test).Β 
  5. Configure additional settings if needed and click Restore DB Instance.Β 

Automating Restore via AWS CLI:

Β 

aws rds restore-db-instance-from-db-snapshot \
Β Β Β Β --db-instance-identifier rds-instance-test \
Β Β Β Β --db-snapshot-identifier rds-snapshot-test-date

Optional: Automate the Process with a ScriptΒ 
To streamline these steps, you can use a script combining AWS CLI commands. Below is an example script:

Β 

#!/bin/bash

# Variables
RDS_INSTANCE_ID="your-rds-instance-id"
SNAPSHOT_ID="rds-snapshot-$(date +%F)"
RESTORED_RDS_INSTANCE_ID="rds-instance-test"

# Step 1: Create a Snapshot
echo "Creating snapshot..."
aws rds create-db-snapshot \
Β Β Β Β --db-snapshot-identifier $SNAPSHOT_ID \
Β Β Β Β --db-instance-identifier $RDS_INSTANCE_ID

# Step 2: Rename and Delete RDS Instance
echo "Renaming and deleting RDS instance..."
aws rds modify-db-instance \
Β Β Β Β --db-instance-identifier $RDS_INSTANCE_ID \
Β Β Β Β --new-db-instance-identifier "${RDS_INSTANCE_ID}-old"

aws rds delete-db-instance \
Β Β Β Β --db-instance-identifier "${RDS_INSTANCE_ID}-old" \
Β Β Β Β --skip-final-snapshot

# Step 3: Restore RDS from Snapshot
echo "Restoring RDS instance from snapshot..."
aws rds restore-db-instance-from-db-snapshot \
Β Β Β Β --db-instance-identifier $RESTORED_RDS_INSTANCE_ID \
Β Β Β Β --db-snapshot-identifier $SNAPSHOT_ID

**Dynamic Scaling with AWS Auto Scaling Groups via Console**

9 December 2024 at 06:00

To configure an Auto Scaling Group (ASG) using the AWS Management Console. Auto Scaling Groups are an essential feature of AWS, allowing you to dynamically scale your EC2 instances based on workload demand. Here, we'll have a clear understanding of creating an ASG, configuring scaling policies, and testing the setup.

Introduction to Auto Scaling Groups

An Auto Scaling Group (ASG) ensures your application has the right number of EC2 instances running at all times. You can define scaling policies based on CloudWatch metrics, such as CPU utilization, to automatically add or remove instances. This provides cost-efficiency and ensures consistent performance.Auto Scaling Groups dynamically adjust EC2 instances based on workload.

Steps to Create an Auto Scaling Group Using the AWS Console

Step 1: Create a Launch Template

  1. Log in to the AWS Management Console and navigate to the EC2 Dashboard.
  2. Create a Launch Template:
    • Go to Launch Templates and click Create Launch Template.
    • Provide a Name and Description.
    • Specify the AMI ID (Amazon Machine Image) for the operating system. For example, use an Ubuntu AMI.
    • Select the Instance Type (e.g., t2.micro).
    • Add your Key Pair for SSH access.
    • Configure Network Settings (use the default VPC and a Subnet).
    • Leave other settings as default and save the Launch Template.
    • Launch Templates simplify EC2 instance configurations for ASG.

Step 2: Create an Auto Scaling Group

  1. Navigate to Auto Scaling Groups under the EC2 Dashboard.
  2. Click "Create Auto Scaling Group".
  3. Select Launch Template: Choose the Launch Template created in Step 1.
  4. Configure Group Size and Scaling Policies:
    • Specify the Minimum size (e.g., 1), Maximum size (e.g., 3), and Desired Capacity (e.g., 1).
    • Set scaling policies to increase or decrease capacity automatically.
  5. Choose Subnets:
    • Select the Subnets from your VPC where the EC2 instances will run.
    • Ensure these Subnets are public if instances need internet access.
  6. Health Checks:
    • Use EC2 health checks to automatically replace unhealthy instances.
    • Set a Health Check Grace Period (e.g., 300 seconds).
  7. Review and Create:
    • Review the settings and click Create Auto Scaling Group.
  8. Dynamic Scaling Policies allow automated scaling based on CloudWatch metrics like CPU utilization.

Step 3: Set Up Scaling Policies

  1. In the ASG configuration, choose Dynamic Scaling Policies.
  2. Add a policy to scale out:
    • Set the policy to add 1 instance when CPU utilization exceeds 70%.
  3. Add a policy to scale in:

    - Set the policy to remove 1 instance when CPU utilization falls below 30%.

    Stress Testing the Auto Scaling Group

    To test the Auto Scaling Group, you can simulate high CPU usage on one of the instances. This will trigger the scaling policy and add more instances.Stress testing helps verify that scaling policies are working as expected.

  4. Connect to an Instance:
    Use your private key to SSH into the instance.

   ssh -i "your-key.pem" ubuntu@<Instance-IP>
  1. Install Stress Tool: Update the system and install the stress tool.
   sudo apt update 
   sudo apt install stress 
  1. Run Stress Test: Simulate high CPU utilization to trigger the scale-out policy.
   stress --cpu 8 --timeout 600 
  1. Monitor Scaling:
    • Go to the Auto Scaling Groups dashboard in the AWS Console.
    • Check the Activity tab to observe if new instances are being launched.

My Output

Image description

Image description

Configuring Auto Scaling Groups using the AWS Management Console is a straightforward process that enables dynamic scaling of EC2 instances. By following these steps, we can ensure your application is resilient, cost-efficient, and capable of handling varying workloads.

Accessing Multiple Instances via Load Balancer in AWS

9 December 2024 at 05:49

When deploying scalable applications, distributing traffic efficiently across multiple instances is crucial for performance, fault tolerance, and reliability. AWS provides Elastic Load Balancing (ELB) to simplify this process. Here,we’ll explore the concept of load balancers, target groups, security groups, and subnets, along with a step-by-step process to setting up an Application Load Balancer (ALB) to access multiple instances.

Load Balancer:

A Load Balancer is a service that distributes incoming application traffic across multiple targets (e.g., EC2 instances) in one or more availability zones. It improves the availability and fault tolerance of your application by ensuring no single instance is overwhelmed by traffic.
AWS supports three types of load balancers:

  1. Application Load Balancer (ALB): Works at Layer 7 (HTTP/HTTPS) and is ideal for web applications.
  2. Network Load Balancer (NLB): Operates at Layer 4 (TCP/UDP) for ultra-low latency.
  3. Gateway Load Balancer (GWLB): Works at Layer 3 (IP) for distributing traffic to virtual appliances.

1. Target Groups

  • Target Groups are collections of targets (e.g., EC2 instances, IPs) that receive traffic from a load balancer.
  • You can define health checks for targets to ensure traffic is routed only to healthy instances. It can Organize and monitor targets (EC2 instances).

2. Security Groups

  • Security Groups act as virtual firewalls for your instances and load balancers.
  • For the load balancer, inbound rules allow traffic on ports like 80 (HTTP) or 443 (HTTPS).
  • For the instances, inbound rules allow traffic only from the load balancer's IP or security group.
  • It Protect resources by restricting traffic based on rules.

3. Subnets

  • Subnets are segments of a VPC that isolate resources.
  • Load balancers require at least two public subnets in different availability zones for high availability.
  • EC2 instances are usually deployed in private subnets, accessible only through the load balancer.
  • It isolate resources; public subnets for load balancers and private subnets for instances.

Steps to Set Up a Load Balancer for Multiple Instances

Step 1: Launch EC2 Instances

  1. Create Two or More EC2 Instances:
    • Use the AWS Management Console to launch multiple EC2 instances in a VPC.
    • Place them in private subnets across two different availability zones.
  2. Configure Security Groups for Instances:
    • Allow traffic only from the load balancer's security group on port 80 (HTTP) or 443 (HTTPS).

Step 2: Create a Target Group

  1. Navigate to Target Groups in the EC2 section of the console.
  2. Click Create Target Group and choose Instances as the target type.
  3. Provide the following configurations:
    • Protocol: HTTP or HTTPS
    • VPC: Select the same VPC as the EC2 instances.
    • Health Check Settings: Configure health checks (e.g., Path: / and Port: 80).
  4. Register the EC2 instances as targets in this group.

Step 3: Set Up a Load Balancer
Application Load Balancer Configuration:

  1. Go to the Load Balancers section of the EC2 console.
  2. Click Create Load Balancer and choose Application Load Balancer.
  3. Configure the following:
    • Name: Provide a unique name for the load balancer.
    • Scheme: Select Internet-facing for public access.
    • Listeners: Use port 80 or 443 (for HTTPS).
    • Availability Zones: Select public subnets from at least two availability zones.

Step 4: Attach Target Group to the Load Balancer

  1. In the Listener and Rules section, forward traffic to the target group created earlier.
  2. Save and create the load balancer.

Step 5: Update Security Groups

  1. For the Load Balancer:
    • Allow inbound traffic on port 80 or 443 (if HTTPS).
    • Allow inbound traffic from all IPs (or restrict by source).
  2. For EC2 Instances:
    • Allow inbound traffic from the load balancer's security group.

Step 6: Test the Setup

  1. Get the DNS name of the load balancer from the AWS console.
  2. Access the DNS name in your browser to verify traffic is being distributed to your instances.

Step:7 Scaling with Auto Scaling Groups
Attach an Auto Scaling Group (ASG) to the target group for dynamic scaling based on traffic demand.

To access multiple EC2 instances via a load balancer in AWS, you first deploy your EC2 instances within a Virtual Private Cloud (VPC), ensuring they are in the same target network. Install and configure your desired application (e.g., a web server like Apache) on these instances. Then, create an Application Load Balancer (ALB) or Network Load Balancer (NLB) to distribute traffic. Associate the load balancer with a Target Group that includes your EC2 instances and their ports. Next, configure the load balancer's listener to route incoming traffic (e.g., HTTP or HTTPS) to the Target Group. To make the setup accessible via a domain name, map your load balancer's DNS to a custom domain using Route 53. This ensures users can access your application by visiting the domain, with the load balancer evenly distributing traffic among the EC2 instances for high availability and scalability.

My output:

Image description

Image description

Linux Basic Commands III

22 November 2024 at 11:17

Process Management Commands:

ps - It Display running processes.
-aux: - It Show all processes.
top - It Monitor system processes in real-time.It displays a dynamic view of system processes and their resource usage.
kill - It helps to Terminate a process.
** - 9*: Forcefully kill a process.
**kill PID
* -terminates the process with the specified process ID.
pkill - Terminate processes based on their name.
pkill **- terminates all processes with the specified name.
**pgrep
- It helps to List processes based on their name.
grep - It used to search for specific patterns or regular expressions in text files or streams and display matching lines.
-i: Ignore case distinctions while searching.
-v: Invert the match, displaying non-matching lines.
-r or -R: Recursively search directories for matching patterns.
-l: Print only the names of files containing matches.
-n: Display line numbers alongside matching lines.
-w: Match whole words only, rather than partial matches.
-c: Count the number of matching lines instead of displaying them.
-e: Specify multiple patterns to search for.
-A: Display lines after the matching line.
-B: Display lines before the matching line.
-C: Display lines both before and after the matching line.

Linux Basic Commands II

21 November 2024 at 14:45

File Permission Commands:

Chmod - Change file permissions.

  • u: User/owner permissions.
  • g: Group permissions.
  • o: Other permissions.
  • +: Add permissions.
  • –: Remove permissions.
  • =: Set permissions explicitly.

Chown - Change file ownership.

Chgrp - Change group ownership.

File Compression and Archiving Commands:
**
**Tar
- Create or extract archive files.

  • -c: Create a new archive.
  • -x: Extract files from an archive.
  • -f: Specify the archive file name.
  • -v: Verbose mode.
  • -z: Compress the archive with gzip.
  • -j: Compress the archive with bzip2.

Gzip - for Compress files

  • -d: Decompress files.

Zip - to Create compressed zip archives.

  • -r: Recursively include directories.

Basic Linux Commands

15 November 2024 at 15:08
  1. pwd β€” When you first open the terminal, you are in the home directory of your user. To know which directory you are in, you can use the β€œpwd” command. It gives us the absolute path, which means the path that starts from the root. The root is the base of the Linux file system and is denoted by a forward slash( / ). The user directory is usually something like β€œ/home/username”.

Image description

  1. ls β€” Use the β€œls” command to know what files are in the directory you are in. You can see all the hidden files by using the command β€œls -a”.

Image description

  1. cd β€” Use the β€œcd” command to go to a directory. β€œcd” expects directory name or path of new directory as input.

Image description

  1. mkdir & rmdir β€” Use the mkdir command when you need to create a folder or a directory.Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm.

Image description

  1. rm – Use the rm command to delete a file. Use β€œrm -r” to recursively delete all files within a specific directory.

Image description

  1. touch β€” The touch command is used to create an empty file. For example, β€œtouch new.txt”.

Image description

  1. cp β€” Use the cp command to copy files through the command line.

Image description

  1. mv β€” Use the mv command to move files through the command line. We can also use the mv command to rename a file.

Image description

9.cat β€” Use the cat command to display the contents of a file. It is usually used to easily view programs.

Image description

10.vi - You can create a new file or modify a file using this editor.

Image description

❌
❌