How to add an user and re set the root user in yoc

2019-05-31 21:39发布

问题:

I like to do some things for the build-in users of my yocto project:

1.) set a password for root to "abc"

2.) set the root shell for ssh login form /bin/sh to /bin/bash

3.) add the user "customUser" with password "xyz"

Think a simple recipe can do this. So far I tried @ myUser.bb:

SUMMARY = "admin + user"
SECTION = "USR"
LICENSE = "CLOSED"

inherit extrausers useradd

# how to
# pw: abc
# at bash: usermod -p $(openssl passwd abc) root
# get a salted hash: openssl passwd abc
# one possible result: 1Cw5PHLy76ps2
# the command now looks: usermod -p 1Cw5PHLy76ps2 root

# set image root password
EXTRA_USERS_PARAMS = "usermod -p 1Cw5PHLy76ps2 root;"

USERADD_PACKAGES = "${PN}"

# password
# "xyz"
# openssl passwd xyz
# result: y5UyLBO4GNAwc

USERADD_PARAM_${PN} = "-u 1200 -d /home/customUser -r -s /bin/bash -p y5UyLBO4GNAwc customUser"

do_install_append () {
    install -d -m 755 ${D}${datadir}/customUser

    # The new users and groups are created before the do_install
    # step, so you are now free to make use of them:
    chown -R customUser ${D}${datadir}/customUser

    # groups
    # chgrp -R group1 ${D}${datadir}/customUser
}

FILES_${PN} = "${datadir}/*"

#ALLOW_EMPTY_${PN} = "1"

Any idea how to get this done?

回答1:

I took your example and made two small changes to get it to work.

First, I removed inherit extrauser, this isn't necessary when working with useradd. That made bitbaking the recipe fail; the username was invalid. I changed the username to custom, and everything builds fine.

When inspecting the resulting myuser_1.0-r0.0_armv5e.ipk, I can see that there are a preinstall script in myuser_1.0-r0.0_armv5e.ipk/control.tar.gz/preinst that will create your user.



回答2:

You can use EXTRA_USERS_PARAMS global in your main recipe.

inherit extrausers
EXTRA_USERS_PARAMS = " useradd customUser1; \
                       useradd customUser2; \
                       usermod  -p 'Password_1' customUser1; \
                       usermod  -p 'Password_2' customUser2; \
                       usermod  -a -G sudo customUser1; \
                       usermod  -a -G sudo customUser2;"