Antarr Byrd
Antarr Byrd

Reputation: 26061

Chef doesn't create home directory for user

I have a chef recipe to create a deploy user. The user is being created when running kitchen converge. When trying to create the .ssh folder for the user it fails with because the home directory for the user does not exists. Parent directory /home/deploy does not exist, cannot create /home/deploy/.ssh.

cookbooks/main/recipes/user.rb

user deploy do
  action :create
  comment 'Application deploy user'
  home "/home/#{node['deploy_user']}"
  shell '/bin/bash'
  system true
  supports manage_home: true
end

directory "/home/#{node['deploy_user']}/.ssh" do
  mode 0700
  owner node['deploy_user']
  group node['deploy_user']
end

template "/home/#{node['deploy_user']}/.ssh/authorized_keys" do
  mode 0600
  owner node['deploy_user']
  source 'authorized_keys.erb'
end

.kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: chef_solo

platforms:
  - name: ubuntu-14.04
  - name: centos-7.1

suites:
  - name: default
    run_list:
      - recipe[main::default]
    attributes:

Upvotes: 3

Views: 4391

Answers (4)

user1883857
user1883857

Reputation: 41

This infuriated me to no end as well. No excuse for Chef not to make such a simple routine action easy to perform.

As this is a top google search and I'm not clear the other answers are proper, here is exactly what I needed to run to get this to work. I'm using chef server 12.4 and client 12.10.24. All on Ubuntu 14.04.

user '<USERNAME>' do
  gid '<MY_GROUP_NAME>'
  shell '/bin/bash'
  comment 'some stuff i want to say'
  home "/home/<USERNAME>"
  supports manage_home: true
  action :create
end

My /etc/login.defs file is unmodified default.

Upvotes: 3

display name
display name

Reputation: 4185

Perhaps it's the run sequence problem. Try

user node['deploy_user'] do
  comment 'Application deploy user'
  home "/home/#{node['deploy_user']}"
  shell '/bin/bash'
  system true
  manage_home true
end.run_action(:create)

Upvotes: 0

coderanger
coderanger

Reputation: 54191

From man useradd:

-r, --system
    Create a system account.

    System users will be created with no aging information in /etc/shadow, and their numeric identifiers are choosen in the SYS_UID_MIN-SYS_UID_MAX range, defined in /etc/login.defs, instead of UID_MIN-UID_MAX (and their GID counterparts for the creation of groups).

    Note that useradd will not create a home directory for such an user, regardless of the default setting in /etc/login.defs (CREATE_HOME). You have to specify the -m options if you want a home directory for a system account to be created.

Or in short, add manage_home true to your resource.

Upvotes: 0

zuazo
zuazo

Reputation: 5738

You passed deploy to the user resource name instead of node['deploy_user']:

user node['deploy_user'] do
  action :create
  comment 'Application deploy user'
  home "/home/#{node['deploy_user']}"
  shell '/bin/bash'
  system true
  supports manage_home: true
end

Upvotes: 1

Related Questions