How To Enable Automatic Logon to a Linux operating system

Source: Internet
Author: User

Automatically log on to Linux at startup and run the XWindow application automatically Program It has a special application background, such as a monitoring system based on the Linux platform. After Linux is started, it directly runs the monitoring program without authentication. This document uses redhat7.2 as a platform and combines it with the Linux Startup Process. It describes how to avoid automatic logon for identity authentication and directly go to X Window to automatically run the application.

I. Work in the final phase of Linux Startup

In the final phase of the Linux Startup Process (the specific startup step is omitted), init will follow the last line of the/etc/inittab file X: 5: respawn: /etc/X11/preofdm-nodaemon run the/etc/X11/preofdm script (redhat7.2 is short for saving time ). The main task of the preofdm script is to start X Window. There are several methods to start X Window, which are included in the preofdm script. There are several main methods:

Run xdm to start X Window;
Run the running Agent agent (gmtms) to enter the GNOME desktop environment;
Run KDM to enter the KDE Desktop Environment;
Automatically log on to Linux;

The script framework of preofdm is roughly as follows:

#! /Bin/sh
Path =/sbin:/usr/sbin:/bin:/usr/x11r6/bin
./Etc/profile. d/lang. Sh
# Step 1: Check whether automatic logon is enabled
If [-F/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; then
If/usr/sbin/autologin; then
Exit 0
Fi
Fi

# Step 2: if it is not an automatic logon method, you will find your preferred logon method in/etc/sysconfig/desktop.
......
# It Can Be kdm, TPD, and xdm, and run corresponding kdm, TPD, and xdm.

II. Implementation of Automatic Logon (implementation of autologin)

In the/etc/X11/preofdm script, whether to Enable Automatic Logon has a conditional test switch. In fact, you can comment out the test switch here and directly start X Window.

Automatic Logon essentially bypasses authentication and directly starts X Window. Xinit can be used to start X Window.

Xinit is used to start the X Window System server and the first client program on the system. You can specify the server and client program to be started by passing command line parameters for xinit. If the parameter is not passed to xinit, it will be searched and run under the user's root directory. the xinitrc script starts the client program and searches for and runs it in the root directory of the user. the xserverrc script starts the server. If xinit cannot find. xinitrc and. xserverrc in the root directory of the user, xinit uses the default X: 0. In fact, it is more convenient to start X with startx. Startx provides better user interfaces for XWindow systems that run a single session. Similarly, startx is first searched in the root directory of the user. xinitrc and. xserverrc script. If the two scripts cannot be found, startx uses the/etc/X11/xinit/xinitrc and/etc/X11/xinit/xserverrc scripts. The most basic framework of startx scripts is:

A. Search for. xinitrc. If not, use xinitrc;
B. Search for. xserverrc. If not, use xserverrc;
C. Determine the xinit parameter based on the script;

It can be seen that startx can complete the task of starting X without passing any parameters. Therefore, you can modify the/etc/X11/preofdm script to achieve automatic login as follows:

#! /Bin/sh
Path =/sbin:/usr/sbin:/bin:/usr/x11r6/bin
./Etc/profile. d/lang. Sh
# Step 1: Check whether automatic logon is enabled
# If [-F/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; then
# Comment out the above condition test and run startx directly
If/usr/x11r6/bin/startx; then
Exit 0
Fi
# Fi

Of course, make sure that the startup level in the/etc/inittab is 5.

After you restart the system, you will find that the system does not verify the user identity and directly enters the XWindow. At this time, the user identity is root. However, if the original root has its own desktop and default shell, the above method does not necessarily guarantee that X will have the original settings. To avoid authentication without changing the user's original settings after starting X, you still have to do work before running startx.

3. After automatic logon, keep the original configurations (desktop, shell, and other environment variables)

Observe the automatic logon section of the original/etc/X11/preofdm script:

......
# Step 1: Check whether automatic logon is enabled
If [-F/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; then
If/usr/sbin/autologin; then
Exit 0
Fi
Fi
......

The script retains the automatic logon interface: an executable file/usr/sbin/autologin and a configuration file/etc/sysconfig/autologin.
1. Implementation of the/etc/sysconfig/autologin configuration file:
# Config for autologin
User = root
Exec =/usr/x11r6/bin/startx
Description: User specifies the username for Automatic Logon; Exec specifies the program to be run to start X.
2. Implementation of/usr/sbin/autologin executable files
/*********************
* *** Autologin. c ****
*********************/
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Int main (INT argc, char ** argv)
{
Struct stat st;
File * F;
Char * CFG;
Struct passwd * PW;
Uid_t uid;
Gid_t GID;
Char * Dir, * shell;
Char * user = NULL;
Char * cmd = NULL;
User = "root ";
/* To illustrate the problem and keep the program simple, the default logon user here is root. In fact,
The login username should be obtained from/etc/sysconfig/autologin,
During program implementation, you must filter out invalid usernames in/etc/sysconfig/autologin */
Cmd = "/usr/x11r6/bin/startx ";
/* Similarly, specify the program to start X Window. In fact, this program should be obtained from/etc/sysconfig/autologin */
PW = getpwnam (User );
// Getpwnam returns the passwd structure containing user information (defined in PWD. h ).
If (PW ){
Uid = pw-> pw_uid;
Gid = pw-> pw_gid; Dir = strdup (PW-> pw_dir );
Shell = strdup (PW-> pw_shell );
}
// Obtain user information
Else {
Printf ("error: no such user % s! \ N ", user );
Return 1;
}
Chown ("/dev/console", uid, GID );
Chown ("/dev/tty", uid, GID );
// Set the user ID and group ID for the console and terminal

// Set the user ID below
Setregid (GID, GID );
Setegid (GID );
Setgid (GID );
Setreuid (UID, UID );
Seteuid (UID );
Setuid (UID );
Setenv ("home", Dir, 1 );
Setenv ("shell", Shell, 1 );
Setenv ("user", user, 1 );
Setenv ("LOGNAME", user, 1 );
// Set user-related environment variables
Chdir (DIR );
// Switch to the user root directory
User = NULL;
Execvp (CMD, argv );
/* After configuring the user's related information, start the X Window operation. Note that/usr/x11r6/bin/startx */is executed by default */
Printf ("error: Couldn't exec % s: % s \ n", CMD, strerror (errno ));
Return 2;
}

Run gcc-O autologin. C, copy the autologin executable file to/usr/sbin/autologin, and copy the autologin configuration file to/etc/sysconfig/autologin. After the system is restarted, the system directly enters the X Window and retains all the original user styles.

If you do not need to log on to the configuration file/etc/sysconfig/autologin automatically, all operations are performed in/usr/sbin/autologin by default (for example, the default logon identity is root, the default operation is/usr/x11r6/bin/startx and so on. Therefore, the automatic logon of the/etc/X11/preofdm script can be simplified as follows:
......
# Step 1: Check whether automatic logon is enabled
If/usr/sbin/autologin; then
Exit 0
Fi
// Step 2 ......
......

Remove the conditional test switch from the script and run/usr/sbin/autologin directly. In this case, you only need to copy the autologin executable file to/usr/sbin/autologin, you do not need to copy the autologin configuration file to/etc/sysconfig/autologin.

4. Select KDE or GNOME and automatically start the X Window application.

If the system enters KDE after the restart and you need to enter gnome, you only need to run switchdesk gnome to restart the system, and then it will automatically enter gnome each time it is started; and vice versa. Generally, the purpose of Automatic System logon is to automatically run an XWindow program after the X Window is started. If the default startup level is 3, you only need to add the corresponding commands to some scripts to automatically run some applications after the system starts. It is complicated to automatically run the application after XWindow is started. Fortunately, both KDE and gnome have left an Automatic startup interface for this. If the application is automatically started in the KDE Desktop Environment, you only need to add the application name to/root /. KDE/autostart/directory (note that the root directories of different users may be different, for example, the root directory of user ZYX may be/home/ZYX ). If the application is automatically started in the GNOME desktop environment, you only need to add the application name to the/Main Menu/Program/settings/session characteristics and the startup programs attribute page of the Startup Program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.