码迷,mamicode.com
首页 > 系统相关 > 详细

Linux课程第三天学习笔记

时间:2016-10-18 23:27:39      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:linux学习   linux课程   linux笔记   

####################2.vim####################
1)vim命令模式
在vim的命令模式下可以配置vim的工作方式
:set nu            ##添加行号
:set nonu        ##取消行号
:set mouse=a        ##添加鼠标选择
:set cursorline        ##显示行线
以上设定都是临时的,永久的设定方式如下:
vim /etc/vimrc        ##此文件为vim的配置文件,在此文件最后加入以上参数。加入参数将默认生效,不需要使用“:”
注意:编辑"/etc/vimrc"这个文件时,不要随意改动里面的内容,否则可能导致vim出问题

####################
vim是全球两大文本编辑器之一
vim有很多参数,自行百度搜索vim用法
####################

2)vim命令模式下关键字搜索
/关键字        ##查找关键字
n向下匹配
N向上匹配

3)vim命令模式下字符的管理
1>字符的复制
yl        ##复制一个字符
y3l        ##复制3个字符
yw        ##复制一个单词
y3w        ##复制3个单词
yy        ##复制一行
y3y        ##复制3行
y3[上键|下键]    ##向上|向下复制4(n+1)行

p        ##粘贴,在当前光标所在位置之后插入复制的内容
P        ##粘贴,在当前光标所在位置之前插入复制的内容
u        ##恢复到未操作之前
ctrl + r    ##恢复到操作之后

2>字符的删除
dl        ##删除一个字母
d3l        ##删除3个字母
dw        ##删除一个单词
d3w        ##删除3个单词
dd        ##删除一行
d3d        ##删除3行
d3[上键|下键]    ##向上|向下删除4(n+1)行

注意:实验发现删除也是剪切,只是不进入插入模式。删除后可以按"p"或"P"进行粘贴

3>字符的剪切
cl        ##剪切一个字母
c3l        ##剪切3个字母
cw        ##剪切一个单词
c3w        ##剪切3个单词
cc        ##剪切一行
c3c        ##剪切3行
d3[上键|下键]    ##向上|向下剪切4(n+1)行
esc --> p    ##剪切过后会进入到插入模式,在执行粘贴动作时,要先按"esc"退出插入模式

4)vim的可视化模式
在命令模式下按“ctrl+v”进入到可视模式
在可视模式下可以使用“上下左右”键,区域选择字符

可视模式下批量添加字符
*>> ctrl+v 选中要加入字符所在列
*>> 按“I”进入插入模式,写入要加入的字符
*>> 按esc

可视模式下批量删除字符
*>> ctrl+v 选中要删除字符所在区域
*>> 按“delete”删除选中区域

5)vim批量修改字符
:%s/原有字符/替换后字符        ##只替换每一行中出现的第一个原有字符
:%s/原有字符/替换后字符/g        ##替换所有
:%s/\t//g            ##把全文的tab键替去掉,"\t"表示tab键
:%s/^\ *//g            ##把全文行首的空格去掉,"^ *"表示行首的若干空格
:%s/^ *//g效果一样
:%s/^\#\ */#/g            ##把全文以#开头后的空格去掉
:%s/^# */#/g效果一样

:%s/\#//g            ##删除全文的"#"
:%s/#//g效果一样
:%s/\ //g            ##删除全文所有的空格
:%s/ //g效果一样
:%s/^\ \#\ */#/g        ##将“[空格]#[若干空格]”替换为"#"
:%s/^ # */#/g效果一样

注意:实验发现" "和"#"不需要加"\"

"%"    ##表示全文,不加%指当前行
"^"    ##以什么为开头
"\"    ##转译字符,比如"t",不加"\"系统会识别为字符"t"
"*"    ##表示若干个,包括没有
"/g"    ##不加"/g"表示只操作每行第一个,加"/g"表示全部操作

####################
man sed        ##sed是一种流编辑器
####################

6)vim的分屏功能
ctrl+w s        ##上下分屏
ctrl+w v        ##左右分屏
ctrl+w c        ##关闭光标所在屏幕
ctrl+w 上|下|左|右    ##光标移动到指定屏幕

