Skip to content

Commit 1401bfe

Browse files
committed
rename ubuntu domain join script
1 parent e5f03e2 commit 1401bfe

File tree

2 files changed

+106
-88
lines changed

2 files changed

+106
-88
lines changed

ubuntu-domainjoin.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/bash
2+
## Ubuntu domain join
3+
4+
set -e
5+
6+
# Function to display usage
7+
usage() {
8+
echo "Usage: $0 --hostname HOSTNAME --user USERNAME --realm REALM"
9+
echo
10+
echo "Options:"
11+
echo " --hostname Specify the server hostname"
12+
echo " --user Specify the username"
13+
echo " --realm Specify the realm (e.g., 45drives.local)"
14+
exit 1
15+
}
16+
17+
# If no arguments passed, show usage
18+
[[ $# -eq 0 ]] && usage
19+
20+
# Parse arguments
21+
while [[ $# -gt 0 ]]; do
22+
case "$1" in
23+
--hostname)
24+
HOSTNAME="$2"
25+
shift 2
26+
;;
27+
--user)
28+
USERNAME="$2"
29+
shift 2
30+
;;
31+
--realm)
32+
REALM="$2"
33+
shift 2
34+
;;
35+
*)
36+
echo "Unknown option: $1"
37+
usage
38+
;;
39+
esac
40+
done
41+
42+
# Validate required variables
43+
if [[ -z "$HOSTNAME" || -z "$USERNAME" || -z "$REALM" ]]; then
44+
echo "Error: --hostname, --user, and --realm arguments are required."
45+
usage
46+
fi
47+
48+
# Force the realm to uppercase for Kerberos
49+
REALM_UPPER="${REALM^^}"
50+
51+
echo "Username: $USERNAME"
52+
echo "Realm: $REALM_UPPER"
53+
54+
echo "Installing prerequisite packages: realmd, samba, krb5-user..."
55+
apt install -y realmd samba krb5-user
56+
57+
# Normalize case for comparison
58+
HOSTNAME_LOWER=$(echo "$HOSTNAME" | tr '[:upper:]' '[:lower:]')
59+
REALM_LOWER=$(echo "$REALM" | tr '[:upper:]' '[:lower:]')
60+
61+
# Only append the realm if it is not already part of the hostname
62+
if [[ "$HOSTNAME_LOWER" == *"$REALM_LOWER"* ]]; then
63+
echo "Hostname already contains the realm. Leaving it as: $HOSTNAME"
64+
FINAL_HOSTNAME="$HOSTNAME"
65+
else
66+
FINAL_HOSTNAME="$HOSTNAME.$REALM_LOWER"
67+
echo "Setting hostname to: $FINAL_HOSTNAME"
68+
hostnamectl set-hostname "$FINAL_HOSTNAME"
69+
fi
70+
71+
currentTimestamp=$(date +%y-%m-%d-%H:%M:%S)
72+
if [ -f /etc/samba/smb.conf ]; then
73+
echo "Backing up existing samba conf to /etc/samba/smb.conf.$currentTimestamp.bak"
74+
mv /etc/samba/smb.conf /etc/samba/smb.conf.$currentTimestamp.bak
75+
else
76+
echo "File /etc/samba/smb.conf does not exist. Skipping."
77+
fi
78+
79+
echo "Generating kerberos ticket, please enter password at the prompt..."
80+
kinit "$USERNAME@$REALM_UPPER"
81+
82+
echo "Validating we can discover the domain..."
83+
realm discover "$REALM_UPPER"
84+
85+
echo "Joining the domain..."
86+
realm join --user="$USERNAME" --membership-software=samba --client-software=winbind --server-software=active-directory "$REALM_UPPER"
87+
88+
echo "Outputting domain join validation..."
89+
realm list
90+
91+
echo "Configuring smb.conf to use net registry"
92+
echo "include = registry" >> /etc/samba/smb.conf
93+
94+
echo "Updating /etc/nsswitch.conf to use winbind"
95+
sed -i -E '/^(passwd|group):/ s/\bsss\b/winbind/g' /etc/nsswitch.conf
96+
97+
echo "Configuring /etc/krb5.conf"
98+
cat <<EOF >/etc/krb5.conf
99+
[libdefaults]
100+
default_realm = $REALM_UPPER
101+
dns_lookup_realm = false
102+
dns_lookup_kdc = true
103+
EOF
104+
105+
# pam-auth-update --enable mkhomedir
106+
systemctl enable --now smbd

ubuntu20-domainjoiner.sh

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)