Pages

Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

Friday, January 12, 2024

Chrooting for Password Reset in Linux


Introduction:

Chrooting is a process that changes the root directory for the current running process and its children. A chroot environment isolates these processes from the rest of the system. This technique can be particularly useful when you need to recover or reset a password on a system where you cannot access the usual command-line tools. Here's how you can use chroot to change the Linux root password.

Step 1: Boot from a Live CD/USB

  1. Insert a Live Linux CD/USB into your system and boot from it. Choose the "Try Linux" option instead of installing it.

Step 2: Mount the System Partition

  1. Identify and Mount the Partition:
    • First, you need to identify the partition where your Linux system is installed. You can use fdisk or lsblk to list all partitions. For instance:
      lsblk
    • Mount the system's root partition (replace /dev/sda1 with your actual partition):
      mount /dev/sda1 /mnt

Step 3: Chroot into the System

  1. Chroot into the Mounted System:
    • Change root into the mounted system:
      chroot /mnt
    • If chroot /mnt fails due to partition schema issues or if it can't find zsh or bash, try specifying the shell directly:
      chroot /mnt /bin/bash

Step 4: Change the Root Password

  1. Change the Root Password:
    • Now that you are in a chrooted environment, you can use the passwd command to change the root password:
      passwd root
    • Enter the new password twice when prompted.

Step 5: Exit and Reboot

  1. Exit Chroot and Reboot:
    • Type exit to leave the chroot environment.
    • Unmount the partition:
      umount /mnt
    • Remove the Live CD/USB and reboot your system.

Step 6: Test the New Password

  1. Test the New Password:
    • Once your system has rebooted, try logging in with the new root password to ensure that the change was successful.

Important Considerations:

  • Backup Important Data: Always ensure you have backups of any important data before performing system operations like this.
  • Correct Partition: Be absolutely sure you've mounted the correct partition before chrooting into it.
  • Live Environment: A Live Linux environment is an entire Linux distribution that can run from a removable medium like a CD/USB without installation.
  • Security: Changing the root password is a sensitive operation. Ensure that you have the authority to perform this action and that you're doing it in a secure manner.

Conclusion:

Chrooting is a powerful tool for system recovery and maintenance. By following these steps, you can reset the root password of a Linux system when you're unable to access it normally. This technique is part of a broader set of Linux skills useful for system administrators and power users. Always proceed with caution and ensure you understand the commands you're executing.

Monday, January 1, 2024

Linux - Resource Manager - Processes limitations (/etc/security/limits.conf)

In a multi-user environment, it's essential to maintain stability and ensure that no single user can consume resources to the point of affecting others or the system itself. The /etc/security/limits.conf file in Linux is a powerful tool for controlling user resource limits. This guide will explain how to use this file to limit user processes and other resources.

Understanding /etc/security/limits.conf

The /etc/security/limits.conf file allows you to set hard and soft limits for various system resources. A hard limit is the maximum value a user cannot exceed, while a soft limit is essentially a warning level.

Syntax

Each line in the /etc/security/limits.conf file has the following syntax:

<domain> <type> <item> <value>
  • <domain>: User, group (prefixed with @), or wildcard (*) for default.
  • <type>: Hard (hard) or soft (soft) limit.
  • <item>: Resource to limit (e.g., nproc for the number of processes).
  • <value>: Numerical value of the limit.

Example Entries

* hard nofile 65535 * soft nofile 4096 @student hard nproc 16384 @student soft nproc 2047 @student hard nproc 50 @student soft nproc 30

These lines set the maximum number of open files and processes for all users and specific limits for users in the student group.

Setting the Limitations

  1. Open /etc/security/limits.conf:
    Use your preferred text editor (like nano or vim) to edit the file.

  2. Modify or Add Entries:
    Based on your requirements, modify existing entries or add new ones following the syntax mentioned above.

  3. Save the File and Exit:
    Once you've made the changes, save the file and exit the editor.

  4. Restart the System (Optional):
    While most changes will apply immediately or on a new session, a restart ensures all services and users are started with the new limits.

Verifying the Limitations

To verify the limits for a particular user, switch to that user and use the ulimit command:

  • Check Soft Limit for File Descriptors:
    ulimit -Sn
  • Check Hard Limit for File Descriptors:
    ulimit -Hn
  • Check Soft Limit for User Processes:
    ulimit -Su
  • Check Hard Limit for User Processes:
    ulimit -Hu

Testing the Limitations

The infamous fork bomb :(){ :|:& };: is a bash function that recursively creates copies of itself. It's often used to test process limitations. Warning: This script can make your system unresponsive. Use it only in a controlled environment.

  1. Ensure you're in a safe test environment.
  2. Run the Fork Bomb:
    :(){ :|:& };:
  3. Observe the Behavior:
    The system should prevent the script from creating processes beyond the set limit.

Conclusion

Correctly setting user limits is a critical task for system administrators to ensure a stable and fair environment for all users. By configuring the /etc/security/limits.conf file, you can prevent individual users from over-consuming resources and maintain the overall health of the system. Always test changes in a controlled environment before applying them to a production system.