search

Home  >  Q&A  >  body text

ubuntu - 用puppet创建用户, 无法指定密码

已经试过在ubuntu上安装 puppet libshadow-ruby1.8

class users {
  package { "libshadow-ruby1.8":
    ensure => latest,
    require => Exec['apt-get update']
  }
  user { "ubuntu":
    ensure  => present,
    uid     => '1001',
    gid     => 'admin',
    shell   => '/bin/bash',
    password => sha1('ubuntu'),
    managehome => true,
    require => Package['libshadow-ruby1.8']
  }
}

include users

这么写用户名是创建了, 但是密码还是没有

ps: 其实我是在用vagrant, vagrant是默认用户名想换成ubuntu

PHP中文网PHP中文网2893 days ago464

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-21 10:57:18

    When you say "there is still no password", do you mean that the verification failed when you entered the password "ubuntu"? Or is there no ubuntu record in the shadow file at all?

    I guess it’s the first one (verification failed). The password in the shadow file doesn’t seem to be generated by sha1(). I don’t know where you saw this method.

    The solutions I thought of are:

    Use the password copied from the shadow file in the puppet class

    First create a user in the terminal using useradd, passswd, and set the password to ubuntu, as shown in the picture:

    Then go to the shadow file and copy the encrypted password, and use this puppet code to create a user:

      user { "ubuntu":
        ensure  => present,
        uid     => '1001',
        gid     => 'admin',
        shell   => '/bin/bash',
        password => '$mEUouB2K$zCgsWNuI0KhUvHbTcv6bV1UV2gx/VFB390zVOZ37lukr2PI7b4RKNkvpHaMRoG4zCqLvP0dN4p6uIi0xEZeRJ0',
        managehome => true,
        require => Package['libshadow-ruby1.8']
      }
    Write a bash script and use the expect and passwd commands to set the password for the new user

    Since the passwd command receives the password directly from stdin, the password cannot be set through command line parameters such as passwd ubuntu ubuntu, so the workaround is Write a bash shell yourself (assuming it's called set_passwd.sh). This shell script receives the plaintext password from the command line, and then uses expect to send it to the passwd command
    In this way, your puppet script is as follows:

      user { "ubuntu":
        ensure  => present,
        uid     => '1001',
        gid     => 'admin',
        shell   => '/bin/bash',
        managehome => true,
        require => Package['libshadow-ruby1.8'],
        notify  => Exec['change_pass']
      }
      
      exec {'change_pass':
          command => '/bin/sh /root/set_passwd.sh ubuntu[用户名] ubuntu[密码]',
          require => File['/root/set_passwd.sh']
      }

    reply
    0
  • Cancelreply