Linux基础-运维常用命令


注:本章内容是在不断增加更新的   


    基础命令

    目录

1.    ls

2.    mkdir

3.    pwd

4.    cd

5.    touch

6.    vi/vim

7.    cat

8.    tac

9.    echo

10.    cp

11.    mv

12.    rm

13.    rmdir

14.    grep

15.    sed

16.    awk

17.    head

18.    tail

19.    tree

20.    which

21.    alias

22.    unalias

23.    find

24.    seq

25.    history

26.    whoami

27.    su

28.    wget

29.    diff

30.    vimdiff

31.    useradd

32.    passwd

33.    rpm

34.    yum

35.    chattr

36.    lsattr

37.    whami

38.    w

39.    chkconfig

40.    netstat

41.    sudo

42.    lsof

 

    ls    -    list directory contents    列出目录

    格式:   

           ls    [选项]    [文件]

    选项:

           -l    长格式输出

           -a    显示所有文件,包括隐藏文件,".",".."

           -d    显示目录本身

    示例:

1)
[root@moban etc]# ls -l
total 1680
drwxr-xr-x.  3 root root   4096 Jun 29 22:57 abrt
drwxr-xr-x.  4 root root   4096 Jun 29 22:58 acpi
-rw-r--r--.  1 root root     46 Jul  1 23:55 adjtime
......................................................
2)
[root@moban /]# ls -la
total 106
dr-xr-xr-x. 24 root root  4096 Jul  2 12:53 .
dr-xr-xr-x. 24 root root  4096 Jul  2 12:53 ..
-rw-r--r--.  1 root root     0 Jul  2 12:43 .autofsck
dr-xr-xr-x.  2 root root  4096 Jul  1 11:15 bin
.......................................................
3)
[root@moban ~]# ls -ld /etc
drwxr-xr-x. 82 root root 4096 Jul  2 12:44 /etc


           

    mkdir    -    make directories    创建目录

    格式:   

            mkdir    [选项]    目录

    选项:   

            -p    级联递归

    示例:

