Linux Learning (ii)

Source: Internet
Author: User

Preface: Next Study on Linux (i)

1. Users, groups, and permissions
2. Text Processing Tools
3.vim Text Editor
4.shell Script Programming Basics

One, users, groups and Permissions 1. User 1. Administrator: root,0
 普通用户:1-65535 shell  交互式登录          非交互式登录   /etc/nologin
2. Basic commands for user and user passwords
 vim  查看修改,有颜色

 cat   查看

 cat/etc/passwd 查看用户信息,显示7列,以 : 分隔

登录名:密码:UID:GID:GECOS:家目录:登录的shell   GID:主要组的组id,附加组的在group文件中shell:空白默认为最传统的 sh shell cat/etc/shadow   查看用户密码,显示9列 pwconv   将passwd当中的密码映射到了 /etc/shadow                映射后的passwd     $id$salt$加密后的字符串                                                salt不一样,即使id一样,密码字符串不一样

 vim /etc/shadow- pwunconv 取消映射,密码仍然放在/etc/passwd

 用户名:密码:密码上次修改的日期:密码的最小存活期:密码的最大存活期:密码过期前几天提醒:密码过期后不改密码触发用户过期的时间:用户过期时间:保留位 密码上次修改的日期:距离到1970.1.1的天数(Linux开发出来的日期) 最小存活期:存活期前不能更改密码    "  " date +%s
2. Group 1. Basic commands for Group and group passwords
 cat /etc/group    组   4列   

 组名:组密码:GID:组成员 组密码:当临时需要获取组信息,进入时需要输入密码               通常没有密码需要创建,没有密码无法临时加入,只能root永久加入 组成员:附加组成员 /etc/gshadow组密码 grpconv 将组密码映射到了/etc/gshadow grpunconv 取消组密码的映射

 组名:组密码:管理员:组成员
3. Additional commands about users and groups 1. Users
 useradd/adduser 创建用户                  -u  指定UID   默认从1000开始往后

                    -o 与 -u 配合,不检查UID的唯一性

 xiaohuihui和huitailang 同一个UID,顺序优先先创建的                  -g   指定用户的主要组为某个已存在的组,可以指定GID也可以指定组名                  -G  指定附加组 以逗号为间隔可以同时指定多个,组必须事先存在,可以指定GID也可以指定组名。                  -c   注释信息                  -d   指定目录路径 ,如不指定默认在/home下与用户同名。指定时注意,目录名需已存在,基名不存在。                  -s   指定登录shell如下                  -N  不创建私用组做主组,使用users组做主组                  -r   创建系统用户 (centos 6及之前的版本 < 500, centos 7 < 1000) usermod  修改用户信息                  -u NEWUID 用户名  修改用户的uid                  -g NEWGID 用户名  修改用户的主要组  可以指定组名,也可以指定GID                  -G GID或组名[,组名]  用户名 设定用户的附加组为哪些组,这前的附加组信息被覆盖                  -a 与-G 结合,-aG   使用可以增加用户的附加组,而不会影响之前的附加组                  -s shell名 用户名   修改用户的Shell=chsh  -s  shell 用户名                  -c "描述信息"  设置用户的描述信息   =chfn                  -d /path 用户名  修改用户的家目录,新家目录不会自动创建                  -m                      把用户原来家目录下的文件移到新的家目录                  -d  -m                 若要创建新家目录并移动原家数据,同时使用-m选项  

                  -l newname 用户名   修改用户的名称                  -L    lock指定用户,在/etc/shadow 密码栏的增加 !   等同于passwd -l                  -U    unlock指定用户,将 /etc/shadow 密码栏的 ! 拿掉  等同于passwd -u                  -e YYYY-MM-DD     指明用户账号过期日期                  -f      设定非活动期限  userdel  删除用户                  -r      删除用户名连同家目录及邮箱文件一起删除
