How to realize Linux automatic landing __linux

Source: Internet
Author: User
  Boot automatically login Linux, and automatically run the Xwindow application, has its special application background, such as based on Linux platform monitoring system, Linux does not need authentication after startup, and directly run the monitoring program and so on. This article takes Redhat7.2 as the platform, combined with Linux startup process, introduced how to avoid authentication automatic login, and go directly to X window to run the application automatically.   I, Linux start the last phase of work   Linux in the final phase of the boot process (the specific startup steps slightly), Init will be based on the last line of/etc/inittab file X:5:respawn:/etc/x11/ Prefdm-nodaemon runs the/ETC/X11/PREFDM script (Redhat7.2 by default).   The main task of the PREFDM script is to start the X window, there are several ways to start X window, all included in the PREFDM script, several main methods are:   Run XDM launch X window; run GDM and enter the GNOME desktop environment; Run KDM into the KDE desktop environment; automatically login to Linux;   PREFDM script framework is roughly as follows:   #!/bin/sh PATH=/SBIN:/USR/SBIN:/BIN:/USR/BIN:/USR/X11R6 /bin. /ETC/PROFILE.D/LANG.SH # First step: Check to see if [-f/etc/sysconfig/autologin-a-x/usr/sbin/autologin] for automatic login; Then If/usr/sbin/autologin; Then Exit 0 fi fi   # Step two: If you're not automatically logged in, you'll search for the user's preferred login in/etc/sysconfig/desktop ... # can be KDM, GDM and XDm, and run the corresponding KDM, GDM and XDm.   II, the implementation of automatic login (autologin)   in the/ETC/X11/PREFDM script, whether the implementation of automatic login has a condition test switch, in fact, you can comment out the test switch here, directly perform the operation of the Start X window.   Automatic login is essentially bypassing authentication, start x Window directly. The start of the X window can be done by Xinit.   Xinit to start the X Window System server and the first client on the system, you can specify the server and the client program to start by passing command-line arguments for Xinit. If the parameter is not passed to Xinit, it will look for and run the. Xinitrc script in the user's root directory to start the client, and locate and run the. XSERVERRC script in the user's root directory to start the server. If Xinit is not found in the user's root directory, XINITRC,. Xserverrc,xinit will use the default x:0. In fact, it's more convenient to start X with StartX. For Xwindow systems running a single session, STARTX provides a better user interface. Similarly, StartX first looks at the user's root directory. XINITRC and. XSERVERRC script, if these two scripts are not found, StartX will use/ETC/X11/XINIT/XINITRC and/ETC/X11/XINIT/XSERVERRC scripts.   The most basic framework for STARTX scripts is:   A, looking for. XINITRC, if not, use XINITRC B, search for. XSERVERRC, if not then use XSERVERRC; c, determine Xinit parameters based on the found script;   It can be seen that STARTX can complete the task of starting x without passing any parameters, so you can modify the/ETC/X11/PREFDM script to implement automatic login as follows:   #!/bin/sh path=/sbin:/usr/sbin:/b In:/usr/bin:/usr/x11r6/bin. /ETC/PROFILE.D/LANG.SH # First step: Check to see if it is an automatic login #if [-f/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; Then #注释掉上边的条件测试, directly run StartX if/usr/x11r6/bin/startx; Then Exit 0 fi #fi   Of course, you should ensure that the startup level in/etc/inittab is 5.   Reboot system, will find that the system does not authenticate the user identity, directly into the Xwindow, at this time the user identity for the ROOt However, if the original root has its own desktop, the default shell, the above method start x does not necessarily guarantee that the original settings. In order to start X, while avoiding authentication while not changing the user's original settings, there is work to be done before running STARTX.     Three, to maintain the user's original configuration (desktop, shell and other environment variables)   Observe the automatic login part of the original/ETC/X11/PREFDM script:   ... # The first step: view is No for automatic login if [-f/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; Then If/usr/sbin/autologin; Then Exit 0 fi fi ...   It is easy to see that the script retains the interface for automatic login: An executable file/usr/sbin/autologin and a configuration file/etc/sysconfig/aut   Ologin. 1,/etc/sysconfig/autologin configuration file implementation: #config for Autologin user=root exec=/usr/x11r6/bin/startx description, user specified automatic logon username; E   XEC Specifies which program to start X to run. 2, the implementation of/usr/sbin/autologin executable/********************* * * * * * * * * autologin.c *********************/#include #incl   Ude #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 be able to explain the problem andKeep the program concise, where the default login user is root, in fact, the login username should be obtained from the/etc/sysconfig/autologin, the program should pay attention to filter out the/etc/sysconfig/autologin in the invalid username * * * cmd= "   /usr/x11r6/bin/startx ";   /* Similarly, here directly specify the program to start X window, in fact, the program should be obtained from the/etc/sysconfig/autologin/pw = Getpwnam (user);   Getpwnam returns a passwd structure that contains user information, which is defined in Pwd.h.       if (PW) {uid=pw->pw_uid;    gid=pw->pw_gid;   Dir=strdup (Pw->pw_dir);   Shell=strdup (Pw->pw_shell);   //Get user related information else {printf ("Error:no such user%s!/n", user);   return 1;   } chown ("/dev/console", UID, GID);       Chown ("/dev/tty", UID, GID);   Set up user IDs and group IDs for consoles and terminals//The following is to set user-related ID 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 variable chdir (dir);   Switch to user root directory user=null;   EXECVP (cmd, argv); /* Performs a start X window operation after you have configured the user's relevant information. Note Here the default execution/usr/x11r6/bin/startx/printf ("Error:couldn ' t exec%s:%s/n", CMD, Strerror (errno));   return 2;   Run Gcc-o autologin autologin.c, copy autologin executable file to/usr/sbin/autologin, Copy autologin config file to/etc/sysconfig/autolog In.       Reboot the system and go directly to the X window and retain all of the user's original style. If you do not need to automatically log on to the profile/etc/sysconfig/autologin, all actions are implemented in/usr/sbin/autologin (for example, the default logon status is root, and the default action is/usr/x11r6/bin /STARTX, etc.), the automatic login part of the/ETC/X11/PREFDM script can be simplified as follows:. # Step one: Check for automatic login if/usr/sbin/autologin; Then Exit 0 fi//second step ...   that is to remove the conditional test switch in the script and execute the/usr/sbin/autologin directly, just copy the Autologin executable To/usr/sbin/autologin, you no longer need to copy autologin configuration files to/etc/sysconfig/autologin.   IV, choose to enter KDE or GNOME, and automatically start the X Window application   If the system goes into KDE after reboot and the user needs to go into gnome, just run Switchdesk gnome to reboot the system, and then each time it starts will automatically enter GNOME, and vice versa. The purpose of the general system automatic login is to automatically run a Xwindow program after X window is started. If the system defaults to a boot level of 3, then if you want to run certain applications automatically after the system starts, just add the commands to some of the scripts and no longer dwell on them. It's a bit more complicated to run the application automatically after Xwindow starts, but thankfully, KDE and Gnome have left an automatic launch interface. If you start an application automatically in a KDE desktop environment, simply add the application name to the/root/.kde/autostart/directory (note that the root directory for different users may be different, such as the user ZYX root directory may be/hoME/ZYX). If you start an application automatically in the GNOME desktop environment, simply add the name of the application/main Menu/Program/settings/session/session feature and Startup programs property page of the launcher.  

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.