How to automatically log on to linux

Source: Internet
Author: User
Article Title: How to automatically log on to linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Based on the redhat 8.0 operating system platform, this article describes how to Enable Automatic Logon at startup Level 3 and how to automatically run corresponding programs, this section briefly introduces how to automatically log on to X window under redhat 8.0 (the system startup level is 5) and run the specified application.
  
   I. Implementation of Automatic logon when the startup level is 3
The Implementation of Automatic Logon at startup Level 3 involves two software packages: mingetty-1.00-3.src.rpm software package and util-linux-2.11r-10.src.rpm software package.
  
(1) mingetty-1.00-3.src.rpm Software Package
For the implementation of automatic logon with startup Level 3, you still need to check the/etc/inittab script,
3: 123: respawn:/sbin/mingetty tty3
  
Therefore, to Enable Automatic Logon at startup Level 3, you must understand the mingetty function and even modify the mingetty code. Run the rpm-qf/sbin/mingetty command to see that the redhat 8.0 version of mingetty is included in the mingetty-1.00-3.src.rpm package, download the package, install the source code, by default, the code will be installed under/usr/src/redhat/. All we care about is mingetty. c source file. Mingetty. c has about five hundred lines of code and mainly implements the following functions:
  
  
Open the specified tty (specified by the parameter );
Prompt the user to log on (login :);
Obtain the logon username;
Call/bin/login using the user login name as the parameter.
  
We only care about the following three lines:
  
......
438 while (logname = get_logname () = 0); // 438 rows in the mingetty. c file
439 execl (_ PATH_LOGIN, _ PATH_LOGIN, "--", logname, NULL );
440 error ("% s: can't exec" _ PATH_LOGIN ": % s", tty, sys_errlist [errno]);
......
  
The function of the first line is to output the login prompt and obtain the user's login username. the login username is returned by the logname. Therefore, you can make the following modifications:
......
438 // while (logname = get_logname () = 0); // comment out this line and no longer prompt login:
439 logname = "root"; // Add this line of code
440 execl (_ PATH_LOGIN, _ PATH_LOGIN, "--", logname, NULL );
441 error ("% s: can't exec" _ PATH_LOGIN ": % s", tty, sys_errlist [errno]);
......
Note: assume that the user logs on as a Super User.
  
The second line uses the user login name as the parameter to call the/bin/login program for further login. Therefore, to achieve automatic login, you should also understand the/bin/login function, and modify its source code when necessary.
  
Third Action error handling.
  
(2) util-linux-2.11r-10.src.rpm Software Package
In the same way, view the package to which/bin/login belongs (the redhad8.0 version of login is included in the util-linux-2.11r-10.src.rpm package), download and install the util-linux-2.11r-10.src.rpm, And the login executable file is compiled with several source files, we are most concerned about login. c source file (about 1500 lines of code ). The following briefly analyzes the functions to be implemented by login and makes necessary modifications to the corresponding sections.
  
The Login program can be divided into the following parts:
  
1. login first checks whether the Login user is a superuser. If the Login user is not a superuser and the/etc/nologin file exists, the content of the file is output and the logon process is aborted. It is mainly implemented by checknologin;
2. If the logon user is a Super User, login must log on to the tty List specified in/etc/securetty/. Otherwise, login will fail. You can also leave the/etc/securetty file Unspecified. At this time, the Super User can log on to any tty.
3. after the first two steps of the test, login prompts you to enter the logon password (called by getpass (). If you are interested, refer to its manual page) and verify the logon password, if the password is incorrect, you are prompted to log on again.
4. after successful password verification, login will also check for the existence. if this file exists, perform a "quiet" logon, no messages are output. When the startup level is 3, this information is output normally)
5. login then sets the user ID and group ID of the logon tty and the corresponding environment variables, including HOME, PATH, SHELL, TERM, and LOGNAME. For common users, PATH is set to/usr/local/bin:/bin/usr/bin: by default. For Super Users, PATH is set to/sbin:/bin: /usr/sbin:/usr/bin:
6. The last step of login is to start the shell for the user. If no shell is specified for the user in/etc/passwd,/bin/sh is used. If the current working directory is not provided in/etc/passwd, "/" is used "/".
Now, a complete logon process is complete.
  
From the above analysis of the login source program, we can find that if you want to achieve automatic login, you should make an article in step 3, try to bypass the prompt to enter the password and verify the password. In fact, it is very simple. The login source program sets a switch to control passwd_req for whether a password is required. By default, the value is 1 (passwd_req = 1), that is, the password is required for authentication. After changing the line of code to (passwd_req = 0), the problem is solved. Modify the source file as follows:
......
402 fflag = hflag = pflag = 0; // 402 lines of the login. c file
403 // passwd_req = 1 // It is time-saving and requires password verification. Comment out this line.
404 passwd_req = 0 // Add a row
......
  
After the modification, you can directly use the Makefile provided by the util-linux-2.11r-10.src.rpm for re-compilation, you can also compile it yourself:
  
Gcc-o login. c setproctitle. c checktty. c xstrncpy. c-Wall-lcrypt. Note that the following compilation option-lcrypt is included; otherwise, problems may occur.
  
With the new version of mingetty and login, copy mingetty to the/sbin/directory, copy login to the/bin directory, and set the startup level in the/etc/inittab to 3, then reboot the system (the reader can write a script to implement the above process ).
  
If you are interested in mingetty or other parts of the login code, you can modify login repeatedly. c or mingetty. c source code to test the code function. Note that before copying the new mingetty and login versions, you must back up the original mingetty and login, at the same time, you also need to prepare a system boot disk (you can also install a system disk, so that you have the opportunity to type linux rescue). This should be done before testing the new version of the program. If the code is slightly modified, the system cannot start normally.
  
If you do not want to perform further code testing, you just need to modify the code as described in this article, so there will be no problem during system startup.
  
   2. automatically run specific applications after automatic logon
After enabling automatic logon at startup Level 3, it is very easy to run the application automatically. You can add the application to the/etc/rc. d/rc. local script. (You can try adding startx to the script to see how it works. In a sense, an automatic logon X window Method is added)
  
   Iii. supplement the automatic logon X window (system startup level: 5) and Automatic Running of the specified application
In "How To Enable Automatic Logon to linux", the redhat 7.2 platform is used as the background. The automatic logon part can be directly used in redhat 8.0 without any modification.
  
However, the interfaces for automatically running applications after logon are different in redhat 8.0, mainly because after logging on to gnome, the interfaces for automatically running applications have changed: first, click the GNOME help (the Red Hat) on the panel, select/Others/preferences/Sessions, and add the program to be started on the launch program properties page of the Session dialog box.
  
After logging on to kde, the interface for automatically running the program has not changed.
  
   Iv. Conclusion
This article is similar to the article "How to automatically log on to linux", which basically solves the problem of how to automatically log on to Linux and run corresponding applications. The methods for the two most common startup levels (3 and 5) are provided.
  
During the system initialization to the mingetty and login stages, the kernel has actually completed the boot process and has reached the highest stage of system initialization, which has nothing to do with the kernel. In this case,/sbin/init depends on the content of/etc/inittab. For more information, see man 8 init or man 5 inittab.
  
When modifying the software package mentioned in this article, comply with the GNU General Public License (GPL) standards. In addition, replacing login is usually considered a hacker, so proceed with caution.
  
References
  
1. login manual page
2. mingetty-1.00-3.src.rpm, including the package in the source code of the release version of redhat 8.0;
3. util-linux-2.11r-10.src.rpm,
  

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.