⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 如何实现自动登录linux.htm

📁 近日使用liunx 的一些实用资料
💻 HTM
📖 第 1 页 / 共 4 页
字号:
fi

# 第二步:如果不是自动登录方式,就会在/etc/sysconfig/desktop中搜寻用户偏爱的登录方式
	......
# 可以是kdm、gdm以及xdm,并运行相应的kdm、gdm以及xdm。
	......
	</CODE></PRE></TD></TR></TBODY></TABLE><BR><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="如何实现自动登录Linux.files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="如何实现自动登录Linux.files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="如何实现自动登录Linux.files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="如何实现自动登录Linux.files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/l-tip-prompt/tip20/index.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=N1005B><SPAN 
            class=atitle>二、自动登录的实现(autologin的实现)</SPAN></A></P>
            <P>在/etc/X11/prefdm脚本中,是否实现自动登录有一个条件测试开关,事实上,可以在这里注释掉测试开关,直接执行启动X 
            window的操作。</P>
            <P>自动登录实质上就是绕过身份验证,直接启动X window。X window的启动可以由xinit来完成。</P>
            <OL>
              <LI>Xinit用来启动X 
              window系统服务器以及系统上的第一个客户程序,可以通过为xinit传递命令行参数的形式指定要启动的服务器及客户程序。如果不传递参数给xinit,它将在用户的根目录下寻找并运行 
              .xinitrc脚本来启动客户程序;在用户的根目录下寻找并运行 
              .xserverrc脚本来启动服务器。如果xinit在用户的根目录下找不到.xinitrc、.xserverrc,xinit将使用缺省的X 
              :0。 
              <LI>实际上,用startx来启动X更为方便。对于运行单一会话的X window 
              系统,startx提供了更为良好的用户接口。同样,startx首先在用户的根目录下寻找 
              .xinitrc及.xserverrc脚本,如果找不到这两个脚本,startx将使用/etc/X11/xinit/xinitrc以及/etc/X11/xinit/xserverrc脚本。 
              </LI></OL>
            <P>startx脚本的最基本框架是:</P>
            <UL>
              <LI>a、 寻找.xinitrc,如果没有则使用xinitrc; 
              <LI>b、 寻找.xserverrc,如果没有则使用xserverrc; 
              <LI>c、 根据找到的脚本确定xinit的参数; </LI></UL>
            <P>由此可看出,startx在不需要传递任何参数的情况下,可以完成启动X的任务,因此,可以如下修改/etc/X11/prefdm脚本来实现自动登录:</P>
            <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee 
            border=1>
              <TBODY>
              <TR>
                <TD><PRE><CODE class=section>

	#!/bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
. /etc/profile.d/lang.sh
# 第一步:查看是否为自动登录
#if [ -f /etc/sysconfig/autologin -a -x /usr/sbin/autologin ]; then
#注释掉上边的条件测试,直接运行startx
		if /usr/X11R6/bin/startx; then	
			exit 0
		fi
#fi
</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P>当然,应确保/etc/inittab中的启动级别为5。</P>
            <P>重新启动系统,会发现系统不验证用户身份,直接进入X 
            window,此时的用户身份为root。但是,如果原来root有自己的桌面、默认shell时,上述方法启动X不一定保证还能拥有原来的设置。为了在启动X后,在避免验证身份的同时,又不改变用户原来的设置,那么在运行startx之前,还有工作要做。</P><BR>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD><IMG height=1 alt="" 
                  src="如何实现自动登录Linux.files/blue_rule.gif" width="100%"><BR><IMG 
                  height=6 alt="" src="如何实现自动登录Linux.files/c.gif" width=8 
                  border=0></TD></TR></TBODY></TABLE>
            <TABLE class=no-print cellSpacing=0 cellPadding=0 align=right>
              <TBODY>
              <TR align=right>
                <TD><IMG height=4 alt="" src="如何实现自动登录Linux.files/c.gif" 
                  width="100%"><BR>
                  <TABLE cellSpacing=0 cellPadding=0 border=0>
                    <TBODY>
                    <TR>
                      <TD vAlign=center><IMG height=16 alt="" 
                        src="如何实现自动登录Linux.files/u_bold.gif" width=16 
                        border=0><BR></TD>
                      <TD vAlign=top align=right><A class=fbox 
                        href="http://www-128.ibm.com/developerworks/cn/linux/l-tip-prompt/tip20/index.html#main"><B>回页首</B></A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR><BR>
            <P><A name=N1008C><SPAN 
            class=atitle>三、自动登录后,保持用户原来的配置(桌面、shell以及其它的一些环境变量)。</SPAN></A></P>
            <P>观察原来/etc/X11/prefdm脚本的自动登录部分:</P>
            <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee 
            border=1>
              <TBODY>
              <TR>
                <TD><PRE><CODE class=section>

......
# 第一步:查看是否为自动登录
if [ -f /etc/sysconfig/autologin -a -x /usr/sbin/autologin ]; then
		if /usr/sbin/autologin; then	
			exit 0
		fi
fi
......
</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P>不难看出,脚本中保留了自动登录的接口:一个可执行文件/usr/sbin/autologin以及一个配置文件/etc/sysconfig/autologin。</P>
            <P>1、/etc/sysconfig/autologin配置文件的实现:</P>
            <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee 
            border=1>
              <TBODY>
              <TR>
                <TD><PRE><CODE class=section>
	
	#config for autologin
	USER=root
	EXEC=/usr/X11R6/bin/startx
	说明,USER指定自动登录时的用户名;EXEC指定启动X要运行的程序。
	</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P>2、/usr/sbin/autologin可执行文件的实现:</P>
            <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#eeeeee 
            border=1>
              <TBODY>
              <TR>
                <TD><PRE><CODE class=section>
	
/*********************
****  autologin.c  ****
*********************/
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;pwd.h&gt;

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";
/*为了能说明问题又保持程序简洁,这里默认登录用户为root,实际上,
登陆用户名应该从/etc/sysconfig/autologin中得到,
程序实现时要注意过滤掉/etc/sysconfig/autologin中的无效用户名*/
	cmd="/usr/X11R6/bin/startx";
/*同样,这里直接指定启动X window的程序,实际上,该程序应该从/etc/sysconfig/autologin中得到*/

	pw = getpwnam(user);
	//getpwnam返回包含用户信息的passwd结构(该结构在pwd.h中定义)。
	if(pw) {
	uid=pw-&gt;pw_uid;		
	gid=pw-&gt;pw_gid;		dir=strdup(pw-&gt;pw_dir);
	shell=strdup(pw-&gt;pw_shell);
	}
	//获得用户相关信息
 else {
	printf("ERROR: No such user %s!\n", user);
	return 1;
	}

	chown("/dev/console", uid, gid);
	chown("/dev/tty", uid, gid);		
	//为控制台和终端设置用户ID及组ID
	
	//下面是设置用户相关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);
	//设置用户相关环境变量

	chdir(dir);
	//切换到用户根目录
	user=NULL;

	execvp(cmd, argv);
/*在配置完用户的相关信息后,执行启动X window操作。注意这里默认执行/usr/X11R6/bin/startx */
	printf("ERROR: Couldn't exec %s: %s\n", cmd, strerror(errno));
	return 2;
}
</CODE></PRE></TD></TR></TBODY></TABLE><BR>
            <P>运行gcc -o autologin 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -