注意:在虚拟环境中使用,不要在生产环境中使用
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
A.在X11模式下
通过编辑文件/etc/gdm/custom.conf,然后修改[daemon]部分。此方法在Full multiuser mode下不起作用。下面的例子是用root自动登录
1.设置为自动登录,立即生效
[daemon]
AutomaticLoginEnable=true
AutomaticLogin=root
2.设置为延时自动登录,下面的意思是在120秒后会自动用root登录如果没有用其他用户登录
[daemon]
TimedLoginEnable=true
TimedLogin=root
TimedLoginDelay=120
B.在Full multiuser mode模式下
执行完下面的脚本后,重启后即可生效
sed -i -e 's/exec \/sbin\/mingetty $TTY/exec \/sbin\/mingetty --autologin root $TTY/g' /etc/init/tty.conf
chmod u+w /etc/shadow
sed -i -e 's/^root:[^:]*:/root::/g' /etc/shadow
chmod u-w /etc/shadow
sed -i -e 's/#PermitEmptyPasswords .*/PermitEmptyPasswords yes/g' /etc/ssh/sshd_config
Reference
https://help.gnome.org/admin/gdm/stable/configuration.html.en
http://www.thatsgeeky.com/2012/03/root-autologin-on-centos/
https://oracle-base.com/articles/linux/linux-automatic-or-timed-login