####################
分屏后每屏都可以分别保存
vimdiff file1 file2    ##使用vim编辑器比较两个文件的差异
####################

7)vim光标移动
在命令模式下
:help        ##查看vim的用法,":q"退出帮助
:数字        ##移动到指定的行
G        ##文件最后一行
gg        ##文件第一行

在插入模式下
i        ##光标所在位置插入
I        ##光标所在行行首插入
a        ##光标所在字符的下一位插入
A        ##光标所在行行尾插入
o        ##光标所在行下一行插入
O        ##光标所在行上一行插入
s        ##删除光标所在字符插入
S        ##删除光标所在行插入

####################
以下为自己实验:
"Y"表示复制整行
"D"表示从光标处剪切到行尾
"C"表示从光标处剪切到行尾,并且进入插入模式
"x"表示删除当前字符
"X"表示向左删除一个字符
"r"表示替换这个字符
"R"表示进入替换模式
"J"把行末回车变成空格
"h"表示以字符为单位向左移动
"l"表示以字符为单位向右移动
"b"和"B"表示以单词为单位向左移动,光标停留在下一个单词的第一个字符上
"e"表示以单词为单位向右移动,光标停留在下一个单词的最后一个字符上
"w"表示以单词为单位向右移动,光标停留在下一个单词的第一个字符上
"k"表示跳转到上一行相同的位置
"j"表示跳转到下一行相同的位置
"H"表示跳转到当前页面的第一行的第一个有效字符上
"L"表示跳转到当前页面的最后一行的第一个有效字符上
"zz"表示把所在行放置在屏幕中间
"K"表示man [当前字符]
"ZZ"强制保存退出
####################


8)vim的退出模式
:q        ##当用vim打开文件但没有对字符作任何操作时可直接退出
:q!        ##当用vim打开文件并对字符作操作,放弃所有操作退出
:wq        ##保存退出
:wq!        ##强行保存退出,对超级用户及文件所有人生效

使用"wq!"的前提条件是:这个文件必须是你自己的,或者你是超级用户

9)vim手册
vimtutor    ##vim的帮助手册
:q        ##退出vimtutor

####################3.gedit####################
ctrl+n            ##在gedit中打开一个新的tab
ctrl+s            ##保存文件
ctrl+o            ##打开文件
ctrl+x            ##剪切字符
ctrl+v            ##粘贴字符
ctrl+c            ##复制字符
yelp help:gedit        ##gedit的图形帮助手册

######################
######    第五单元    ######
######################

####################1.用户理解####################
用户就是系统使用者的身份
在系统中用户存储为若干窜字符+若干个系统配置文件
用户信息涉及到的系统配置文件:
/etc/passwd    ##用户信息
用户:密码:uid:gid:说明:家目录:用户使用的shell
/etc/shadow    ##用户认证信息
用户:密码:最后一次密码修改该时间:最短有效期:最长有效期:警告期:非活跃期:帐号到期日
/etc/group    ##组信息
组名称:组密码:组id:附加组成员
/etc/gshadow    ##组认证信息
/home/username    ##用户家目录
/etc/skel/.*    ##用户骨架文件

