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