How to implement the Linux operating system automatic login _unix 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.

   First, Linux start the last phase of work

Linux is in the final phase of the startup process (the specific startup step is slightly), Init will run the/ETC/X11/PREFDM script based on the last line of the/etc/inittab file X:5:respawn:/etc/x11/prefdm-nodaemon. (This is Redhat7.2 by default). The main task of the PREFDM script is to complete the start of X window, there are several ways to start X window, which are included in the PREFDM script, and several main methods are:

Run XDm to start x Window;
Run GDM and enter the GNOME desktop environment;
Run KDM into the KDE desktop environment;
automatically login into Linux;

The 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 for automatic logon
If [-f/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; Then
If/usr/sbin/autologin; Then
Exit 0
Fi
Fi

# Step Two: If it's not an automatic login, you'll search the/etc/sysconfig/desktop for the user's preferred login mode.
......
# can be KDM, GDM and XDm, and run the corresponding KDM, GDM and XDm.

   II. Implementation of automatic login (autologin)

In the/ETC/X11/PREFDM script, whether to implement automatic login has a condition test switch, in fact, you can comment out the test switch here, directly perform the action of starting X window.

Automatic login essentially bypasses authentication and starts X Window directly. The start of the X window can be done by Xinit.

Xinit is used to start the X Window System server and the first client on the system, and you can specify the server and client programs 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 searches for the. xinitrc and. XSERVERRC scripts in the user's root directory, and if these two scripts are not found, StartX will use/ETC/X11/XINIT/XINITRC and/etc/x11/xinit/ XSERVERRC script. The most basic framework for STARTX scripts is:

A, looking for. XINITRC, if not then use XINITRC;
b, look for. XSERVERRC, if not then use XSERVERRC;
C, according to the found script to determine the parameters of xinit;

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:/bin:/usr/bin:/usr/x11r6/bin
. /etc/profile.d/lang.sh
# First step: Check for automatic logon
#if [-f/etc/sysconfig/autologin-a-x/usr/sbin/autologin]; Then
#注释掉上边的条件测试, run directly 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 the system, you will find that the system does not authenticate the user identity, directly into the Xwindow, at this time the user identity as 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.


   third, automatically login, to maintain the user's original configuration (desktop, shell and other environmental variables)

Observe the automatic login section of the original/ETC/X11/PREFDM script:

......
# First step: Check for automatic logon
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/autologin.
1, the/etc/sysconfig/autologin configuration file implementation:
#config for Autologin
User=root
Exec=/usr/x11r6/bin/startx
Description, user specifies the username at the time of automatic logon, and exec specifies the program to run when X starts.
2,/usr/sbin/autologin executable file implementation
/*********************
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 be able to explain the problem and keep the program concise, where the default login user is root, in fact,
Login username should be obtained from/etc/sysconfig/autologin,
Program implementation should pay attention to filter out the invalid username in/etc/sysconfig/autologin.
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 setting up a 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);
Setting user-related environment variables
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 that this is 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 configuration file to/etc/sysconfig/autologin. 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 portion of the/ETC/X11/PREFDM script can be simplified as follows:
......
# First step: Check for automatic logon
If/usr/sbin/autologin; Then
Exit 0
Fi
Second step ...
......

That is, remove the conditional test switch in the script and execute the/usr/sbin/autologin directly, at which point you only need to copy the Autologin executable to/usr/sbin/autologin, no longer need to copy autologin configuration file 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 automatically go to gnome each time it starts, 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 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.