####################
[root@localhost Desktop]# vim /etc/passwd
------------------------------------------------------------
在最下面添加:
westos:x:8888:8888:Westos User:/home/westos:/bin/bash
:wq
------------------------------------------------------------
[root@localhost Desktop]# id westos
uid=8888(westos) gid=8888 groups=8888
[root@localhost Desktop]# su - westos
su: warning: cannot change directory to /home/westos: No such file or directory
id: cannot find name for group ID 8888
mkdir: cannot create directory ‘/home/westos‘: Permission denied
-bash-4.2$ whoami
westos
-bash-4.2$ pwd
/root/Desktop
-bash-4.2$ exit
logout
[root@localhost Desktop]# mkdir /home/westos
[root@localhost Desktop]# su - westos
Last login: Mon Oct 10 22:02:48 EDT 2016 on pts/0
id: cannot find name for group ID 8888
mkdir: cannot create directory ‘/home/westos/.cache‘: Permission denied
-bash-4.2$ logout
[root@localhost Desktop]# ll -d /home/westos
drwxr-xr-x. 2 root root 6 Oct 10 22:42 /home/westos
[root@localhost Desktop]# chown westos.westos /home/westos/
chown: invalid user: ‘westos.westos’
[root@localhost Desktop]# vim /etc/group
------------------------------------------------------------
在最下面添加:
westos:x:8888:
:wq
------------------------------------------------------------
[root@localhost Desktop]# chown westos.westos /home/westos/
[root@localhost Desktop]# ll -d /home/westos
drwxr-xr-x. 2 westos westos 6 Oct 10 23:18 /home/westos
[root@localhost Desktop]# ls -a /home/westos
.  ..
[root@localhost Desktop]# su - westos
Last login: Mon Oct 10 22:05:39 EDT 2016 on pts/0
-bash-4.2$ pwd
/home/westos
-bash-4.2$ ls -a
.  ..  .cache  .config
-bash-4.2$ logout
[root@localhost Desktop]# ls -a /home/westos
.  ..  .bash_history  .cache  .config
[root@localhost Desktop]# ls -a /etc/skel/
.  ..  .bash_logout  .bash_profile  .bashrc  .config  .mozilla
[root@localhost Desktop]# cp /etc/skel/.* /home/westos
cp: omitting directory ‘/etc/skel/.’
cp: omitting directory ‘/etc/skel/..’
cp: omitting directory ‘/etc/skel/.config’
cp: omitting directory ‘/etc/skel/.mozilla’
[root@localhost Desktop]# ls -a /home/westos/
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cache  .config
[root@localhost Desktop]# su - westos
Last login: Mon Oct 10 22:10:53 EDT 2016 on pts/0
[westos@localhost ~]$ logout
[root@localhost Desktop]# passwd westos
Changing password for user westos.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost Desktop]#
####################

####################
/etc/login.defs是设置用户帐号限制的文件,该文件里的配置对root用户无效
如果/etc/shadow文件里有相同的选项,则以/etc/shadow里的设置为准
也就是说/etc/shadow的配置优先级高于/etc/login.defs
####################

####################2.用户管理####################
1)用户建立
useradd        参数    用户名字
        -u    ##指定用户uid
        -g    ##指定用户初始组信息,这个组必须已经存在
        -G    ##指定附加组,这个组必须存在
        -c    ##用户说明
        -d    ##用户家目录
        -s    ##用户所使用的shell,/etc/shells记录了用户能使用shell的名字

一般情况下uid和gid是一样的
新建的用户不能登陆系统,因为它只有用户的身份,没有认证。需要添加用户认证信息,即设置密码

####################
[root@localhost Desktop]# useradd -d /mnt/test test
[root@localhost Desktop]# ll /mnt
total 0
drwx------. 2 test test 6 Oct 11 03:23 test
####################

####################
[root@localhost Desktop]# useradd -s /bin/sh test
[root@localhost Desktop]# su - test
Last failed login: Tue Oct 11 02:19:58 EDT 2016 on pts/0
There were 6 failed login attempts since the last successful login.
-sh-4.2$ ps
  PID TTY          TIME CMD
31220 pts/0    00:00:00 sh    ##使用的shell是sh
31263 pts/0    00:00:00 ps
####################

####################
[root@localhost Desktop]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
注意:其中"/sbin/nologin"和"/usr/sbin/nologin"是不可交互,系统自己使用
####################

2)用户删除
userdel     -r     用户名字
-r表示删除用户信息及用户的系统配置

####################
[root@localhost Desktop]# useradd test
[root@localhost Desktop]# userdel test
[root@localhost Desktop]# useradd test
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@localhost Desktop]# userdel -r test
[root@localhost Desktop]# useradd test
[root@localhost Desktop]#
不加"-r",会有残留文件
####################

3)组的建立
groupadd    -g    ##建立组
groupdel    组名字    ##删除组,前提是这个组下面没有用户

####################
[root@localhost Desktop]# groupadd -g 1001 test
[root@localhost Desktop]# useradd -G 1001 test
useradd: group test exists - if you want to add this user to that group, use -g.
##新建组名是"test",新建用户名也是"test"。"test"被认为是初始组,不能指定为附加组
[root@localhost Desktop]# useradd -g 1001 test
##test指定1001为初始组成功
[root@localhost Desktop]# useradd -G 1001 test1成功
##test1指定1001为附加组成功
####################