2. Group
  groupadd  创建组                  -g GID 组名  创建组  groupmod  修改                 -n newname 组名  修改组名                 -g newgid 组名      修改组id  groupdel    删除组                 -r  删除组名连同成员所有的信息  groupmems   对组成员进行操作  仅root执行                 -g 组名 -a 用户名  将某用户以附加组方式加入到某组当中                   -g 组名 -d 用户名  将某用户从指定组当中踢出                 -g 组名 -p              清空所有组成员                 -g 组名 -l               查看指定组有哪些成员(仅显示附加组)groups 用户名                   查看指定用户属于哪些组(包含主组和附加组)   newgrp 组名(普通用户执行)                   非附加组成员的普通用户在正确输入组密码后可以使用该命令临时变更主要组.                 附加组成员的普通用户无需输入密码可以使用该命令临时变更主要组. 原主要组临时变成附加组    passwd username    修改密码                   普通用户改密码 需要先输入旧密码 不符合要求的密码被拒绝重新输入                 root改密码          直接输入新密码  任意设置密码           passwd  -l  锁定用户  实际就是在密码位前加!!

                  -u  取消用户锁定                   -e  强制用户下次登录时更改密码                  -n  指定最短使用期限                  -x  最大使用期限                  -w 提前多少天开始警告                  -i   非活动期限                  --stdin username  从标准输入接收用户密码 显示密码 只输入一次 只有root可以

     gpasswd  设置组密码  管理员可以执行                  -a 用户名 组名   将某用户以附加组方式加入到某组当中  管理员可以执行                  -d 用户名 组名   将某用户从指定组当中踢出                    管理员可以执行                  -M 用户名[,用户名...]  组名 设置某组的用户列表         仅root执行                  -A 用户名[,用户名...]  组名  设置某组的管理员列表     仅root执行     组管理员在不是组成员的情况下,仅能修改组密码,和将其他人加入组和踢出组,但并不具有组成员的权限
3. Other
     id  -u   显示UID          -g   显示GID          -G  显示用户所属的组的ID          -n   显示名称,需配合-u -g -G使用

      su   username     表示非登录式切换     需要输入密码      su - username     表示登录式切换      su -                      表示登录root      当前用户是root时,切换到其他用户不要密码     chage (修改文件 不建议使用 容易出错)

             -d     上一次修改时间             -E     密码过期时间             -I(大写i)      密码过期之后账户过期             -m    最小存活期             -M    最大             -W    提醒             -l       显示密码策略   查看修改密码信息

                示例:                chage -d 0 tom 下一次登录强制重设密码                chage -m 0 –M 42 –W 14 –I 7 tom                chage -E 2016-09-10 tom
4. Permissions 1. Default Permissions
       chown  更改文件的所属人,只有root可以操作                用户名 filename   只更改owner                用户名.组名 filename 或 用户名:组名 filename  同时更改owner和group                  如果.或:前省略,则只改group                -R 递归

         chgrp  组名  filename              文件的owner可以使用chgrp修改文件的group,但该owner一定要属于目标group                     -R  递归

         权限的表示形式

     -owner-group-other     read-r  write-w  exe-x     d  表示目录          chmod  修改权限                r:4              w:2             x:1                         可以随意组合

               -R 递归               X:只给目录及已有部分x权限的文件加上x权限,不给三个权限位完全没有x的文件加x文件权限                            当仅r权限作用在文件上的时候,用户可以读取该文件的内容:cat less more head tail   当仅w权限作用在文件上的时候,用户可以修改文件的内容:>>   当仅x权限作用在文件上的时候,无作为.   当rw权限作用在文件上的时候,用户即能读与能修改:cat less more head tail vim nano > >>   当rx权限作用在文件上的时候,用户可以执行该文件   当wx权限作用在文件上的时候,权限等同于仅w权限. 文件的常见权限是r--  rw-   rwx目录权限   当仅r权限作用在目录上的时候,用户可以短列出目录下的文件名.   当仅w权限作用在目录上的时候,无作为.   当仅x权限作用在目录上的时候,用户可以进入该目录,并且在知道具体文件名且具有相关权限的情况下,可以访问子文件.   当rw权限作用在目录上的时候,权限等同于仅r权限   当rx权限作用在目录上的时候,用户进入,长列出.   当wx权限作用在目录上的时候,用户可以进入且可以创建及删除文件.但不能使用文件名通配符 目录的常见权限r-x  rwx