1)
[root@moban ~]# mkdir /data
[root@moban ~]# ls -ld /data
drwxr-xr-x. 2 root root 4096 Jul  2 12:53 /data
2)
[root@moban ~]# mkdir /abc/efg/123
mkdir: cannot create directory `/abc/efg/123': No such file or directory
[root@moban ~]# mkdir -p /abc/efg/123
[root@moban ~]# ls -ld /abc/efg/123/
drwxr-xr-x. 2 root root 4096 Jul  2 13:08 /abc/efg/123/



    pwd    -    print name of current/working directory    打印当前的工作目录

    格式:  

            pwd    [选项]

    选项:

            -L    逻辑位置

            -P    物理位置

    示例:

[root@moban init.d]# pwd
/etc/init.d
[root@moban init.d]# pwd -L
/etc/init.d
[root@moban init.d]# pwd -P
/etc/rc.d/init.d

 

           

    cd    -    Change  the  current  directory to dir.    更改当前目录的目录

    格式:

            cd    [选项]    [目录]

    选项:

            .    当前目录

            ..    上一级目录    

            ~    用户家目录

            -    上一级目录

    示例:

[root@moban init.d]# cd
[root@moban ~]# cd /data/
[root@moban data]# pwd
/data
[root@moban data]# cd ~
[root@moban ~]# pwd
/root
[root@moban ~]# cd -
/data
[root@moban data]# pwd
/data
[root@moban data]# cd ..
[root@moban /]# pwd
/


   

    touch    -    change file timestamps    改变文件的时间戳。如果访问的文件不存在,则创建创建文件,文件如果存在,则只是更改文件的时间戳

    格式:

            touch    [选项]    文件

    示例:

[root@moban data]# touch test.txt
[root@moban data]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jul  2 14:24 test.txt



    vi/vim    -     Vi IMproved, a programmers text editor    文本编辑器

    格式:

            vim    [选项]    文件

    操作:

            1)命令模式    在编辑模式按Esc,进入命令模式,

                                   常用命令

                                    :set nu    显示行号

                                    :dd    删除当前行

                                    :yy    拷贝当前行

                                    :p    粘贴

                                    :gg    到文件头部

                                    :G    到文件尾部

                                    :ngg    到制定行,n为数字

            2)编辑模式    在命令模式按a或者i进入编辑模式

            3)保存方式    在命令模式下输入“:wq”或者“:x”

    示例:

[root@moban data]# vim test.txt 
123
~                                                                                                                     
~                                                                                                                     
~                                                                                                                     
:wq 
[root@moban data]# cat test.txt 
123

    


    cat    -    concatenate files and print on the standard output    合并文件和将文件打印到标准输出

    格式:

            cat    [选项]    文件

    选项:

            -n    显示行号

            -b    显示行号,但是不显示空行

            -T    显示tab行,以“^I”的形式

    示例:

[root@moban data]# cat test.txt 
123
[root@moban data]# cat test2.txt 
345
[root@moban data]# cat test.txt test2.txt 
123
345
[root@moban data]# cat -bT test2.txt test.txt 
     1    345
     2    123
     3    ^I^I^I^I

    特殊用法(与“>”“>>”配合使用):   

            “>”    标准输出重定向

            “>>”  标准输出追加重定向

            “<”    标准输入重定向

            “<<”  标准输入追加重定向

    示例:

1)
[root@moban ~]# cat >test.txt
this is test 1
# 这里要按回车,再按Ctrl+C退出
^C
[root@moban ~]# cat test.txt
this is test 1
2)
[root@moban ~]# cat >>456.txt <<EOF
> 123
> 456
> 789
> EOF
[root@moban ~]# cat 456.txt 
this is test 2.
123
456
789




    tac    -    concatenate and print files in reverse    反向合并和打印文件,与cat正好相反

    格式:

            tac    [选项]    文件

    选项:

    示例:

[root@moban ~]# cat 3.txt 
1
2
3
4
5
6
7
8
9
10
[root@moban ~]# tac 3.txt 
10
9
8
7
6
5
4
3
2
1

                        


                 

    echo    -    display a line of text    显示一行文本

    格式:       

            echo    [选项]    内容

    选项:

            -n    输出完毕不进行换行

            -e    使用反斜线解释:可用\n,\t...等

    示例:

[root@moban data]# echo 123
123
[root@moban ~]# echo -e "123\n456"
123
456
[root@moban ~]# echo -n 123
123[root@moban ~]#

    特殊用法(与“>”“>>”配合使用):

    示例:

1)
[root@moban ~]# echo "this is test." > /data/test.txt 
[root@moban ~]# cat /data/test.txt 
this is test.
2)
[root@moban ~]# echo "this is test 2." >> /data/test.txt 
[root@moban ~]# cat /data/test.txt 
this is test.
this is test 2.



    cp    -    copy files and directories    拷贝文件和目录

    格式:

            cp    [选项]    [拷贝源]    [拷贝目标]

    选项:

            -a    相当于“-dR”,文件的所有属性

            -p    连同文件的属性

            -R,-r    拷贝目录选项

    示例:

1)
[root@moban ~]# ls /data/
test2.txt  test.txt
[root@moban ~]# cp 123.txt /data/
[root@moban ~]# ls /data/
123.txt  test2.txt  test.txt
2)
[root@moban ~]# mkdir -p /tmp/test
[root@moban ~]# cp /data/ /tmp/test/
cp: omitting directory `/data/'
[root@moban ~]# cp -r /data/ /tmp/test/
[root@moban ~]# cd /tmp/test/
[root@moban test]# ls -l
total 4
drwxr-xr-x. 2 root root 4096 Jul  2 15:41 data

     特殊用法:

            拷贝文件不提醒的使用方法示例:

[root@moban ~]# cp 1.txt  /tmp/
[root@moban ~]# cp 1.txt  /tmp/
cp: overwrite `/tmp/1.txt'? n
[root@moban ~]# /bin/cp 1.txt  /tmp/
[root@moban ~]# \cp 1.txt  /tmp/
[root@moban ~]#


    mv    -    move (rename) files    移动(重命名)文件

    格式:        

            mv    [选项]    [源]    [目标]

    示例:

[root@moban ~]# ls
123  456  anaconda-ks.cfg  install.log  install.log.syslog
[root@moban ~]# mv 123 789
[root@moban ~]# ls
456  789  anaconda-ks.cfg  install.log  install.log.syslog
[root@moban ~]# mv 789 /tmp/
[root@moban ~]# ls /tmp/
789  yum.log
[root@moban ~]# ls
456  anaconda-ks.cfg  install.log  install.log.syslog
[root@moban ~]# mv 456 /tmp/789
mv: overwrite `/tmp/789'? y



    rm    -    remove files or directories    删除文件或目录

    格式:

            rm    [选项]    文件

    选项:

            -f    强制

            -r ,-R   递归删除目录和内容

    示例:

