Skip to content

Commit 9de471a

Browse files
committed
fix: update auto-login and home directory handling for current user
1 parent 1c95422 commit 9de471a

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

pi-node/setup.sh

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,35 @@ systemctl enable display-update.timer
137137
# ── 5. Auto-login + start X ──
138138
echo "[5/5] Configuring auto-login..."
139139

140+
# Detect current user (handle sudo)
141+
REAL_USER=${SUDO_USER:-$(id -un)}
142+
USER_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
143+
140144
mkdir -p /etc/systemd/system/getty@tty1.service.d
141-
cat > /etc/systemd/system/getty@tty1.service.d/autologin.conf << 'CONF'
145+
cat > /etc/systemd/system/getty@tty1.service.d/autologin.conf << CONF
142146
[Service]
143147
ExecStart=
144-
ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM
148+
ExecStart=-/sbin/agetty --autologin $REAL_USER --noclear %I \$TERM
145149
CONF
146150

147-
# only add auto‑start line if the pi user exists and has a home
148-
if [[ -d /home/pi ]]; then
149-
if ! grep -q "startx" /home/pi/.bash_profile 2>/dev/null; then
150-
# >> will create the file if it doesn't already exist
151-
echo '[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor' >> /home/pi/.bash_profile
151+
# only add auto‑start line if the user home exists
152+
if [[ -d "$USER_HOME" ]]; then
153+
if ! grep -q "startx" "$USER_HOME/.bash_profile" 2>/dev/null; then
154+
echo '[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && startx -- -nocursor' >> "$USER_HOME/.bash_profile"
152155
fi
153156
fi
154157

155-
# only configure openbox/autostart if the pi home directory exists
156-
if [[ -d /home/pi ]]; then
157-
mkdir -p /home/pi/.config/openbox
158-
cat > /home/pi/.config/openbox/autostart << 'AUTOSTART'
158+
# only configure openbox/autostart if the user home directory exists
159+
if [[ -d "$USER_HOME" ]]; then
160+
mkdir -p "$USER_HOME/.config/openbox"
161+
cat > "$USER_HOME/.config/openbox/autostart" << 'AUTOSTART'
159162
xset s off
160163
xset s noblank
161164
xset -dpms
162165
unclutter -idle 0.5 -root &
163166
systemctl start display-kiosk &
164167
AUTOSTART
165-
chown -R $(id -un):$(id -gn) /home/$(id -un)/.config
168+
chown -R "$REAL_USER:$(id -gn "$REAL_USER")" "$USER_HOME/.config"
166169
fi
167170

168171
echo ""

0 commit comments

Comments
 (0)