How to automatically log on to Linux

Source: Internet
Author: User
Author: Zheng Yanxing & nbsp; Source: Beijing. This article uses Redhat7.2 as the level

Author: Zheng Yanxing Source: http://www.coolersky.com/

Automatically log on to Linux at startup and run the XWindow application automatically. it has a special background, such as the monitoring system based on the Linux platform. authentication is not required after Linux is started, and directly run the monitoring program. 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 steps are omitted), init will follow the last line x: 5: respawn: of the/etc/inittab file: /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 (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! ", 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: Couldnt exec % s: % s", 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
& N

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.