2. Special Permissions
      工具和文件都拥有权限      which  查看绝对路径

      对工具拥有权限对文件没有权限不能查看文件

        suid权限(文件),可执行的二进制文件,可以临时调用所属人的权限                    chmod u+s  临时调用文件所属人的权限

          sgid权限(目录)目录所属组的权限                        chmod g+s  对目录操作了sgid权限,任何人在该目录下所创建的文件所属组都与该目录的所属组相同          sticky权限      进行删除操作的权限                        chmod o+t  该目录下的文件只能root,目录的所属人和文件的所属人删除          权限位上原本有x的,加上特殊权限时,为小写                         原本没有x的,加上特殊权限时,为大写

           实验:刘备关羽张飞三个用户有一个共享目录叫shuguo,要求三人的主组不一样的情况下,三人在Shuguo目录下所创建的文件,彼此之间可以读取及修改,但不能删除其他人创建的文件



             验证


            文件特定权限                         chattr +a    不能删除,改名,修改,可以追加                                   +i     不能删除,改名,修改,追加



                          lsattr           查看权限及属性

3.ACL
            acl:除了文件的所属人,所属组,其他,还可以进行更多的设置用户权限            acl生效顺序:所有者,自定义用户,自定义组,其他人            CentOS7中的所有文件系统是划分的文件系统均已默认开启acl             CentOS6随着操作系统安装所有文件系统是话费的文件系统均已默认开启acl

     为多用户或者组的文件和目录赋予访问权限rwx             getfacl      查看权限信息             setfacl  -m  设置user的acl权限                          -x    删除设置的user的acl权限

              setfacl  -b    删除权限包括acl权限属性


               setfacl  -m  m   设置权限上限阀值,修改权限无法超过上限阀值,不显示但是是有效的

               setfacl  -d -m                           -m d        设置默认权限

            setfacl  -k      清除默认权限

            setfacl  -Rm   递归设置,设置的目录及该目录下的所有目录和文件都被设置权限

                setfacl  -M   file1   file2     file1是写有权限的文件,把写有的权限设置file2




                setfacl  -X   file1    file2     file1是写有取消权限的文件,对file2进行取消权限设置        acl权限的备份及还原

     umask值       umask值可以存储在/etc/bashrc当中,使所有用户均被设置                 也可以存在~/.bashrc只对某用户设置    文件最高权限  666        目录最高权限  777        默认权限=最高权限-umask        文件权限,如果umask 有奇数,也就是含有-x ,计算完要加1

5. Other
    手工创建家目录的方法            mkdir /home/username            chmod 700 /home/username/            cp -r /etc/skel/.[^.]*  /home/username/          (..为上一级目录,/etc/skel   新创建的用户的初始化环境变量脚本都在这个目录下)            chown -R username.usename /home/username

        cat /etc/passwd |grep username  检索                                  |grep -E "maidou|biubiu"  同时都检索



2. Text processing tools 1. Various text tools to view, analyze, and statistic text
           cat  查看                cat  -E  显示换行符$

                cat  -n 输出的结果前显示行号

                cat  -v 显示^M,即windows的换行符                 cat  -T 显示^I     即tab                cat  -A = -EvT                                                          cat  -b 输出的结果前显示行号,非空行不参与排列

                cat  -s 将连续的空行压缩为一行

           head   向前截取                  head  -c   N    指定截取前N字节                  head  -n   N    指定截取前N行                head  -N     指定获取N行

                         tail  向后截取                              tail  -c N    指定截取后N字节                              tail  -n N    指定截取后N行                            tail  -N     指定截取N行                            tail  -f    跟踪显示文件新追加的内容,常用于日志监控


                         cut  切割                             cut  -d    指明分隔符                             cut  -f  N ; N,N ; N-N    指明字段                             cut  -c    按字符一个一个切割                             cut  --output-delimiter="  "     指定输出分隔符

                            paste    合并俩个文件,同行的内容在一行显示                                  paste  -d 分隔符:指定分隔符,默认用TAB                                  paste  -s : 所有行合成一行显示

                    wc   文本数据统计                       wc  -l     行数                       wc  -w    单词总数                       wc  -c    字节总数                       wc  -m    字符总数

                   sort     排序                        sort  -r 反方向(由上至下)整理                        sort  -n    按数字大小整理

                        sort  -f 忽略(fold)字符串中的字符大小写

                        sort  -u  (独特, unique)删除输出中的重复行

                        sort  -t c   使用c做为字段界定符                        sort  -k X  列

                    uniq   删除重复的行数                         uniq  -c: 显示每行重复出现的次数                         uniq  -d: 仅显示重复过的行                         uniq  -u: 仅显示不曾重复的行                连续且完全相同方为重复

                    diff f1 f2     比较2文件之间的区别

                     patch  复制其他文件进行的改变

