Operator Libs Linux

  • Jon Seager
Channel Revision Published Runs on
latest/stable 2 09 Mar 2023
Ubuntu 22.04 Ubuntu 20.04
latest/stable 1 28 Oct 2021
Ubuntu 22.04 Ubuntu 20.04
juju deploy operator-libs-linux
Show information

Platform:

Ubuntu
22.04 20.04

charms.operator_libs_linux.v0.passwd

Simple library for managing Linux users and groups.

The passwd module provides convenience methods and abstractions around users and groups on a Linux system, in order to make adding and managing users and groups easy.

Example of adding a user named 'test':

import passwd
passwd.add_group(name='special_group')
passwd.add_user(username='test', secondary_groups=['sudo'])

if passwd.user_exists('some_user'):
    do_stuff()

def user_exists(user)

Check if a user exists.

Arguments

user

username or gid of user whose existence to check

def group_exists(group)

Check if a group exists.

Arguments

group

username or gid of user whose existence to check

def add_user(
    username: str,
    password,
    shell: str,
    system_user: bool,
    primary_group: str,
    secondary_groups,
    uid: int,
    home_dir: str,
    create_home: bool
)

Add a user to the system.

Arguments

username

Username to create

password

Password for user; if None, create a system user

shell

The default shell for the user

system_user

Whether to create a login or system user

primary_group

Primary group for user; defaults to username

secondary_groups

Optional list of additional groups

uid

UID for user being created

home_dir

Home directory for user

create_home

Force home directory creation

Returns

The password database entry struct, as returned by pwd.getpwnam

Description

Will log but otherwise succeed if the user already exists.

def add_group(
    group_name: str,
    system_group: bool,
    gid: int
)

Add a group to the system.

Arguments

group_name

group to create

system_group

Create system group

gid

GID for user being created

Returns

The group's password database entry struct, as returned by grp.getgrnam

Description

Will log but otherwise succeed if the group already exists.

def add_user_to_group(
    username: str,
    group: str
)

Add a user to a group.

Arguments

username

user to add to specified group

group

name of group to add user to

Returns

The group's password database entry struct, as returned by grp.getgrnam

def remove_user(
    user,
    remove_home: bool
)

Remove a user from the system.

Arguments

user

the username or uid of the user to remove

remove_home

indicates whether the user's home directory should be removed

def remove_group(
    group,
    force: bool
)

Remove a user from the system.

Arguments

group

the name or gid of the group to remove

force

force group removal even if it's the primary group for a user