[root@moban tmp]# ls
789  yum.log
[root@moban tmp]# rm 789
rm: remove regular empty file `789'? 
[root@moban tmp]# rm -f 789
[root@moban tmp]# 
[root@moban tmp]# rm /data
rm: cannot remove `/data': Is a directory
[root@moban tmp]# rm -r /data
rm: descend into directory `/data'?



    rmdir    -    remove empty directories    删除空目录

    格式:

            rmdir    [选项]    目录

    示例:

[root@moban data]# ls /data/123.txt 
/data/123.txt
[root@moban data]# rmdir /data/123.txt 
# 提示目录不是空的,需要将目录文件删除,才可以删除目录
rmdir: failed to remove `/data': Directory not empty
[root@moban data]# rm /data/123.txt 
rm: remove regular file `/data/123.txt'? y
[root@moban data]# ls /data/
[root@moban data]# rmdir /data
[root@moban data]# ls /data
ls: cannot access /data: No such file or directory



    grep    -    print lines matching a pattern    打印匹配的行

    格式:

            grep    [选项]    文件

    选项:

            -v    排除,取非

            -E    相当于egrep

            -n    显示行号

    示例:

1)
[root@moban ~]# cat test.txt 
123
456
abc
[root@moban ~]# grep -v abc test.txt 
123
456
2)
[root@moban ~]# grep -n root test.txt 
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

      


    sed    -    stream editor for filtering and transforming text    用于筛选和编辑的流编辑器,擅长行筛选,而awk更倾向于列筛选

    格式:

            sed    [选项]    文件

    选项:

            -n    取消默认输出

            //p    打印

            //d    删除

            ^    开头标识符

            $    结尾标识符   

    示例:

1)
[root@moban ~]# sed  -n /root/p test.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2)打印含有关键词的行
[root@moban ~]# sed  -n /^root/p test.txt 
root:x:0:0:root:/root:/bin/bash
3)打印指定的范围
[root@moban ~]# sed -n 20,30p 1.txt 
20
21
22
23
24
25
26
27
28
29
30
4)文件的替换
[root@moban ~]# cat test.txt 
topspeedking
[root@moban ~]# sed -i "s#topspeedking#king#g" test.txt 
[root@moban ~]# cat test.txt 
king



    awk    -     pattern scanning and processing language    模式扫描和处理语音

    格式:

            awk    [选项]    文件

    选项:

            -F    指定分隔符

    示例:

[root@moban ~]# awk -F':' '{print$1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
vcsa
abrt
haldaemon
ntp
saslauth
postfix
sshd
oprofile
tcpdump
zhang



    head    -    output the first part of files    输出文件的第一部分,默认输出文件的前10行

    格式:

            -n    n为数字,显示到第n行

    示例:

1)
[root@moban ~]# head 1.txt 
1
2
3
4
5
6
7
8
9
10
2)
[root@moban ~]# head -14 1.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@moban ~]# head -4 1.txt 
1
2
3
4



    tail    -   output the last part of files    输出文件的最后部分,默认输出文件的后10行,与head正好相反

    格式:

            tail    [选项]    文件

    选项:

            -f    动态显示文件内容,适合查看日志

            -n    n为数字,显示到倒数第n行

    示例:

1)
[root@moban ~]# tail 1.txt 
91
92
93
94
95
96
97
98
99
100
2)
[root@moban ~]# tail -5 1.txt 
96
97
98
99
100



    tree    -    list contents of directories in a tree-like format.    以树形格式显示目录结构

    格式:

            tree    [选项]    目录

    选项:

            -L    显示目录深度

    示例:

[root@moban ~]# mkdir -p /data/test/123
[root@moban ~]# tree /data/
/data/
└── test
    └── 123

2 directories, 0 files
[root@moban ~]# tree -L 1 /data/
/data/
└── test

1 directory, 0 files



    which    -    shows the full path of (shell) commands.    显示shell内置命令的完整路径

    格式:

            which    命令

    示例:

[root@moban ~]# which ls
alias ls='ls --color=auto'
    /bin/ls



    alias    -    别名系统

    格式:

            alias    名字=值

    示例:

1)
[root@moban ~]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2)
[root@moban ~]# alias king='ls -l --color=auto'
[root@moban ~]# king
total 56
-rw-r--r--. 1 root root   292 Jul  2 21:33 1.txt
-rw-------. 1 root root  1091 Jun 29 23:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22877 Jun 29 23:00 install.log
-rw-r--r--. 1 root root  6240 Jun 29 22:59 install.log.syslog
-rw-r--r--. 1 root root  4740 Jul  2 21:20 test2.txt
-rw-r--r--. 1 root root  1185 Jul  2 21:42 test.txt



    unalias    -    取消别名

    格式:

            unalias    名字

    示例:

[root@moban ~]# alias king='ls -l --color=auto'
[root@moban ~]# king
total 72
-rw-r--r--. 1 root root   292 Jul  2 21:33 1.txt
-rw-r--r--. 1 root root    54 Jul  3 19:03 2.sh
-rw-r--r--. 1 root root    21 Jul  3 19:03 3.txt
-rw-r--r--. 1 root root   292 Jul  3 19:06 4.txt
-rw-r--r--. 1 root root    10 Jul  3 19:07 5.txt
-rw-------. 1 root root  1091 Jun 29 23:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22877 Jun 29 23:00 install.log
-rw-r--r--. 1 root root  6240 Jun 29 22:59 install.log.syslog
-rw-r--r--. 1 root root  4740 Jul  2 21:20 test2.txt
-rw-r--r--. 1 root root     5 Jul  3 21:53 test.txt
[root@moban ~]# unalias king
[root@moban ~]# king
-bash: king: command not found

    


    find    -    search for files in a directory hierarchy    搜索目录中的文件

    格式:

            find    [路径]    [选项]    

    选项:

            -type    后面可接文件类型,如:f,d...等

            -name    文件名字

            -size    文件大小

            -atime    访问时间

            -mtime    修改时间

    示例:

[root@moban ~]# find /root -name "*.txt"
/root/test.txt
/root/1.txt
/root/test2.txt

  

    特殊用法:将找到的文件转交给后面的命令处理

    1)与exec配合

[root@moban ~]# find /root -name "*.txt" -exec ls -l {} \;
-rw-r--r--. 1 root root 1185 Jul  2 21:42 /root/test.txt
-rw-r--r--. 1 root root 292 Jul  2 21:33 /root/1.txt
-rw-r--r--. 1 root root 4740 Jul  2 21:20 /root/test2.txt

    2)与xargs配合

[root@moban ~]# find /root -name "*.txt" |xargs ls -l 
-rw-r--r--. 1 root root  292 Jul  2 21:33 /root/1.txt
-rw-r--r--. 1 root root 4740 Jul  2 21:20 /root/test2.txt
-rw-r--r--. 1 root root 1185 Jul  2 21:42 /root/test.txt



    seq    -    print a sequence of numbers    打印一系列数字

    格式:

            seq    [选项]    最后的数字

            seq    [选项]    开始数字    最后的数字

            seq    [选项]    开始数字    步长    最后的数字

    选项:

    示例:

1)
[root@moban ~]# seq 10
1
2
3
4
5
6
7
8
9
10
2)
[root@moban ~]# seq 10 20
10
11
12
13
14
15
16
17
18
19
20
3)
[root@moban ~]# seq 1 2 20
1
3
5
7
9
11
13
15
17
19
[root@moban ~]# seq 5 > 5.txt
[root@moban ~]# cat 5.txt 
1
2
3
4
5

    


    

    history    -    命令历史记录

    格式:

            history    [选项]    [命令]

    选项:

            -d    删除指定历史命令

            -c    清空历史记录

    示例:

1)查询历史命令
[root@moban ~]# history 
    1  ifconfig -a
    2  setup
    3  /etc/init.d/network restart
    4  ifconfig 
    5  ifconfig -a
..............................
2)
[root@moban ~]# history -d 507
3)
[root@moban ~]# history -c
[root@moban ~]# history 
    1  history

 


    su    -    切换用户

    示例:

)root切换到普通用户
[root@moban ~]# su - king
[king@moban ~]$ 
2)普通用户切到root,需要root密码
[king@moban ~]$ su - root
Password: 
[root@moban ~]# 
3)普通用户之间的切换,需要知道对方的密码
[zhang@moban ~]$ su - king
Password: 
[king@moban ~]$



    wget    -    The non-interactive network downloader.    Linux下文件下载工具

    格式:

            wget    [选项]    [文件网络地址]

    选项:

            -P    指定下载目录

    示例:

[root@moban data]# wget http://mirrors.sohu.com/apache/httpd-2.2.29.tar.bz2 -P /usr/local/src/
--2015-07-05 21:59:41--  http://mirrors.sohu.com/apache/httpd-2.2.29.tar.bz2
Resolving mirrors.sohu.com... 119.188.36.70
Connecting to mirrors.sohu.com|119.188.36.70|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5625498 (5.4M) [application/octet-stream]
Saving to: “/usr/local/src/httpd-2.2.29.tar.bz2”

100%[============================>] 5,625,498    410K/s   in 13s     

2015-07-05 21:59:59 (420 KB/s) - “/usr/local/src/httpd-2.2.29.tar.bz2” saved [5625498/5625498]

[root@moban data]# ls -l /usr/local/src/
total 5500
-rw-r--r--. 1 root root    2006 Sep 18  2014 CentOS6-Base-163.repo
-rw-r--r--  1 root root 5625498 Sep  2  2014 httpd-2.2.29.tar.bz2

  


    diff    -   compare files line by line    文件行比对工具

    格式:

            diff    [文件    1]    [文件    2]

    示例:

[root@moban data]# echo "123">1.txt
[root@moban data]# echo "1234">2.txt
[root@moban data]# diff 1.txt 2.txt 
1c1
< 123
---
> 1234


 

    vimdiff    -    edit two, three or four versions of a file with Vim and show differences    多个文件编辑,差异比较

    格式:

            vimdiff    文件_1    文件_2

    示例:

[root@moban data]# echo "123756">1.txt 
[root@moban data]# echo "123456">2.txt 
[root@moban data]# vimdiff 1.txt 2.txt 
123756                                   |  123456                                               
  ~                                      |  ~                                                                                   
  ~                                      |  ~                                                    
1.txt                 1,1            All 2.txt                        1,1            All
"2.txt" 1L, 7C



    useradd    -    create a new user or update default new user information    创建用户或者更新新用户的默认信息

    格式:

            useradd    [选项]    用户

    选项:

    示例:

[root@moban data]# useradd king



    passwd    -    update user’s authentication tokens

    格式:

            passwd    [选项]    用户

    选项:

            --stdin    使用标准输入接收

    示例:

1)
[root@moban data]# passwd king
Changing password for user king.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
2)
[root@moban data]# passwd --stdin king
Changing password for user king.
1234abcd
passwd: all authentication tokens updated successfully.



    rpm    -    RPM Package Manager    RPM包管理工具

    格式:

            rpm    [选项]    包名

    选项:

            -i    安装

            -e   卸载

            -v    可视化

            -h    哈希值

            -q    查询

            -a    所有的

            -f    查询指定的命令,是由哪个包安装来的

    示例:

1)
[root@moban ~]# rpm -ivh ntpdate-4.2.6p5-1.el6.centos.x86_64
[root@moban data]# rpm -qa ntpdate
ntpdate-4.2.6p5-1.el6.centos.x86_64
2)
[root@moban data]# rpm -qi ntpdate
Name        : ntpdate                      Relocations: (not relocatable)
Version     : 4.2.6p5                           Vendor: CentOS
Release     : 1.el6.centos                  Build Date: Sun 24 Nov 2013 02:21:55 AM CST
Install Date: Mon 29 Jun 2015 10:58:12 PM CST      Build Host: c6b9.bsys.dev.centos.org
Group       : Applications/System           Source RPM: ntp-4.2.6p5-1.el6.centos.src.rpm
Size        : 121391                           License: (MIT and BSD and BSD with advertising) and GPLv2
Signature   : RSA/SHA1, Mon 25 Nov 2013 03:32:51 AM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.ntp.org
Summary     : Utility to set the date and time via NTP
Description :
ntpdate is a program for retrieving the date and time from
NTP servers.
3)
[root@moban ~]# rpm -qf `which ntpdate`
ntpdate-4.2.6p5-1.el6.centos.x86_64



    yum    -    Yellowdog Updater Modified    Yellowdog更新修改工具

    格式:

            yum    [选项]    包名

    选项:

            list    列出所有包

            grouplist    列出所有包组

            install    安装

            remove    删除

            clean    清除缓存信息

            makecache    生产缓存信息

    示例:

[root@moban ~]# yum install -y ntp
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-3.el6.centos will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-3.el6.centos for package: ntp-4.2.6p5-3.el6.centos.x86_64
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-1.el6.centos will be updated
---> Package ntpdate.x86_64 0:4.2.6p5-3.el6.centos will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================
 Package                Arch                  Version                              Repository              Size
================================================================================================================
Installing:
 ntp                    x86_64                4.2.6p5-3.el6.centos                 updates                593 k
Updating for dependencies:
 ntpdate                x86_64                4.2.6p5-3.el6.centos                 updates                 75 k

Transaction Summary
================================================================================================================
Install       1 Package(s)
Upgrade       1 Package(s)

Total download size: 668 k
Downloading Packages:
(1/2): ntp-4.2.6p5-3.el6.centos.x86_64.rpm                                               | 593 kB     00:00     
(2/2): ntpdate-4.2.6p5-3.el6.centos.x86_64.rpm                                           |  75 kB     00:00     
----------------------------------------------------------------------------------------------------------------
Total                                                                           113 kB/s | 668 kB     00:05     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Updating   : ntpdate-4.2.6p5-3.el6.centos.x86_64                                                          1/3 
  Installing : ntp-4.2.6p5-3.el6.centos.x86_64                                                              2/3 
  Cleanup    : ntpdate-4.2.6p5-1.el6.centos.x86_64                                                          3/3 
  Verifying  : ntpdate-4.2.6p5-3.el6.centos.x86_64                                                          1/3 
  Verifying  : ntp-4.2.6p5-3.el6.centos.x86_64                                                              2/3 
  Verifying  : ntpdate-4.2.6p5-1.el6.centos.x86_64                                                          3/3 

Installed:
  ntp.x86_64 0:4.2.6p5-3.el6.centos                                                                             

Dependency Updated:
  ntpdate.x86_64 0:4.2.6p5-3.el6.centos                                                                         

Complete!




    chattr    -    change file attributes on a Linux file system    更新文件属性信息

    格式:

            chattr    [选项]    文件

    选项:

            +-a    增加或删除“a”属性,“a”属性,只可增加内容,不可删除内容,不可删除文件

            +-i    增加或删除“i”属性,“i”属性,不可增加,不可删除文件,不可修改

    示例:

1)
[root@moban data]# cat 1.txt 
123756
[root@moban data]# chattr +a 1.txt 
[root@moban data]# echo "456">> 1.txt 
[root@moban data]# cat 1.txt 
123756
456
[root@moban data]# echo "456"> 1.txt 
-bash: 1.txt: Operation not permitted
[root@moban data]# rm -f 1.txt 
rm: cannot remove `1.txt': Operation not permitted

