Saturday, August 26, 2023

10 scp command example in Linux | How to copy files from remote server in Linux

10 examples of `scp` (secure copy) commands in Linux, along with explanations for each:


1. Copy a local file to a remote server:

scp /path/to/local/file.txt  user@remote-server:/path/on/remote/

This command copies the local `file.txt` to the remote server at the specified path.


2. Copy a remote file to a local directory:

scp user@remote-server:/path/to/remote/file.txt /path/on/local/

This command copies the remote `file.txt` to the local directory at the specified path.


3. Copy an entire directory to a remote server:

scp -r /path/to/local/directory/ user@remote-server:/path/on/remote/

The `-r` flag indicates recursive copying, allowing you to copy the entire local directory to the remote server.


4. Copy a remote directory to a local directory:

scp -r user@remote-server:/path/to/remote/directory/ /path/on/local/

Similar to the previous example, this command copies a remote directory to a local directory using the `-r` flag.


5. Copying with a specific SSH key:

scp -i /path/to/private/key.pem /path/to/local/file.txt user@remote-server:/path/on/remote/

If you want to use a specific private key for the SSH connection, you can use the `-i` flag followed by the path to the private key.


6. Copying using a specific port:

scp -P 2222 /path/to/local/file.txt user@remote-server:/path/on/remote/

The `-P` flag specifies the port to be used for the SSH connection. In this example, the port is set to `2222`.


7. Copying multiple files with a wildcard:

