---
title: Charmhub | Deploy Operator Libs Linux using Charmhub - The Open Operator Collection
description: Deploy the latest version of Operator Libs Linux on any cloud.
url: https://charmhub.io/operator-libs-linux/libraries/sysctl
---

# Operator Libs Linux

[Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

* [Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

Platform:

22.04

20.04

stable 2

```
juju deploy operator-libs-linux
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/operator-libs-linux/libraries/sysctl#drawer)

## charms.operator\_libs\_linux.v0.sysctl

* [*Docstrings*Docstrings](https://charmhub.io/operator-libs-linux/libraries/sysctl)
  [*Code*Source code](https://charmhub.io/operator-libs-linux/libraries/sysctl/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.operator_libs_linux.v0.sysctl
    ```

    [Download sysctl.py](https://charmhub.io/operator-libs-linux/libraries/sysctl/download)
  + *Last updated* 13 Feb 2026
  + *Revision* Library version 0.5

Legacy Charmhub-hosted sysctl library, deprecated in favour of `charmlibs.sysctl`.

WARNING: This library is deprecated and will no longer receive feature updates or bugfixes.
`charmlibs.sysctl` version 1.0 is a bug-for-bug compatible migration of this library.
Add 'charmlibs-sysctl~=1.0' to your charm's dependencies, and remove this Charmhub-hosted library.
Then replace `from charms.operator_libs_linux.v0 import sysctl` with
`from charmlibs import sysctl`.
Read more:

* https://documentation.ubuntu.com/charmlibs
* https://pypi.org/project/charmlibs-sysctl

---

Handler for the sysctl config.

This library allows your charm to create and configure sysctl options to the machine.

Validation and merge capabilities are added, for situations where more than one application
are setting values. The following files can be created:

* /etc/sysctl.d/90-juju-
  Requirements from one application requesting to configure the values.
* /etc/sysctl.d/95-juju-sysctl.conf
  Merged file resulting from all other `90-juju-*` application files.

A charm using the sysctl lib will need a data structure like the following:

```
{
"vm.swappiness": "1",
"vm.max_map_count": "262144",
"vm.dirty_ratio": "80",
"vm.dirty_background_ratio": "5",
"net.ipv4.tcp_max_syn_backlog": "4096",
}
```

Now, it can use that template within the charm, or just declare the values directly:

```
from charms.operator_libs_linux.v0 import sysctl

class MyCharm(CharmBase):

    def __init__(self, *args):
        ...
        self.sysctl = sysctl.Config(self.meta.name)

        self.framework.observe(self.on.install, self._on_install)
        self.framework.observe(self.on.remove, self._on_remove)

    def _on_install(self, _):
        # Alternatively, read the values from a template
        sysctl_data = {"net.ipv4.tcp_max_syn_backlog": "4096"}}

        try:
            self.sysctl.configure(config=sysctl_data)
        except (sysctl.ApplyError, sysctl.ValidationError) as e:
            logger.error(f"Error setting values on sysctl: {e.message}")
            self.unit.status = BlockedStatus("Sysctl config not possible")
        except sysctl.CommandError:
            logger.error("Error on sysctl")

    def _on_remove(self, _):
        self.sysctl.remove()
```

---

Index

* [class Error](https://charmhub.io/operator-libs-linux/libraries/sysctl#error)
* + [def message(
    self)](https://charmhub.io/operator-libs-linux/libraries/sysctl#error-message)
* [class CommandError](https://charmhub.io/operator-libs-linux/libraries/sysctl#commanderror)
* [class ApplyError](https://charmhub.io/operator-libs-linux/libraries/sysctl#applyerror)
* [class ValidationError](https://charmhub.io/operator-libs-linux/libraries/sysctl#validationerror)
* [class Config](https://charmhub.io/operator-libs-linux/libraries/sysctl#config)
* + [def \_\_init\_\_(
    self,
    name)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-__init__)
* + [def \_\_contains\_\_(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-__contains__)
* + [def \_\_len\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-__len__)
* + [def \_\_iter\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-__iter__)
* + [def \_\_getitem\_\_(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-__getitem__)
* + [def charm\_filepath(
    self)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-charm_filepath)
* + [def configure(
    self,
    config)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-configure)
* + [def remove(
    self)](https://charmhub.io/operator-libs-linux/libraries/sysctl#config-remove)

#### class Error

Description

Base class of most errors raised by this library. None

Methods

Error.
message(

*self*
)

Description

Return the message passed as an argument. None

#### class CommandError

Description

Raised when there's an error running sysctl command. None

#### class ApplyError

Description

Raised when there's an error applying values in sysctl. None

#### class ValidationError

Description

Exception representing value validation error. None

#### class Config

Description

Represents the state of the config that a charm wants to enforce. None

Methods

Config.
\_\_init\_\_(

*self*

,
name: str
)

Config.
\_\_contains\_\_(

*self*

,
key: str
)

Description

Check if key is in config. None

Config.
\_\_len\_\_(

*self*
)

Description

Get size of config. None

Config.
\_\_iter\_\_(

*self*
)

Description

Iterate over config. None

Config.
\_\_getitem\_\_(

*self*

,
key: str
)

Description

Get value for key form config. None

Config.
charm\_filepath(

*self*
)

Description

Name for resulting charm config file. None

Config.
configure(

*self*

,
config
)

Configure sysctl options with a desired set of params.

Arguments

config

dictionary with keys to configure:

Config.
remove(

*self*
)

Remove config for charm.

Description

The removal process won't apply any sysctl configuration. It will only merge files from
remaining charms.