4)用户id信息查看
id        参数    用户
        -u    ##用户uid
        -g    ##用户初始组id
        -G    ##用户所有所在组id
        -n    ##显示名称而不是id数字
        -a    ##显示所有信息

####################
[root@localhost Desktop]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
##"context"表示安全上下文
[root@localhost Desktop]# id -u
0
[root@localhost Desktop]# id -g
0
[root@localhost Desktop]# id -G
0
[root@localhost Desktop]# id -n        ##无法显示
id: cannot print only names or real IDs in default format
[root@localhost Desktop]# id -un
root
[root@localhost Desktop]# id -gn
root
[root@localhost Desktop]# id -Gn    ##必须跟"-un"或"-gn"或"-Gn"
root
[root@localhost Desktop]# id -a
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
####################

5)用户信息更改
usermod        参数    用户
        -l    ##更改用户名称
        -u    ##更改uid
        -g    ##更改gid
        -G    ##更改附加组
        -aG    ##添加附加组
        -c    ##更改说明
        -d    ##更改家目录指定
        -md    ##更改家目录指定及家目录名称
        -s    ##更改shell
        -L    ##冻结帐号
        -U    ##解锁

passwd -l和usermod -L的区别:锁定的强度不一样(没密码要先设定密码)
输入一次"passwd -l",加两个!
输入一次"usermod -L",加一个!
再输入一次"usermod -L",再加一个!
也就是说输入两次"usermod -L"的效果,等于输入一次"passwd -l"
查看方法:vim /etc/shadow

做以上实验的监控命令:
watch -n 1 ‘tail -n 3 /etc/passwd /etc/group /etc/shadow ; echo ===== ; ls -l /home ;echo ===== ; ls -l /mnt‘

####################
[root@localhost Desktop]# usermod -c " " test
[root@localhost Desktop]# tail -n 1 /etc/passwd
test:x:1001:1001: :/home/test:/bin/bash

[root@localhost Desktop]# usermod -d /home/westos test
[root@localhost Desktop]# tail -n 1 /etc/passwd
test:x:1001:1001: :/home/westos:/bin/bash    ##已改变
[root@localhost Desktop]# ls /home/
student  test                    ##未改变
[root@localhost Desktop]# usermod -d /home/test test

[root@localhost Desktop]# usermod -md /home/westos test
[root@localhost Desktop]# tail -n 1 /etc/passwd
test:x:1001:1001: :/home/westos:/bin/bash    ##已改变
[root@localhost Desktop]# ls /home/
student  westos                    ##已改变
[root@localhost Desktop]# usermod -md /mnt/westos test
[root@localhost Desktop]# tail -n 1 /etc/passwd
test:x:1001:1001: :/mnt/westos:/bin/bash    ##已改变
[root@localhost Desktop]# ls /home/
student
[root@localhost Desktop]# ls /mnt/
westos                        ##已改变

[root@localhost Desktop]# userdel -r test
[root@localhost Desktop]# useradd test
[root@localhost Desktop]# passwd -S test
test LK 2016-10-10 0 99999 7 -1 (Password locked.)
[root@localhost Desktop]# passwd -u test
Unlocking password for user test.
passwd: Warning: unlocked password would be empty.
passwd: Unsafe operation (use -f to force)
[root@localhost Desktop]# passwd -uf test        ##"passwd -uf"可以强制解锁
Unlocking password for user test.
passwd: Success
[root@localhost Desktop]# passwd -S test
test NP 2016-10-10 0 99999 7 -1 (Empty password.)    ##强制解锁后,密码为空。可直接登陆

