c9eeb4d6a47331cb326f1db1e60f6eb61215e3be
[webi-installers/.git] / ssh-adduser / README.md
1 ---
2 title: SSH adduser
3 homepage: https://webinstall.dev/ssh-adduser
4 tagline: |
5   SSH adduser: Because friends don't let friends login or run stuff as root
6 linux: true
7 description: |
8   Many modern web programs (`npm` and `postgres`, for example) will not function correctly if run as root.
9
10   `ssh-adduser` will
11
12     1. add the user `app`
13     2. set a random, 32-character password (as a failsafe)
14     3. copy the `root` user's **`~/.ssh/authorized_keys`** (so the same users can still login)
15     4. give the `app` user `sudo` (admin) privileges
16     5. allow `app` to `sudo` without a password
17 ---
18
19 How to create a new user named 'app':
20
21 ```bash
22 # --disable-password prevents a password prompt
23 # --gecos "" skips the useless questions
24 adduser --disabled-password --gecos "" app
25 ```
26
27 How to create a and set a random password:
28
29 ```bash
30 # sets 'my_password' to 32 random hex characters (16 bytes)
31 my_password=$(openssl rand -hex 16)
32
33 # uses 'my_password' for to reset and confirm 'app's password
34 printf "$my_password"'\n'"$my_password" | passwd app
35 ```
36
37 How to make the user 'app' a "sudo"er (an admin):
38
39 ```bash
40 adduser app sudo
41 ```
42
43 How to allow 'app' to run sudo commands without a password:
44
45 ```bash
46 echo "app ALL=(ALL:ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/app
47 ```
48
49 How to copy allowed keys from root to the new user:
50
51 ```bash
52 mkdir -p /home/app/.ssh/
53 chmod 0700 /home/app/.ssh/
54
55 cat "$HOME/.ssh/authorized_keys" >> /home/app/.ssh/authorized_keys
56 chmod 0600 /home/app/.ssh/authorized_keys
57
58 chown -R app:app /home/app/.ssh/
59 ```