Ask Your Question

How to reference a user's home directory?

asked 2014-02-17 11:44:39 -0500

mikek gravatar image

updated 2014-02-18 09:17:05 -0500

This seems like a simple question, but I'm trying to write a ~/.netrc file in a user's home directory, but I can't figure out how to find out where $HOME is. Assuming /home/$user is not a good idea for users like 'backup' on Ubuntu, or even /root has a different directory than everyone else.

Google tells me that this might not be so simple... what's the accepted practice to do this?

I applied some of Stephan's answer to create an array-like fact:

# Create a fact of ,user:homedir,user2:homedir2, (note leading and trailing ...
edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2014-02-17 15:38:11 -0500

Stefan gravatar image

updated 2014-02-17 15:39:44 -0500

You are facing problems here because the desired state of your machine (presence of a netrc file in the user's homedirectory) depends on the current state of the machine (the location of the homedirectory). The only way to get the current state of the agent is through facts. So you basically have two options here:

  1. Manage the user with puppet directly and use the home property to explicitly set the homedirectory of the specified user. In the long run this often turns out to be easier to understand and to manage.
  2. Define a custom fact that returns the user ...
edit flag offensive delete link more


Is there a way to reference the username as a parameter? e.g., the equivalent of the imaginary ${::homedir_${user}} ?

mikek gravatar imagemikek ( 2014-02-18 07:59:50 -0500 )edit

Come on!! ..See my Post ! My solution will to find the TTY7 user!!...the logged in User! It's difficult to have more than one user logged in... With this ...(more)

cristaldo gravatar imagecristaldo ( 2014-02-18 14:00:40 -0500 )edit

@milek, here is how you can solve this: `` $home_var = "homedir_${user}" $home = inline_template("<%= scope.lookupvar(@home_var) %>") ``

grundic gravatar imagegrundic ( 2014-10-07 12:43:49 -0500 )edit

answered 2014-02-17 12:09:44 -0500

Not sure about the 'accepted practice', but you could use the tilde expansion property of the shell. In other words, execute

sh -c 'echo ~auser'

where auser of course is an example user.

edit flag offensive delete link more


Putting it in an exec looks slick enough. Maybe I could put it in an 'if' with spuder's answer to save on exec statements.

mikek gravatar imagemikek ( 2014-02-17 13:40:51 -0500 )edit

answered 2014-02-17 12:19:07 -0500

spuder gravatar image

updated 2014-02-17 12:20:12 -0500

Since puppet runs execute as root, you can't use the $HOME variable to find a users home directory.

The way I've approached this is to use a variable in my manifests that points to the users home directory. I modify the module parameter as needed.

  $git_home               = '/home/git'

  #Verify .ssh directory exists
  file { "${gitlab::git_home}/.ssh":
    ensure  =>  directory,
    mode    =>  '0700',    
edit flag offensive delete link more


This is for your manifest for your user's account? i.e, will work for Puppet managed users, but any built-ins which aren't puppetized would need separate handling?

mikek gravatar imagemikek ( 2014-02-17 13:36:49 -0500 )edit

This manifest creates the user 'git', if you have a user that already exists, you will need to know their home directory (no way to have puppet figure it out)

spuder gravatar imagespuder ( 2014-02-17 15:24:06 -0500 )edit

Come on!! ..See my Post ! My solution will to find the TTY7 user!!...the logged in User! It's difficult to have more than one user logged in... With this ...(more)

cristaldo gravatar imagecristaldo ( 2014-02-18 14:05:19 -0500 )edit

I don't know how to organize text's here...

cristaldo gravatar imagecristaldo ( 2014-02-18 14:06:27 -0500 )edit

answered 2014-02-17 13:47:44 -0500

cristaldo gravatar image


I had a similar problem, but my users are not from posix account!, my users are from a domain controller!

So, i have to build a Custom Fact to resolve my problem! Because I have more than 500 Desktops, and it was dificult to know what is the user of eatch machine.

My custom fact named as who.rb

mkdir -p /etc/puppet/modules/myfacts/lib/facter/who.rb

cat /etc/puppet/modules/myfacts/lib/facter/who.rb require 'facter' Facter.add("who") do setcode do %x{who |egrep 'tty7' | cut -d ' ' -f1}.chomp end end

export RUBYLIB

export ... (more)

edit flag offensive delete link more


The fact seems to be designed to find the name of a user logged into the GUI, then who.pp assumes the home directory is in /home/$user . Correct?

mikek gravatar imagemikek ( 2014-02-19 04:13:29 -0500 )edit

exact!! With this Custom Fact, we can discovery the NAME of the user logged into the GUI (TTY7), so the HOMES DIRECTORY is /home/${who} :) You can change the name ...(more)

cristaldo gravatar imagecristaldo ( 2014-02-19 06:06:25 -0500 )edit

It's a cool way to find the name of the logged-in user, but the real problem is that I can't assume their home directory is "/home/$user".

mikek gravatar imagemikek ( 2014-02-19 08:12:58 -0500 )edit

Mikek....usually the user's home is the SAME as the user login! I use LDAP with a domain controller, and this ir a rule.... You create a user, set ...(more)

cristaldo gravatar imagecristaldo ( 2014-02-20 04:19:56 -0500 )edit

I have a way to manage ALL the home's directories too...but the problem is to set the permissions when you need to put files there.

cristaldo gravatar imagecristaldo ( 2014-02-20 04:21:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2014-02-17 11:44:39 -0500

Seen: 5,135 times

Last updated: Feb 18 '14