scp /path/to/local/*.txt user@remote-server:/path/on/remote/

You can use wildcards like `*` to match multiple files and copy them to the remote server.


8. Copying with progress information:

scp -r /path/to/local/directory/ user@remote-server:/path/on/remote/ -v

The `-v` flag adds verbose output, showing the progress of the copy operation.


9. Copying between two remote servers:

scp user@source-server:/path/to/source/file.txt user@destination-server:/path/on/destination/

You can use `scp` to directly transfer files between two remote servers.


10. Copying with compression:

scp -rC /path/to/local/directory/ user@remote-server:/path/on/remote/

The `-C` flag enables compression during the copy process, which can speed up the transfer of large files or directories.


Remember to replace `/path/to/local` and similar placeholders with the actual paths you want to use in your specific scenarios, and replace `user`, `remote-server`, and other placeholders with your actual remote server details.

Monday, August 14, 2023

How to create swap file in Linux | How to increase swap partition in RHEL/CentOS

 To create a swap file in Red Hat Enterprise Linux 8 (RHEL 8) or any other
  Linux 
distribution, you can follow these steps:

  • Check Current Swap Usage (Optional): Before creating a new swap file, you might want to check your current swap usage. You can use the free or swapon command to do this:
    bash

    free -h 

    swapon -s

  • Determine Swap File Size: Decide on the size of the swap file you want to create. The size of the swap file depends on factors such as the amount of RAM you have and your specific needs. A common recommendation is to set the swap size to be equal to or slightly larger than your system's RAM.

  • Create the Swap File: Open a terminal and use the following commands to create a swap file. Replace <swapfile_path> with the actual path where you want to create the swap file (e.g., /mnt/swapfile), and <swapfile_size> with the desired size in megabytes (e.g., 2048M for 2GB):
    bash
     
    dd if=/dev/zero of=<swapfile_path> bs=1M count=<swapfile_size>

    Example:

    dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048

  • Adjust Permissions: Set appropriate permissions on the swap file to ensure it's accessible only by the root user:
    bash

    sudo chmod 600 <swapfile_path>

    Example:

    sudo chmod 600 /mnt/swapfile

  • Configure the Swap File: Set up the swap file using the mkswap command:
    bash

    sudo mkswap <swapfile_path>

    Example:

    sudo mkswap /mnt/swapfile

  • Enable the Swap File: Activate the swap file using the swapon command:
    bash

    sudo swapon <swapfile_path>

    Example:

    sudo swapon /mnt/swapfile

  • Make Swap Permanent (Optional): To ensure that the swap file is enabled after system reboots, you need to add an entry to the /etc/fstab file. Open the file in a text editor:
    bash

    sudo nano /etc/fstab


    Add the following line to the file, where <swapfile_path> is the path of your swap file:
    php

    <swapfile_path> swap swap defaults 0 0

    Example:

    /mnt/swapfile swap swap defaults 0 0


    Save and close the file.

  • Verify Swap: You can verify that the swap file is being used by checking the output of the free or swapon command again:
    bash

    free -h

    swapon -s

That's it! You have successfully created a swap file on your Red Hat Enterprise Linux 8 or any Linux flavoured system. Remember that using swap space can impact performance, so it's recommended to have an appropriate balance of RAM and swap based on your system's needs.

Saturday, August 12, 2023

Head Command Example in Linux | How to Use Head Command in Linux

The `head` command in Linux is used to display the beginning lines of a text file. By default, it displays the first 10 lines of a file, but you can specify the number of lines you want to display using the `-n` option. Here are some examples:


1. Display the first 10 lines of a file (default behavior):
   
   head filename.txt
   
2. Display a specific number of lines (e.g., first 20 lines):
   
    head -n 20 filename.txt
   
3. Display the first few lines of multiple files:
   
     head file1.txt file2.txt
   
4. Display the first 5 lines of all `.log` files in a directory:
   
      head -n 5 *.log
   
5. Display the first 15 lines of a remote file using SSH:
   
      ssh user@hostname "head -n 15 /path/to/remote/file.txt"
   
Remember that the `head` command is used to display the beginning lines of a file. If you want to view the ending lines, you can use the `tail` command.


Friday, August 11, 2023

Tail Command Example in Linux | How to Use Tail Command in Linux


The tail command in Linux is used to display the last few lines of a text file. By default, it displays the last 10 lines of a file, but you can specify the number of lines you want to display using the `-n` option. Here are some examples:

1. Display the last 10 lines of a file (default behavior):
   
 tail filename.txt
   
2. Display a specific number of lines from the end of a file (e.g., last 20 lines):
   
tail -n 20 filename.txt
   
3. Display the last few lines of multiple files:
   
tail file1.txt file2.txt
   
4. Display the last 5 lines of all .log files in a directory:
   
tail -n 5 *.log
   
5. Display the last 15 lines of a remote file using SSH:
  
 ssh user@hostname "tail -n 15 /path/to/remote/file.txt"
   
6. Display the last lines of a file and continuously update as new lines are added (useful for log files):
   
tail -f filename.log
   
Remember that the tail command is used to display the ending lines of a file. If you want to view the beginning lines, you can use the `head` command.

Thursday, August 10, 2023

AWS CLI Examples on Linux | AWS CLI



  1. List all S3 buckets:

    aws s3 ls

  2. Upload a file to an S3 bucket:

    aws s3 cp local-file.txt s3://bucket-name/

  3. Download a file from an S3 bucket:
    aws s3 cp s3://bucket-name/remote-file.txt local-file.txt
  4. Sync local directory to an S3 bucket (upload only changed files): aws s3 sync local-dir s3://bucket-name/
  5. Delete a file from an S3 bucket:
    aws s3 rm s3://bucket-name/remote-file.txt
  6. List all EC2 instances:
    aws ec2 describe-instances
  7. Start an EC2 instance:
    aws ec2 start-instances --instance-ids i-1234567890abcdef0
  8. Stop an EC2 instance:
    aws ec2 stop-instances --instance-ids i-1234567890abcdef0
  9. Create an RDS database instance: 
    aws rds create-db-instance --db-instance-identifier mydbinstance --engine mysql --db-instance-class db.t2.micro --allocated-storage 20 --master-username admin --master-user-password mysecretpassword
  10. List all Lambda functions:

    aws lambda list-functions

  11. Invoke a Lambda function:

    aws lambda invoke --function-name my-function --payload '{"key": "value"}' output.json


  12. Create a new SNS topic:
    aws sns create-topic --name my-topic aws sns subscribe --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic --protocol email --notification-endpoint my-email@example.com
  13. List all SQS queues:
    aws sqs send-message --queue-url https://sqs.us-west-2.amazonaws.com/123456789012/my-queue --message-body "Hello, AWS!"

  14. Create a new DynamoDB table:
    aws dynamodb create-table --table-name my-table --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
  15. List all CloudFront distributions:
    aws cloudfront list-distributions
  16. Create a CloudFront invalidation:
    aws cloudfront create-invalidation --distribution-id E1A2B3C4D5E6F7 --paths /path1/* /path2/*
  17. List all IAM users:
    aws iam list-users
  18. Create a new IAM user: aws iam create-user --user-name my-user



Tuesday, August 8, 2023

How to Optimize PostgreSQL database server | Optimizing of a PostgreSQL database server

Optimizing a PostgreSQL database server involves several aspects, including database design, configuration settings, query optimization, and performance monitoring. Here are some steps you can take to optimize your PostgreSQL database server:


1. Database Design:

   - Ensure your database schema is well-designed with appropriate normalization and
      indexing to minimize redundant data and improve query performance.

   - Use appropriate data types for columns to minimise storage and improve query efficiency.


2. Hardware and System Resources:

   - Choose hardware that meets the performance requirements of your workload, including
     CPU, memory, and storage.

   - Use fast and reliable storage solutions (SSDs are generally preferred over HDDs) to
      reduce I/O latency.


3. Configuration Settings:

   - Adjust PostgreSQL's configuration settings in the `postgresql.conf` file to match your
     hardware and workload. Key parameters to consider include `shared_buffers`,
     `work_mem`, `effective_cache_size`, and `maintenance_work_mem`.

   - Regularly review and adjust these parameters based on your database's usage patterns and
      performance metrics.


4. Indexing:

   - Create indexes on columns that are frequently used in WHERE clauses, JOINs, and
     ORDER BY clauses.

   - Be cautious with over-indexing, as it can slow down write operations.


5. Query Optimization:

   - Write efficient queries that utilize indexes and minimize unnecessary joins and subqueries.

   - Use the `EXPLAIN` command to analyze query execution plans and identify performance
      bottlenecks.

   - Optimize slow queries by adding appropriate indexes, rewriting queries, or partitioning
     large tables.


6. Vacuum and Analyze:

   - PostgreSQL's automatic vacuum process helps manage table bloat and maintains index
      performance.

   - Regularly monitor and analyze the need for manual vacuuming based on the
      `pg_stat_user_tables` and
 `pg_stat_user_indexes` system views.


7. Connection Pooling:

   - Use a connection pooling solution to efficiently manage database connections and reduce
     the overhead of establishing new connections for each query.


8. Caching:

   - Implement caching mechanisms, such as using the built-in `pgBouncer`, to reduce the load
     on the database server and improve response times for frequently accessed data.


9. Monitoring and Tuning:

   - Use monitoring tools like `pg_stat_activity`, `pg_stat_statements`, and third-party tools like
     PgHero or pgAdmin to track database performance and identify bottlenecks.

   - Regularly analyze query performance and server metrics to make informed tuning
     decisions.


10. Backup and Maintenance:

    - Implement regular backups and test the restore process to ensure data integrity and disaster
      recovery readiness.

    - Schedule routine maintenance tasks like vacuuming, analyze, and index rebuilds during
      off-peak hours.


11. Version Updates:

    - Keep your PostgreSQL version up to date to take advantage of performance
      improvements, bug fixes, and new features.


Remember that database optimization is an ongoing process that requires continuous monitoring, analysis, and adjustments based on changing workloads and usage patterns. It's also recommended to thoroughly test any changes in a non-production environment before applying them to a live database.

Saturday, August 5, 2023

What is Linux Operating System | What is Linux

Linux is a free and open-source operating system (OS) based on the Unix family of operating systems. It was initially created by Linus Torvalds in 1991 and has since become one of the most widely used operating systems, especially in the server and embedded systems domains.



The key features of Linux include:


Open Source: Linux is distributed under various open-source licenses, which means that its source code is freely available for anyone to view, modify, and distribute. This fosters a collaborative community of developers who continuously contribute to its development and improvement.


Kernel: The Linux kernel is the core component of the operating system. It manages hardware resources and provides essential services that enable other programs to run on the system.


Distributions: Linux comes in various distributions or "distros," which are different packaged versions of the operating system. Each distribution includes the Linux kernel along with various software packages, utilities, and a package manager for easy software installation and updates. Some popular Linux distributions include Ubuntu, Debian, Fedora, CentOS, and Arch Linux.


Multiuser and Multitasking: Linux is designed to support multiple users simultaneously and enables multitasking, allowing users to run multiple applications concurrently.


Command Line Interface (CLI) and Graphical User Interface (GUI): Linux provides both a command-line interface and a graphical user interface, giving users the flexibility to choose the method they prefer for interacting with the system.


Security: Linux is known for its robust security features. It has a strong permission-based model that controls access to files and resources, reducing the risk of unauthorized access and malicious attacks.


Stability and Performance: Linux is known for its stability and performance, making it a popular choice for servers, supercomputers, and other high-performance computing systems.


Device Support: Linux has extensive hardware support, making it compatible with a wide range of devices, including computers, smartphones, servers, routers, and IoT devices.


Overall, Linux's open-source nature, versatility, and community-driven development model have made it a powerful and reliable choice for various computing needs, ranging from personal computers to enterprise-level servers.




Linux server hardening | Secure Linux Servers

 Linux server hardening is the process of securing a Linux server by reducing its attack surface and mitigating security risks. The goal is ...