2.grep
            grep       关键字高亮显示            grep  -v     反向搜索            grep  -i     忽略大小写            grep  -n    显示行号            grep  -c    不显示搜索结果,仅显示匹配的行数            grep  -o    仅显示匹配到的字符串            grep  -q    静默输出            grep  -A  N    显示搜索行及其向下相临的N行            grep  -B  N    显示搜索行及其向上相临的N行            grep  -C  N    显示搜索行及其向上和向下相临的N行            grep  -e          -e   多个选项间or关系            grep  -w   匹配整个单词

3. Regular expression meta-characters

位置锚定:定位出现的位置         ^    行首锚定,用于模式的最左侧         $    行尾锚定,用于模式的最右侧         ^    ..     $    用于模式匹配整行         ^$ 空行         ^[[:space:]]\*$    空白行         \<   词首锚定,用于单词模式的左侧         \>   词尾锚定;用于单词模式的右侧         \<  ..  \>    匹配整个单词分组         \(  ..  \) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如: \(root\)         \1, \2, \3, ..    分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中         \1 表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符         后向引用:引用前面的分组括号中的模式所匹配字符, 而非模式本身

           \|     或者

4. Extending regular expression meta-characters

  egrep        egrep=grep  -E        fgrep=grep  -F   不支持正则表达式
3.vim Text Editor
      Vim three main mode command mode (default): Move cursor, copy, cut, paste, undo, redo, search, move cursor into other modes: K H J L Fit! H! L,! J! K can be moved at once!                               Characters W jumps to the next word first B jumps to the first word of the previous word                               H jumps to the top of the current page M jumps to the current page L jumps to the end of the current page                               GG jumps to the first line of the entire file G jumps to the end of the entire file ^ moves to the beginning of a non-whitespace line $ move to the end of the line! G Jump to specify!                         line, or exit mode:! R replaces a character D cut DD to cut the current line! DD Cut!                                Row DW cuts a word d^ from the cursor to the beginning of the line  d$ or D cuts from the cursor to the end of the line                        Y copy yy or y to copy the current line! YY copy! Row yw copy a word y^ copy from the cursor to the beginning of the line y                               $ copy from the cursor location to the end of the line p paste p The current line is pasted down or the cursor is positioned to paste backwards                          P the current line is pasted up or the cursor is positioned forward paste Replace mode R enter ESC exit from command mode into insert mode                               Insert mode: text edit I cursor where to enter I cursor moves to the beginning of the line and into the insertion mode    A cursor moves backward one character at a time into insert mode a cursor moves to the end of the line and enters insert mode O                                The cursor inserts a new row into the insert mode at the same time. O cursor inserts a new row up and into insert mode exit Mode: Save, exit   W Save Q Exit q!    Force exit Wq or X save and exit                            w! The current user is the person who owns the file, if the file is read only, you can add! Force save.                                Returns the command mode in exit mode ESCESC or enter R/path/filename reads the contents of another file and writes the output to a file W/path/filename Save As (verify that the current user has WX permissions on the last level in the path)! command executes an order to display the output on the terminal r! Command executes one of the commands, inserts a new row down the current line where the cursor is located, and enters the output.                                      Command executes one of the commands, replaces the current line where the cursor is located and outputs the result into a search? keywords n Look up N to look down

Linux Learning (ii)

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.