Kanrisuru

chown

Change a file owner and group in ruby
linux manual

Basic Usage

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

host.stat("~/test.txt").user
"ubuntu"

host.stat("~/test.txt").group
"ubuntu"

host.su("root")

result = host.chown("/home/ubuntu/test.txt", owner: "root", group: "root")
result.success?
true

result.user
"root"

result.uid
0

result.group
"root"

result.gid
0

This command needs escalated privileges to make changes to certain files and directory ownership.

Parameters

Field Type Description
path string
Required location of file / directory to update the owner and group to.
owner string
integer
Must either have a owner (uid) present and/or the group (gid) to make changes
group string
integer
Must either have a owner (uid) present and/or the group (gid) to make changes
recursive boolean
If path is a directory, recursively update all sub-directories and files to the specified owner / group.

Result

Returns a single FileStat struct.

FileStat Fields

Field Type Description
mode Kanrisuru::Mode The mode bits set for the file / directory.
blocks integer The number of blocks allocated for the file.
device string The major and minor device number combined into one value (in hex and decimal) of the device on which the file resides.
file_type string What's the file type, ie: directory, file, block_device, char_device, symlink
gid integer The group id in which the file belongs to.
group string The group name in which the file belongs to.
links integer Number of hard links pointing to the underlying inode.
inode integer Unique inode value assigned to the file
path string Where is the file located at
fsize integer Size of the file (in bytes)
uid integer The user id that owns the file
user string The user that owns the file
last_access DateTime When was the last time the file was accessed.
last_modified DateTime When was the last time the file was updated.
last_change DateTime When was the last time the file was changed.

Return Example

host.su("root")
host.chown("/home/ubuntu/directory", owner: 0, group: 0, recursive: true).data
#<Struct:Kanrisuru::Core::Stat::FileStat:0x000006b8
  fsize = 4096,
  blocks = 8,
  device = "fc01",
  file_type = "directory",
  gid = 0,
  group = "root",
  inode = 371157,
  last_access = #<DateTime: 2021-08-25T12:17:55-05:00 ((2459452j,62275s,570593100n),-18000s,2299161j)>,
  last_changed = #<DateTime: 2021-08-25T12:17:55-05:00 ((2459452j,62275s,570593100n),-18000s,2299161j)>,
  last_modified = #<DateTime: 2021-08-25T12:17:23-05:00 ((2459452j,62243s,142213183n),-18000s,2299161j)>,
  links = 2,
  mode = #<Kanrisuru::Mode:0x1000 @numeric=775 @symbolic=drwxrwxr-x>,
  path = "/home/ubuntu/directory",
  uid = 0,
  user = "root"
>

Exit Status

Code Description
0 Success
1 Failure

Tested On

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