2)
[root@moban data]# cat 2.txt 
123456
[root@moban data]# echo "123">>2.txt 
[root@moban data]# cat 2.txt 
123456
123
[root@moban data]# chattr +i 2.txt 
[root@moban data]# echo "123">>2.txt 
-bash: 2.txt: Permission denied
[root@moban data]# rm -f 2.txt 
rm: cannot remove `2.txt': Operation not permitted



    lsattr    -    list file attributes on a Linux second extended file system    列出文件系统的扩展属性,即chattr所增加的属性

    格式:

            lsattr    文件

    示例:

[root@moban data]# lsattr 2.txt 
----i--------e- 2.txt
[root@moban data]# chattr -i 2.txt 
[root@moban data]# lsattr 2.txt 
-------------e- 2.txt


    whoami    -    查询当前系统终端用户

    示例:

[root@moban ~]# whoami
root


 

    w    -   Show who is logged on and what they are doing.    查询当前系统登陆的用户及他们的操作

    示例:

[root@moban ~]# w
 23:20:29 up  6:36,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                23:20    6.00s  0.00s  0.00s -bash
king     pts/0    192.168.1.105    16:45    0.00s  0.16s  0.02s sshd: king [priv]



    chkconfig    -    updates and queries runlevel information for system services    对系统服务更新和查询运行级别信息

    格式:

            chkconfig    []    服务

    选项:

            --list    列出所有服务

            --add    增加服务

            --del    删除服务

            --level    指定级别

            on    服务开启

            off    服务关闭

    示例:

1)
[root@moban ~]# chkconfig --list
abrt-ccpp          0:off    1:off    2:off    3:off    4:off    5:off    6:off
abrtd              0:off    1:off    2:off    3:off    4:off    5:off    6:off
acpid              0:off    1:off    2:off    3:off    4:off    5:off    6:off
atd                0:off    1:off    2:off    3:off    4:off    5:off    6:off
auditd             0:off    1:off    2:off    3:off    4:off    5:off    6:off
2)
[root@moban ~]# chkconfig --list|grep atd
atd                0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@moban ~]# chkconfig atd on
[root@moban ~]# chkconfig --list|grep atd
atd                0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@moban ~]# chkconfig atd off


    

    netstat    -    Print network connections, routing tables, interface statistics, masquerade connec-tions, and multicast memberships    列出网络相关信息,包括网络连接,路由表等

    格式:

            netstat    [选项]

    选项:

            -l    只显示套接字

            -n    以数字形式显示网络地址

            -t    tcp协议

            -p    显示进程及名称

            -a    所有的

    示例:

1)
[root@moban ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22               0.0.0.0:*                   LISTEN      916/sshd            
tcp        0      0 :::22                   :::*                        LISTEN      916/sshd    
2)
[root@moban ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:22               0.0.0.0:*                   LISTEN      
tcp        0      0 192.168.1.111:22         192.168.1.105:50401         ESTABLISHED 
tcp        0      0 :::22                    :::*                        LISTEN      
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     7593   @/com/ubuntu/upstart
unix  2      [ ]         DGRAM                    7972   @/org/kernel/udev/udevd
unix  6      [ ]         DGRAM                    9657   /dev/log
unix  2      [ ]         DGRAM                    11971  
unix  2      [ ]         DGRAM                    10089  
unix  2      [ ]         DGRAM                    10068  
unix  3      [ ]         STREAM     CONNECTED     9897   
unix  3      [ ]         STREAM     CONNECTED     9896   
unix  2      [ ]         DGRAM                    9893   
unix  2      [ ]         DGRAM                    9732   
unix  3      [ ]         DGRAM                    7988   
unix  3      [ ]         DGRAM                    7987

               


    sudo    -    execute a command as another user    指派其他用户来执行命令,一般是用来使普通用户获得root的权限

    格式:

            sudo    命令

    示例:

[king@moban ~]$ sudo cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin



    lsof    -    list open files    列出打开的文件

    格式:

            lsof    [选项]    文件

    选项:

            -i    指定端口

    示例:

1)
[root@moban ~]# lsof 
COMMAND    PID USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
init         1 root  cwd       DIR                8,3     4096          2 /
init         1 root  rtd       DIR                8,3     4096          2 /
init         1 root  txt       REG                8,3   150352     135736 /sbin/init
init         1 root  mem       REG                8,3    65928     130103 /lib64/libnss_files-2.12.so
2)
[root@moban ~]# lsof -i:52113
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     916 root    3u  IPv4   9700      0t0  TCP *:52113 (LISTEN)
sshd     916 root    4u  IPv6   9702      0t0  TCP *:52113 (LISTEN)
sshd    2344 root    3r  IPv4  13595      0t0  TCP 192.168.1.111:52113->192.168.1.105:57016 (ESTABLISHED)
sshd    2347 king    3u  IPv4  13595      0t0  TCP 192.168.1.111:52113->192.168.1.105:57016 (ESTABLISHED)


















  

更新中...






你可能感兴趣的:(linux)