1# User Management {#sec-user-management}
2
3NixOS supports both declarative and imperative styles of user
4management. In the declarative style, users are specified in
5`configuration.nix`. For instance, the following states that a user
6account named `alice` shall exist:
7
8```nix
9users.users.alice = {
10 isNormalUser = true;
11 home = "/home/alice";
12 description = "Alice Foobar";
13 extraGroups = [ "wheel" "networkmanager" ];
14 openssh.authorizedKeys.keys = [ "ssh-dss AAAAB3Nza... alice@foobar" ];
15};
16```
17
18Note that `alice` is a member of the `wheel` and `networkmanager`
19groups, which allows her to use `sudo` to execute commands as `root` and
20to configure the network, respectively. Also note the SSH public key
21that allows remote logins with the corresponding private key. Users
22created in this way do not have a password by default, so they cannot
23log in via mechanisms that require a password. However, you can use the
24`passwd` program to set a password, which is retained across invocations
25of `nixos-rebuild`.
26
27If you set [](#opt-users.mutableUsers) to
28false, then the contents of `/etc/passwd` and `/etc/group` will be congruent
29to your NixOS configuration. For instance, if you remove a user from
30[](#opt-users.users) and run nixos-rebuild, the user
31account will cease to exist. Also, imperative commands for managing users and
32groups, such as useradd, are no longer available. Passwords may still be
33assigned by setting the user\'s
34[hashedPassword](#opt-users.users._name_.hashedPassword) option. A
35hashed password can be generated using `mkpasswd`.
36
37A user ID (uid) is assigned automatically. You can also specify a uid
38manually by adding
39
40```nix
41uid = 1000;
42```
43
44to the user specification.
45
46Groups can be specified similarly. The following states that a group
47named `students` shall exist:
48
49```nix
50users.groups.students.gid = 1000;
51```
52
53As with users, the group ID (gid) is optional and will be assigned
54automatically if it's missing.
55
56In the imperative style, users and groups are managed by commands such
57as `useradd`, `groupmod` and so on. For instance, to create a user
58account named `alice`:
59
60```ShellSession
61# useradd -m alice
62```
63
64To make all nix tools available to this new user use \`su - USER\` which
65opens a login shell (==shell that loads the profile) for given user.
66This will create the \~/.nix-defexpr symlink. So run:
67
68```ShellSession
69# su - alice -c "true"
70```
71
72The flag `-m` causes the creation of a home directory for the new user,
73which is generally what you want. The user does not have an initial
74password and therefore cannot log in. A password can be set using the
75`passwd` utility:
76
77```ShellSession
78# passwd alice
79Enter new UNIX password: ***
80Retype new UNIX password: ***
81```
82
83A user can be deleted using `userdel`:
84
85```ShellSession
86# userdel -r alice
87```
88
89The flag `-r` deletes the user's home directory. Accounts can be
90modified using `usermod`. Unix groups can be managed using `groupadd`,
91`groupmod` and `groupdel`.