forked from ComPlat/chemotion_ELN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-chuser.sh
More file actions
executable file
·61 lines (51 loc) · 1.81 KB
/
Copy pathprepare-chuser.sh
File metadata and controls
executable file
·61 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh
# shell script to change user uid and gid
# to be run in the container as root
#
# Usage: chuser.sh <uid> <gid>
# Example: chuser.sh 1001 1001
# Note: This script assumes the user 'ubuntu' exists in the container
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <uid> <gid>"
exit 1
fi
uid=$1
gid=$2
uid_was=$(id -u ubuntu)
gid_was=$(id -g ubuntu)
# Check if the user 'ubuntu' exists
# If not, exit with an error message
if ! id -u ubuntu >/dev/null 2>&1; then
echo "User 'ubuntu' does not exist. Please create the user first."
exit 1
fi
# Check if the provided uid and gid are valid numbers
# If not, exit without error (assumes the process has to be skipped)
if ! echo "$uid" | grep -qE '^[0-9]+$' || ! echo "$gid" | grep -qE '^[0-9]+$'; then
echo "Both uid and gid must be valid numbers. Skipping"
exit 0
fi
# Check if the provided uid and gid are not the same as the current uid and gid
# If they are, exit with an error message
if [ "$uid" -eq "$uid_was" ] && [ "$gid" -eq "$gid_was" ]; then
echo "The provided uid and gid are the same as the current uid and gid."
exit 0
fi
# Check if the provided uid and gid are not already in use
# If they are, exit with an error message
if getent passwd ${uid} >/dev/null 2>&1; then
echo "UID ${uid} is already in use. Please choose a different UID."
exit 1
fi
if getent group ${gid} >/dev/null 2>&1; then
echo "GID ${gid} is already in use. Please choose a different GID."
exit 1
fi
ps -u ubuntu # kill any existing ubuntu processes
usermod -u ${uid} ubuntu
find / -user ${uid_was} -exec chown -h ${uid} {} \;
groupmod -g ${uid} ubuntu
usermod -u ${uid} -g ${gid} ubuntu
find / -group ${gid_was} -exec chgrp -h ${gid} {} \;
echo "User 'ubuntu' has been changed to UID ${uid} and GID ${gid}."
sudo chown -R ubuntu:ubuntu /home/ubuntu