Linux(7):用户管理

用户管理

让一个脚本或命令开机自启动的方法:

# 方法一: 
    把脚本放到  /etc/rc.local  中
# 方法二: 
    把脚本或命令通过 chkconfig 管理
    
# 如何让一个脚本被 chkconfig 管理 ?
# 1). 被 chkconfig 管理的脚本必须要放在 /etc/init.d 下面
[root@NEO ~]# vim /etc/init.d/oldgirld 
[root@NEO ~]# cat /etc/init.d/oldgirld 
echo oldgirl
[root@NEO ~]# ll /etc/init.d/oldgirld
-rw-r--r-- 1 root root 13 Apr  8 21:26 /etc/init.d/oldgirld

# 2). 这个脚本要有执行权限
[root@NEO ~]# chmod +x /etc/init.d/oldgirld 
[root@NEO ~]# /etc/init.d/oldgirld
oldgirl

# 我们平时运行脚本是通过 sh 命令,但系统运行脚本是通过 绝对路径

# 3). 这个脚本的开头要有 chkconfig 指定的内容: # chkconfig: 2345 99 99   ---> #空格chkconfig:空格2345空格99空格99
[root@NEO ~]# vim /etc/init.d/oldgirld
[root@NEO ~]# cat /etc/init.d/oldgirld
# chkconfig: 2345 99 99        # 2345 表示这段脚本被 chkconfig 管理的时候 默认在哪些运行级别上开机启动;第一个99表示这个脚本是第几个开机启动的(开机启动顺序;我们自己写的脚本一般放到99开机顺序;最大也是99);第二个99表示关机顺序 
echo oldgirl

# 4). 让 chkconfig 管理脚本:
[root@NEO ~]# 
[root@NEO ~]# chkconfig --add oldgirld   # oldgirld 这个脚本不需要写 绝对路径,因为这个脚本必须放在 /etc/init.d/ 下面
[root@NEO ~]# chkconfig |grep girl
oldgirld           0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@NEO ~]# 

chkconfig 背后的原理

# chkconfig 也是命令,但却能永久生效,是因为它改变了一些文件,拿 3运行级别来说,它影响的是 /etc/rc3.d/ 下面的文件
[root@NEO ~]# ls /etc/rc3.d/
K01smartd     K74ntpd         K92iptables      S10network     S22messagebus        S50kdump      S99local
K10psacct     K75ntpdate      K99rngd          S11auditd      S25blk-availability  S55sshd       S99oldgirld
K10saslauthd  K75quota_nld    S01sysstat       S12rsyslog     S25netfs             S82abrtd
K15svnserve   K87restorecond  S02lvm2-monitor  S13cpuspeed    S26acpid             S83abrt-ccpp
K30postfix    K89netconsole   S05rdma          S13irqbalance  S26haldaemon         S90crond
K61nfs-rdma   K89rdisc        S08ip6tables     S15mdmonitor   S26udev-post         S95atd            # 这些都是软链接
[root@NEO ~]# chkconfig iptables on
[root@NEO ~]# chkconfig |grep ipt
iptables           0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@NEO ~]# ll /etc/rc3.d/ |grep ipt
lrwxrwxrwx  1 root root 18 Apr  8 22:04 S08iptables -> ../init.d/iptables
[root@NEO ~]# chkconfig iptables off
[root@NEO ~]# ll /etc/rc3.d/ |grep ipt
lrwxrwxrwx  1 root root 18 Apr  8 22:06 K92iptables -> ../init.d/iptables
[root@NEO ~]# chkconfig |grep ipt
iptables           0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@NEO ~]# 

# 由上面可知,chkconfig iptables on/off 对 /etc/rc3.d/ 中的文件 产生了影响:
chkconfig iptables on ====> /etc/rc3.d/ 下面的 S08iptables -> ../init.d/iptables        # S ---> start
chkconfig iptables off  ====> /etc/rc3.d/ 下面的 K92iptables -> ../init.d/iptables        # K ---> kill

# 验证 S 代表 start 和 K 代表 kill
[root@NEO ~]# chkconfig |grep ipt
iptables           0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@NEO ~]# mv /etc/rc3.d/K92iptables /tmp/        # 把这个软链接移动到 /tmp/  , 不要删除
[root@NEO ~]# chkconfig |grep ipt
iptables           0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@NEO ~]# ls -l /etc/rc3.d/ |grep ipt
[root@NEO ~]# ln -s /etc/init.d/iptables /etc/rc3.d/S08iptables        # 给 /etc/init.d/iptables 创建软链接, S08iptables
[root@NEO ~]# ls -l /etc/rc3.d/ |grep ipt
lrwxrwxrwx  1 root root 20 Apr  8 23:18 S08iptables -> /etc/init.d/iptables
[root@NEO ~]# chkconfig |grep ipt
iptables           0:off    1:off    2:off    3:on    4:off    5:off    6:off        # 此时在3级别上已经变成了 开机启动
[root@NEO ~]# 


# 结论:在执行 chkconfig on/off 时,它会在3运行级别(不止3级别)上 创建一个 S 或 K 开头的文件
# 08 和 92 的含义如下:
[root@NEO ~]# head -5 /etc/init.d/iptables 
#!/bin/sh
#
# iptables    Start iptables firewall
#
# chkconfig: 2345 08 92            

# 08 是开机时的顺序, 92 是关机时的顺序

用户分类 与 用户相关文件

# 用户分类:
    1. root 皇帝  uid:0
    2. 虚拟用户 傀儡  uid: 1-499    # 虚拟用户无法被切换到
        2.1 每个程序、服务运行的时候都需要一个用户
        2.2 傀儡用户不需要用来登陆系统
        2.3 傀儡用户的命令解释器: /sbin/nologin    # 区分傀儡用户最核发的标准;正常用户的命令解释器为: /bin/bash
    3. 普通用户 uid: 500+

[root@NEO ~]# id oldboy
uid=500(oldboy) gid=500(oldboy) groups=500(oldboy)
[root@NEO ~]# grep nobody /etc/passwd    # nobody 为 虚拟用户
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@NEO ~]# su - nobody
This account is currently not available.    # 虚拟用户无法登陆


# 用户相关的配置文件:
# /etc/passwd   ---> 存放用户的信息
# /etc/shadow     ---> 存放用户密码信息
# /etc/group     ---> 存放用户、用户组信息
# /etc/gshadow     ---> 存放用户组密码信息

[root@NEO ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@NEO ~]# tail -1 /etc/passwd
oldboy:x:500:500::/home/oldboy:/bin/bash

 root        :x                        :0        :0        :root                :/root            :/bin/bash
 oldboy        :x                        :500    :500    :                    :/home/oldboy    :/bin/bash
# 用户名    原先存放密码的地方        uid        gid        用户的说明信息        用户的家目录    用户的命令解释器(shell;重要)
        (现在密码存放在/etc/shadow)                (添加用户时该列默认为空)            /bin/bash 是用户默认的;/sbin/nologin 是虚拟用户的; 判断是否为虚拟用户的标准
         把x删掉后该用户就没有密码了,可以随便切换到该用户

# 其它的命令解释器:
[root@NEO ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash        # ubuntu 系统的命令解释器
/bin/tcsh        # Unix系统的; tcsh 是 csh 的升级版
/bin/csh        # Unix系统的

-bash-4.1$ 故障分析

猜你喜欢

转载自www.cnblogs.com/neozheng/p/10674547.html