This is the development version of Kanrisuru. Since this is still a pre-1.0 release, we don't recommend using this in system-critical production environments.

Kanrisuru

create_user

Create a user in ruby.
linux manual

Basic Usage

require 'kanrisuru'
host = Kanrisuru::Remote::Host.new(host: '127.0.1.1', username: 'ubuntu', keys: ['~/.ssh/id_rsa'])

result = host.create_user('kanrisuru', 
  shell: '/bin/bash', 
  uid: 1005, 
  groups: ['ubuntu', 'sudo', 'admin']
)

result.success?
true

result.user
'kanrisuru'

result.uid
1005

Parameters

Field Type Description
user string
Required name of the user to create.
uid integer
The numerical value of the user's ID. This value must be unique, unless the non_unique option is used.
group string
The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already existing group.
groups array
A list of supplementary groups which the user is also a member of.
shell string
The name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd, or an empty string by default.
non_unique boolean
Allow the creation of a user account with a duplicate (non-unique) UID.
system boolean
Create a system account.
home string
The new user will be created using HOME_DIR as the value for the user's login directory. The default is to append the LOGIN name to BASE_DIR and use that as the login directory name.
createhome boolean
Create the user's home directory if it does not exist.
skeleton string
The skeleton directory, which contains files and directories to be copied in the user's home directory, when the home directory is created.
password string
The encrypted password, as returned by crypt(3). The default is to disable the password.
expires string
The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.

Result

Returns a single User struct.

User Fields

Field Type Description
uid integer The numerical value of the user's ID.
name string The name of the user
shell Kanrisuru::Core::User::FilePath The name of the user's login shell.
home Kanrisuru::Core::User::FilePath Location of the users home directory.
groups array Array of UserGroup structs, that define the all the groups a user belongs to.

Return Example

result = host.create_user('kanrisuru002', shell: '/bin/zsh', uid: 9100, group: 'kanrisuru')
result.data
#<Struct:Kanrisuru::Core::User::User:0x000008c0
  groups = [
    [0] #<Struct:Kanrisuru::Core::User::UserGroup:0x00000910
      gid = 1005,
      name = "kanrisuru"
    >
  ],
  home = #<Struct:Kanrisuru::Core::User::FilePath:0x00000960
    path = "/home/kanrisuru002"
  >,
  name = "kanrisuru002",
  shell = #<Struct:Kanrisuru::Core::User::FilePath:0x000009d8
    path = "/bin/zsh"
  >,
  uid = 9100
>

Exit Status

Code Description
0 Success
1 Can't update password file
2 Invalid command syntax
3 Invalid argument to option
4 UID already in use (and no -o)
6 Specified group doesn't exist
9 Username already in use
10 Can't update group file
12 Can't create home directory
14 Can't update SELinux user mapping

Tested On

  • Ubuntu, Debian, Centos, Fedora, Redhat, OpenSuse, SLES