4、Vim文本编辑器 1. 三种模式 命令模式:文件打开后的默认模式,只能查看文件内容不能修改 输入模式:能够编辑和修改 末行模式:保存退出 2. 切换 命令模式 --> 输入模式 按i键 命令模式 --> 末行模式 按:键 输入模式和末行模式 --> 命令模式 按Esc键 备注:输入模式和末行模式不能直接切换,须要通过命令模式 3. vim filename 若是filename存在则打开这个文件 若是filename不存在则新建这个文件 实验 1. 在 /root/ 目录下新建文件 hello.sh 1)录入内容“Hello World !!!” 2)保存后使用 cat 命令确认文件内容 2. 修改系统文件 /etc/hosts 1)在末尾增长一行内容“127.0.0.1 www.baidu.com” 2)使用 ping 命令测试到 www.baidu.com 的连通性,观察结果 # ls /root/hello.sh # vim /root/hello.sh 按i键 输入 Hello World!!! 按esc键 按: wq! # ls /root/hello.sh # cat /root/hello.sh 4.命令模式操做 光标行内调整 ^ = Home键 移动光标到行首 $ = End键 移动光标到行尾 光标行间的调整 gg 跳转到文件的第一行 G 跳转到文件的最后一行 复制,粘贴,删除 yy 复制当前行 #yy 复制当前往下#行 p 当前光标下粘贴 delete 删除当前光标所在的单个字符 dd 删除(剪切)当前行 #dd 删除(剪切)当前光标往下到#行 查找 /world 当前光标往下查找world n 下一个 eg: [root@ntd1711 ~]# rm -rf /tmp/* [root@ntd1711 ~]# mkdir /tmp/test01 [root@ntd1711 ~]# cp /etc/mail.rc /tmp/test01/ [root@ntd1711 ~]# ls /tmp/test01/mail.rc [root@ntd1711 ~]# vim /tmp/test01/mail.rc 5.末行模式操做 :w 保存 :q 退出 :wq 保存并退出 :wq! 强制保存并退出 :w /root/xxx.file 把当前文件另存为/root/xxx.file :r /root/xxx.file 把/root/xxx.file文件加载到当前文件中 6.查找替换 :s/old/new 替换当前行第一个old为new :s/old/new/g 替换当前行全部的old为new :n,m s/old/new/g 替换第n-m行全部的old为new :% s/old/new/g 替换文件内全部的old为new u 撤销 eg: [root@ntd1711 test01]# ls /etc/passwd /tmp/test01/passwd [root@ntd1711 test01]# cp /etc/passwd /tmp/test01/ [root@ntd1711 test01]# ls /etc/passwd /tmp/test01/passwd [root@ntd1711 test01]# vim /tmp/test01/passwd 在末行模式输入 /root :s/root/feige u :s/root/feige/g u :1,10s/root/feige/g u :%s/root/feige/g u :q! 显示和关闭行号 :set nu|nonu 5、管理用户和组 1.用户管理 a.用户分类 超级用户:管理员帐户root uid为0 系统用户:系统服务产生 uid范围 1 ~ 999 普通用户:管理员本身建立的帐户,uid范围 1000 ~ 60000 b.建立用户 # id 帐户名 验证系统是否存在这个帐户 # useradd 帐户名 建立帐户 c.设置密码 #passwd 帐户 设置密码 d.修改帐户信息 #usermod -l 新帐户 旧帐户 修改登陆名字 e.删除帐户 #userdel 帐户 删除帐户 -r 连同家目录一块儿删除 总结: 当默认建立一个普通用户的时候,会在/home下建立一个同名的文件夹。 这个文件夹就是建立用户的家目录 eg: [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# useradd nvshen [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# passwd nvshen [root@ntd1711 ~]# id miaodt [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# usermod -l miaodt nvshen [root@ntd1711 ~]# id miaodt [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# usermod -l nvshen miaodt [root@ntd1711 ~]# id miaodt [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# userdel nvshen 实验: 1.新建名为 nvshen 的用户帐号,将密码设置为 1234567 测试以用户 nvshen 远程登陆到本机系统 2.将此用户的家目录迁移到 /opt/nvshen 目录 从新以用户 nvshen 远程登陆本机系统,确认当前 pwd 工做目录位置 3.完全删除名为 nvshen 的用户帐号 检查其ID信息,查看提示结果。检查其家目录是否可用 [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# useradd nvshen [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# passwd nvshen [root@ntd1711 ~]# ssh nvshen@127.0.0.1 [nvshen@ntd1711 ~]$ pwd [nvshen@ntd1711 ~]$ whoami [nvshen@ntd1711 ~]$ exit [root@ntd1711 ~]# ls -ld /opt/nvshen [root@ntd1711 ~]# ls -ld /home/nvshen/ [root@ntd1711 ~]# usermod -d /opt/nvshen nvshen [root@ntd1711 ~]# ls -ld /opt/nvshen [root@ntd1711 ~]# mv /home/nvshen/ /opt/ [root@ntd1711 ~]# ls -ld /opt/nvshen [root@ntd1711 ~]# ssh nvshen@127.0.0.1 [nvshen@ntd1711 ~]$ pwd [nvshen@ntd1711 ~]$ exit [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# ls -ld /opt/nvshen/ [root@ntd1711 ~]# userdel nvshen [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# ls -ld /opt/nvshen/ 2.组管理 a.建立组 #groupadd 组名 -g gid 建立组的时候指定gid b.给组添加删除成员(用户) #gpasswd -a:添加指定用户为组成员 -d:从组内删除指定的成员用户 c.删除组 #groupdel eg: [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# useradd nvshen [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# groupadd -g 600 stugrp [root@ntd1711 ~]# gpasswd -a nvshen stugrp [root@ntd1711 ~]# id nvshen [root@ntd1711 ~]# groupdel stugrp [root@ntd1711 ~]# id nvshen