[root@localhost Desktop]# useradd westos
[root@localhost Desktop]# passwd -S westos
westos LK 2016-10-10 0 99999 7 -1 (Password locked.)
[root@localhost Desktop]# tail -n 1 /etc/shadow
westos:!!:17085:0:99999:7:::
[root@localhost Desktop]# usermod -U westos
[root@localhost Desktop]# passwd -S westos
westos LK 2016-10-10 0 99999 7 -1 (Password locked.)
[root@localhost Desktop]# tail -n 1 /etc/shadow
westos:!:17085:0:99999:7:::                ##"!!"变成"!"
[root@localhost Desktop]# usermod -U westos
usermod: unlocking the user‘s password would result in a passwordless account.
You should set a password with usermod -p to unlock this user‘s password.
####################

####################
ctrl+a        ##光标移动到行首
ctrl+e        ##光标移动到行尾
####################

####################
[root@foundation50 Desktop]# pwd
/root/Desktop
[root@foundation50 Desktop]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@foundation50 Desktop]# su - kiosk
Last login: Tue Oct 11 09:00:28 CST 2016 on :0
[kiosk@foundation50 ~]$ pwd            ##位置变化
/home/kiosk
[kiosk@foundation50 ~]$ echo $PATH        ##环境变量变化
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/kiosk/.local/bin:/home/kiosk/bin
[kiosk@foundation50 ~]$ exit
logout
[root@foundation50 Desktop]# su kiosk
[kiosk@foundation50 Desktop]$ pwd        ##位置没变化
/root/Desktop
[kiosk@foundation50 Desktop]$ echo $PATH    ##环境变量没变化
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin

echo $PATH查看用户可执行的命令
su - kiosk    ##切换用户及用户环境变量
su kiosk    ##用户环境变量没有变化
####################

####################3.用户权力下放####################
1.在系统中超级用户可以下放普通用户不能执行的操作给普通用户
下放权力配置文件:/etc/sudoers

2.下放权力的方法
*)超级用户执行visudo进入编辑/etc/sudoers模式
*)格式:
获得权限用户    主机名称=(获得到的用户身份)        命令
test        desktop0.example.com=(root)    /usr/sbin/useradd
test用户能在desktop0.example.com以超级用户身份执行/usr/sbin/useradd

注意:建议写在99行的位置

3.执行下放权限命令
sudo    命令    ##如果第一次执行sudo需要输入当前用户密码
在/etc/sudoers中如果设置如下:
test        desktop0.example.com=(root)    NOPASSWD: /usr/sbin/useradd
表示用户调用sudo命令的时候不需要自己密码

####################
[root@localhost Desktop]# visudo
------------------------------------------------------------
     99 test localhost=(root) NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel
:wq
------------------------------------------------------------
[root@localhost Desktop]# su - test
Last login: Tue Oct 11 04:59:42 EDT 2016 on pts/0
[test@localhost ~]$ sudo useradd linux
[test@localhost ~]$ sudo userdel -r linux
####################

############################
####4.用户认证信息的控制####
############################
chage        参数    用户
        -d    ##用户密码组后一次修改的时间,如果设定成0,用户登陆系统后必须修改自己的密码
        -m    ##最短有效期
        -M    ##最长有效期
        -W    ##警告期
        -I    ##用户非活跃天数
        -E    ##帐号到期日格式 -E "YYYY-MM-DD"

####################
[root@localhost Desktop]# useradd hello
[root@localhost Desktop]# echo 123 | passwd --stdin hello    ##设定hello用户的密码为123
Changing password for user hello.
passwd: all authentication tokens updated successfully.
[root@localhost Desktop]# su - student
Last login: Tue Oct 11 04:14:16 EDT 2016 on pts/1
[student@localhost ~]$ su - hello
Password:
Last login: Tue Oct 11 05:08:23 EDT 2016 on pts/0
####################

####################
[root@localhost Desktop]# chage -E 20170101 hello
[root@localhost Desktop]# tail -n 1 /etc/shadow
hello:$6$rVfvEiRc$tnQEF.cgq81jH6XwwhbRq/CnAd7Vl1LNHmPwi/Qa4tsrW/bSrt0NjGsZ4G20stroR7hce.hmum/Z.qUftx9RV.:17085:0:99999:7::20170101:
####################

本文出自 “施超Linux学习笔记” 博客,谢绝转载!

Linux课程第三天学习笔记

标签:linux学习   linux课程   linux笔记   

原文地址:http://shichao.blog.51cto.com/5804953/1863110

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!