Posts

Auto Login Linux Mint

2 months ago · By Engineerisaac · Public
You can execute this code
1
curl -fsSL https://engineerisaac.com/download/mint_enable_autologin.sh | head -n 20

This is for convinence for you. You dont have to use the direct download bash command. However it will assume the native user is the default user and auto login to that account.

This will excute this code

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
set -euo pipefail

if [[ "${EUID}" -ne 0 ]]; then
    exec sudo -E bash "$0" "$@"
fi

ts() { date +"%Y%m%d_%H%M%S"; }
die() { echo "ERROR: $*" >&2; exit 1; }

backup_file() {
    local f="$1"
    [[ -f "$f" ]] || return 0
    local b="${f}.bak.$(ts)"
    cp -a "$f" "$b"
    echo "Backup: $b"
}

guess_user() {
    if [[ -n "${1:-}" ]]; then
        echo "$1"
        return 0
    fi
    if [[ -n "${SUDO_USER:-}" && "${SUDO_USER}" != "root" ]]; then
        echo "${SUDO_USER}"
        return 0
    fi
    local ln
    ln="$(logname 2>/dev/null || true)"
    if [[ -n "$ln" && "$ln" != "root" ]]; then
        echo "$ln"
        return 0
    fi
    die "Could not determine target username. Run: sudo $0 YOUR_USERNAME"
}

detect_dm() {
    if [[ -f /etc/X11/default-display-manager ]]; then
        local dm_path
        dm_path="$(cat /etc/X11/default-display-manager 2>/dev/null || true)"
        case "$dm_path" in
            *lightdm*) echo "lightdm"; return 0 ;;
            *gdm3*)    echo "gdm3"; return 0 ;;
            *sddm*)    echo "sddm"; return 0 ;;
        esac
    fi

    if systemctl list-unit-files 2>/dev/null | grep -qE '^lightdm\.service'; then echo "lightdm"; return 0; fi
    if systemctl list-unit-files 2>/dev/null | grep -qE '^gdm3\.service'; then echo "gdm3"; return 0; fi
    if systemctl list-unit-files 2>/dev/null | grep -qE '^sddm\.service'; then echo "sddm"; return 0; fi

    die "Could not detect display manager (lightdm/gdm3/sddm)."
}

choose_session() {
    local desired="${SESSION:-}"
    if [[ -n "$desired" ]]; then
        echo "$desired"
        return 0
    fi

    if [[ -d /usr/share/xsessions ]]; then
        [[ -f /usr/share/xsessions/cinnamon.desktop ]] && { echo "cinnamon"; return 0; }
        [[ -f /usr/share/xsessions/mate.desktop ]] && { echo "mate"; return 0; }
        [[ -f /usr/share/xsessions/xfce.desktop ]] && { echo "xfce"; return 0; }
    fi

    echo "cinnamon"
}

enable_lightdm() {
    local user="$1"
    local dir="/etc/lightdm/lightdm.conf.d"
    local conf="${dir}/50-autologin.conf"

    mkdir -p "$dir"
    backup_file "$conf"

    printf "[Seat:*]\nautologin-user=%s\nautologin-user-timeout=0\n" "$user" > "$conf"
    chmod 0644 "$conf"
    echo "Configured LightDM autologin in: $conf"
}

enable_gdm3() {
    local user="$1"
    local conf="/etc/gdm3/custom.conf"

    [[ -f "$conf" ]] || die "Expected $conf to exist, but it does not."
    backup_file "$conf"

    local tmp
    tmp="$(mktemp)"

    awk -v user="$user" '
    BEGIN {
        in_daemon=0
        seen_daemon=0
        set_enable=0
        set_user=0
    }

    /^\[daemon\][[:space:]]*$/ {
        seen_daemon=1
        in_daemon=1
        print
        next
    }

    /^\[[^]]+\][[:space:]]*$/ {
        if (in_daemon==1) {
            if (set_enable==0) print "AutomaticLoginEnable=True"
            if (set_user==0)   print "AutomaticLogin=" user
            in_daemon=0
        }
        print
        next
    }

    {
        if (in_daemon==1) {
            if ($0 ~ /^[[:space:]]*AutomaticLoginEnable[[:space:]]*=/) { print "AutomaticLoginEnable=True"; set_enable=1; next }
            if ($0 ~ /^[[:space:]]*AutomaticLogin[[:space:]]*=/)       { print "AutomaticLogin=" user; set_user=1; next }
        }
        print
    }

    END {
        if (in_daemon==1) {
            if (set_enable==0) print "AutomaticLoginEnable=True"
            if (set_user==0)   print "AutomaticLogin=" user
        }
        if (seen_daemon==0) {
            print ""
            print "[daemon]"
            print "AutomaticLoginEnable=True"
            print "AutomaticLogin=" user
        }
    }' "$conf" >"$tmp"

    cat "$tmp" >"$conf"
    rm -f "$tmp"
    chmod 0644 "$conf"
    echo "Configured GDM3 autologin in: $conf"
}

enable_sddm() {
    local user="$1"
    local session="$2"
    local dir="/etc/sddm.conf.d"
    local conf="${dir}/99-autologin.conf"

    mkdir -p "$dir"
    backup_file "$conf"

    printf "[Autologin]\nUser=%s\nSession=%s\nRelogin=true\n" "$user" "$session" > "$conf"
    chmod 0644 "$conf"
    echo "Configured SDDM autologin in: $conf"
}

main() {
    local user
    user="$(guess_user "${1:-}")"
    id "$user" >/dev/null 2>&1 || die "User '$user' does not exist."

    local dm
    dm="$(detect_dm)"

    echo "Target user: $user"
    echo "Detected display manager: $dm"

    case "$dm" in
        lightdm) enable_lightdm "$user" ;;
        gdm3)    enable_gdm3 "$user" ;;
        sddm)
            local session
            session="$(choose_session)"
            echo "SDDM session: $session (override with: SESSION=... )"
            enable_sddm "$user" "$session"
            ;;
        *) die "Unsupported display manager: $dm" ;;
    esac

    echo "Done. Reboot to apply: sudo reboot"
}

main "$@"
Comments are on the post page: View comments