Linux參考筆記 3

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
相關文章
相關標籤/搜索