Skip to content

Commit a8bea76

Browse files
committed
Try to head off problems created by unnecessary use of sudo
A recurrent theme on Discord is questions where the underlying cause of the user's problem is an unnecessary use of the sudo command. To try to reduce the incidence of both the problems and the questions, this PR adds code to the start of most bash scripts. The code checks the EUID and exits with "This script should NOT be run using sudo". A discussion of the problem of using sudo is also added to docs/Getting-Started.md. Aside from hopefully catching the eye of every new IOTstack user, this will be a useful anchor for Discord questions. Some scripts also lacked the "hash bang" preamble and I took the opportunity to fix such problems.
1 parent 416091c commit a8bea76

File tree

11 files changed

+93
-3
lines changed

11 files changed

+93
-3
lines changed

docs/Getting-Started.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,54 @@ Andreas Spiess video #295
66

77
[![#295 Raspberry Pi Server](http://img.youtube.com/vi/a6mjt8tWUws/0.jpg)](https://www.youtube.com/watch?v=a6mjt8tWUws)
88

9+
## A word about the `sudo` command
10+
11+
Many first-time users of IOTstack get into difficulty by misusing the `sudo` command. The problem is best understood by example. In the following, you would expect `~` (tilde) to expand to `/home/pi`. It does:
12+
13+
```
14+
$ echo ~/IOTstack
15+
/home/pi/IOTstack
16+
```
17+
18+
The command below sends the same `echo` command to `bash` for execution. This is what happens when you type the name of a shell script. You get a new instance of `bash` to run the script:
19+
20+
```
21+
$ bash -c 'echo ~/IOTstack'
22+
/home/pi/IOTstack
23+
```
24+
25+
Same answer. Again, this is what you expect. But now try it with `sudo` on the front:
26+
27+
```
28+
$ sudo bash -c 'echo ~/IOTstack'
29+
/root/IOTstack
30+
```
31+
32+
The answer is different. It is different because `sudo` means "become root, and then run the command". The process of becoming root changes the home directory, and that changes the definition of `~`.
33+
34+
Any script designed for working with IOTstack assumes `~` (or the equivalent `$HOME` variable) expands to `/home/pi`. That assumption is invalidated if the script is run by `sudo`.
35+
36+
Of necessity, any script designed for working with IOTstack will have to invoke `sudo` **inside** the script **when it is required**. You do not need to second-guess the script's designer.
37+
38+
Please try to minimise your use of `sudo` when you are working with IOTstack. Here are some rules of thumb:
39+
40+
1. Is what you are about to run a script? If yes, check whether the script already contains `sudo` commands. Using `menu.sh` as the example:
41+
42+
```
43+
$ grep -c 'sudo' ~/IOTstack/menu.sh
44+
28
45+
```
46+
47+
There are 28 separate uses of `sudo` within `menu.sh`. That means the designer thought about when `sudo` was needed.
48+
49+
2. Did the command you **just executed** work without `sudo`? Note the emphasis on the past tense. If yes, then your work is done. If no, and the error suggests elevated privileges are necessary, then re-execute the last command like this:
50+
51+
```
52+
$ sudo !!
53+
```
54+
55+
It takes time, patience and practice to learn when `sudo` is **actually** needed. Over-using `sudo` out of habit, or because you were following a bad example you found on the web, is a very good way to find that you have created so many problems for yourself that will need to reinstall your IOTstack. *Please* err on the side of caution!
56+
957
## Download the project
1058

1159
You may need to install these support tools first:

menu.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
36
#get path of menu correct
47
pushd ~/IOTstack
58

scripts/backup_influxdb.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
36
#first move the contents of the old backup out and clear the directory
47
echo "Moving old influxdb backups if they exist"
58
[ -d ~/IOTstack/backups/influxdb/db_old ] || sudo mkdir ~/IOTstack/backups/influxdb/db_old

scripts/docker_backup.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
36
pushd ~/IOTstack
47
USER=$(whoami)
58

scripts/prune-images.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
16
docker image prune -a

scripts/prune-volumes.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
docker system prune --volumes
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
25

6+
docker system prune --volumes

scripts/restart.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
16
docker-compose restart

scripts/start.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
docker-compose up -d
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
6+
docker-compose up -d

scripts/stop-all.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
16
docker container stop $(docker container ls -aq)

scripts/stop.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
docker-compose down
1+
#!/bin/bash
2+
3+
# should not run as root
4+
[ "$EUID" -eq 0 ] && echo "This script should NOT be run using sudo" && exit -1
5+
6+
docker-compose down

0 commit comments

Comments
 (0)