2014-08-24 18:18:18 +01:00
|
|
|
|
<chapter xmlns="http://docbook.org/ns/docbook"
|
|
|
|
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
|
|
|
|
xmlns:xi="http://www.w3.org/2001/XInclude"
|
|
|
|
|
version="5.0"
|
|
|
|
|
xml:id="sec-user-management">
|
2018-05-02 00:57:09 +01:00
|
|
|
|
<title>User Management</title>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
NixOS supports both declarative and imperative styles of user management. In
|
|
|
|
|
the declarative style, users are specified in
|
|
|
|
|
<filename>configuration.nix</filename>. For instance, the following states
|
|
|
|
|
that a user account named <literal>alice</literal> shall exist:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-17 00:19:55 +01:00
|
|
|
|
<xref linkend="opt-users.users"/>.alice = {
|
2020-08-23 18:11:40 +01:00
|
|
|
|
<link linkend="opt-users.users._name_.isNormalUser">isNormalUser</link> = true;
|
|
|
|
|
<link linkend="opt-users.users._name_.home">home</link> = "/home/alice";
|
|
|
|
|
<link linkend="opt-users.users._name_.description">description</link> = "Alice Foobar";
|
|
|
|
|
<link linkend="opt-users.users._name_.extraGroups">extraGroups</link> = [ "wheel" "networkmanager" ];
|
|
|
|
|
<link linkend="opt-users.users._name_.openssh.authorizedKeys.keys">openssh.authorizedKeys.keys</link> = [ "ssh-dss AAAAB3Nza... alice@foobar" ];
|
2018-04-17 00:19:55 +01:00
|
|
|
|
};
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Note that <literal>alice</literal> is a member of the
|
|
|
|
|
<literal>wheel</literal> and <literal>networkmanager</literal> groups, which
|
|
|
|
|
allows her to use <command>sudo</command> to execute commands as
|
|
|
|
|
<literal>root</literal> and to configure the network, respectively. Also note
|
|
|
|
|
the SSH public key that allows remote logins with the corresponding private
|
|
|
|
|
key. Users created in this way do not have a password by default, so they
|
|
|
|
|
cannot log in via mechanisms that require a password. However, you can use
|
|
|
|
|
the <command>passwd</command> program to set a password, which is retained
|
|
|
|
|
across invocations of <command>nixos-rebuild</command>.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
If you set <xref linkend="opt-users.mutableUsers"/> to false, then the
|
|
|
|
|
contents of <literal>/etc/passwd</literal> and <literal>/etc/group</literal>
|
|
|
|
|
will be congruent to your NixOS configuration. For instance, if you remove a
|
|
|
|
|
user from <xref linkend="opt-users.users"/> and run nixos-rebuild, the user
|
|
|
|
|
account will cease to exist. Also, imperative commands for managing users and
|
|
|
|
|
groups, such as useradd, are no longer available. Passwords may still be
|
|
|
|
|
assigned by setting the user's
|
2020-08-23 18:11:40 +01:00
|
|
|
|
<link linkend="opt-users.users._name_.hashedPassword">hashedPassword</link>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
option. A hashed password can be generated using <command>mkpasswd -m
|
2020-10-26 02:22:17 +00:00
|
|
|
|
sha-512</command>.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
A user ID (uid) is assigned automatically. You can also specify a uid
|
|
|
|
|
manually by adding
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2019-06-17 11:01:51 +01:00
|
|
|
|
uid = 1000;
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
to the user specification.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
Groups can be specified similarly. The following states that a group named
|
|
|
|
|
<literal>students</literal> shall exist:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<programlisting>
|
2018-04-05 09:43:56 +01:00
|
|
|
|
<xref linkend="opt-users.groups"/>.students.gid = 1000;
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</programlisting>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
As with users, the group ID (gid) is optional and will be assigned
|
|
|
|
|
automatically if it’s missing.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
In the imperative style, users and groups are managed by commands such as
|
|
|
|
|
<command>useradd</command>, <command>groupmod</command> and so on. For
|
|
|
|
|
instance, to create a user account named <literal>alice</literal>:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<screen>
|
2020-09-22 23:38:47 +01:00
|
|
|
|
<prompt># </prompt>useradd -m <replaceable>alice</replaceable></screen>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
To make all nix tools available to this new user use `su - USER` which opens
|
|
|
|
|
a login shell (==shell that loads the profile) for given user. This will
|
|
|
|
|
create the ~/.nix-defexpr symlink. So run:
|
2015-02-28 14:30:06 +00:00
|
|
|
|
<screen>
|
2020-09-22 23:38:47 +01:00
|
|
|
|
<prompt># </prompt>su - <replaceable>alice</replaceable> -c "true"</screen>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
The flag <option>-m</option> causes the creation of a home directory for the
|
|
|
|
|
new user, which is generally what you want. The user does not have an initial
|
|
|
|
|
password and therefore cannot log in. A password can be set using the
|
|
|
|
|
<command>passwd</command> utility:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<screen>
|
2020-09-22 23:38:47 +01:00
|
|
|
|
<prompt># </prompt>passwd <replaceable>alice</replaceable>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
Enter new UNIX password: ***
|
|
|
|
|
Retype new UNIX password: ***
|
|
|
|
|
</screen>
|
2018-05-02 00:57:09 +01:00
|
|
|
|
A user can be deleted using <command>userdel</command>:
|
2014-08-24 18:18:18 +01:00
|
|
|
|
<screen>
|
2020-09-22 23:38:47 +01:00
|
|
|
|
<prompt># </prompt>userdel -r <replaceable>alice</replaceable></screen>
|
2019-09-19 18:17:30 +01:00
|
|
|
|
The flag <option>-r</option> deletes the user’s home directory. Accounts
|
|
|
|
|
can be modified using <command>usermod</command>. Unix groups can be managed
|
|
|
|
|
using <command>groupadd</command>, <command>groupmod</command> and
|
|
|
|
|
<command>groupdel</command>.
|
2018-05-02 00:57:09 +01:00
|
|
|
|
</para>
|
2014-08-24 18:18:18 +01:00
|
|
|
|
</chapter>
|