332.3 Resource Control (weight: 3)

a bunch of mountain sheep being stable

Tech Tutorial: 332.3 Resource Control (weight: 3) #

Exam Objective: #

The aim of this tutorial is to equip you with the knowledge to manage and control system resources in a Linux environment. This includes setting limits for user processes, managing kernel resources, and controlling system services using various utilities.

Key Knowledge Areas: #

  • Understanding and managing user and system limits.
  • Configuring and managing kernel parameters.
  • Using tools to control system resource allocation.

Utilities: #

  • ulimit
  • sysctl
  • systemctl

Introduction #

In a Linux system, controlling resources effectively is crucial for maintaining system stability, security, and performance. This tutorial will delve into how to use ulimit, sysctl, and systemctl to manage and restrict resources for users and system processes.

Step-by-Step Guide #

1. Managing User Limits with ulimit #

The ulimit utility is used to control the resources available to the shell and to processes started by it. You can set limits on various resources like CPU time, file sizes, and number of processes.

Detailed Code Examples: #

Viewing Current Limits

ulimit -a

Setting a Specific Limit

  • Set the maximum file size to 1000000 blocks:
ulimit -f 1000000
  • Set the maximum number of open file descriptors:
ulimit -n 1024

Applying Limits for a Specific Session

ulimit -u 50  # maximum user processes
ulimit -t 60  # CPU time in seconds

2. Configuring Kernel Parameters with sysctl #

sysctl is used to modify kernel parameters at runtime. Parameters are found under /proc/sys/ and can be both viewed and set using sysctl.

Detailed Code Examples: #

Viewing a Kernel Parameter

sysctl vm.swappiness

Setting a Kernel Parameter

sudo sysctl -w vm.swappiness=10

Persistent Configuration

Edit or create /etc/sysctl.conf and add:

vm.swappiness = 10

Then run:

sudo sysctl -p

3. Controlling System Services with systemctl #

systemctl is used to examine and control the systemd system and service manager.

Detailed Code Examples: #

Start a Service

sudo systemctl start nginx

Stop a Service

sudo systemctl stop nginx

Enable a Service at Boot

sudo systemctl enable nginx

Check Status of a Service

sudo systemctl status nginx

Restart a Service

sudo systemctl restart nginx

Conclusion #

In this tutorial, we have covered how to use ulimit, sysctl, and systemctl for effective resource control in Linux. Understanding these utilities allows you to manage system resources proactively, ensuring that your Linux systems run efficiently and reliably. Whether you are limiting user process resources with ulimit, adjusting kernel parameters via sysctl, or managing service states with systemctl, these tools are indispensable for system administrators.