linux基础学习整理

  • 本文目的是了解linux基础命令,具体深入学习需另外扩展。练习使用系统信息如下
[root@host shl]#cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
  • 清屏:clear

  • 帮助命令:help、man、info

  • 查看文件内容:cat
[root@host ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
  • 屏幕打印信息:echo
[root@host ~]# echo hello world.
hello world.
[root@host ~]# 
  • 显示主机名称:hostname;显示当前路径:pwd;显示当前用户:whoami
[root@host ~]# hostname;pwd;whoami
host.localdomain
/root
root
[root@host ~]#
  • 可使用反斜杠 对命令换行
-bash-4.2$ echo hello
> linux
hellolinux
-bash-4.2$ 
  • 系统区分大小写
-bash-4.2$ pwd
/usr/local/kusy
-bash-4.2$ PWD
-bash: PWD: command not found
-bash-4.2$ 
  • 日期格式化
-bash-4.2$ date '+%Y/%m/%d %H:%M:%S'
2018/11/05 22:36:23
  • 查看指定年月的日历
-bash-4.2$ cal 12 2018
    December 2018   
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
-bash-4.2$ 
  • 几个快捷键:Ctrl+C 中断当前命令、Ctrl+D 退出登录、Shift+PgUp/PgDown 前后翻页

  • 数据同步写入磁盘:sync

  • 列出所有文件详细属性(包括隐藏文件,即文件名第一个字符为 . 的字符)
[root@host kusy]# ls -al
total 32
drwxr-xr-x   5 root root 4096 Nov  6 02:17 .
drwxr-xr-x. 15 root root 4096 Oct 30 21:54 ..
drwxr-xr-x   3 root root 4096 Oct 29 04:07 myproject
drwxr-xr-x   5 root root 4096 Oct 29 04:22 mysoftware
-rw-------   1 root root 1679 Oct 29 23:57 sshkey
-rw-r--r--   1 root root  400 Oct 29 23:57 sshkey.pub
drwxr-xr-x   2 root root 4096 Oct 30 05:10 ssl
-rw-r--r--   1 root root    5 Nov  6 02:17 .test.txt
[root@host kusy]# ll
total 20
drwxr-xr-x 3 root root 4096 Oct 29 04:07 myproject
drwxr-xr-x 5 root root 4096 Oct 29 04:22 mysoftware
-rw------- 1 root root 1679 Oct 29 23:57 sshkey
-rw-r--r-- 1 root root  400 Oct 29 23:57 sshkey.pub
drwxr-xr-x 2 root root 4096 Oct 30 05:10 ssl
  • ll 显示完整时间格式
[root@host kusy]# ll --full-time
total 20
drwxr-xr-x 3 root root 4096 2018-10-29 04:07:42.499263925 -0400 myproject
drwxr-xr-x 5 root root 4096 2018-10-29 04:22:32.612639261 -0400 mysoftware
-rw------- 1 root root 1679 2018-10-29 23:57:49.737676506 -0400 sshkey
-rw-r--r-- 1 root root  400 2018-10-29 23:57:49.737676506 -0400 sshkey.pub
drwxr-xr-x 2 root root 4096 2018-10-30 05:10:11.676048683 -0400 ssl
  • 修改系统默认语言
[root@host kusy]# cat /etc/locale.conf
LANG="en_US.UTF-8"
  • 修改用户、组、权限
    • chgrp:修改文件所属群组
    • chown:修改文件所属用户
    • chmod:修改文件访问权限(读r:4,写w:2,执行x:1)
[root@host test]# ll
total 4
-rw-r--r-- 1 root root 5 Nov  6 03:49 test.txt
[root@host test]# chgrp kusy test.txt
[root@host test]# ll
total 4
-rw-r--r-- 1 root kusy 5 Nov  6 03:49 test.txt
[root@host test]# chown kusy test.txt
[root@host test]# ll
total 4
-rw-r--r-- 1 kusy kusy 5 Nov  6 03:49 test.txt
[root@host test]# chmod 774 test.txt
[root@host test]# ll
total 4
-rwxrwxr-- 1 kusy kusy 5 Nov  6 03:49 test.txt
  • 查看当前用户及所属群组
-bash-4.2$ id kusy
uid=1000(kusy) gid=1000(kusy) groups=1000(kusy)
-bash-4.2$ whoami
zoey
-bash-4.2$ groups
zoey
-bash-4.2$ 
  •  查看系统信息
[root@host ~]# uname -a
Linux host.localdomain 4.10.4-1.el7.elrepo.x86_64 #1 SMP Sat Mar 18 12:50:10 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
  • 目录操作
    • 目录切换:cd(cd / cd~ 表示回到家目录,cd - 表示回到切换前的目录)
    • 显示当前目录:pwd
    • 新建目录:mkdir
    • 删除空目录:rmdir
[root@host usr]# pwd
/usr
[root@host usr]# cd ~
[root@host ~]# pwd
/root
[root@host ~]# cd -
/usr
[root@host usr]# pwd
/usr
[root@host usr]# mkdir testAbc
[root@host usr]# ll testAbc
total 0
[root@host usr]# rmdir testAbc
[root@host usr]# ll testAbc
ls: cannot access testAbc: No such file or directory
  • 环境变量PATH,与Windows中环境变量的意义类似(输入命令时,系统会先到PATH变量中指定的路径搜索可执行文件)
[root@host ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host ~]# PATH="$PATH:/usr"
[root@host ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr
  • 文件基本处理
    • 创建文件:touch
    • 删除文件:rm
    • 复制文件:cp
    • 移动/改名:mv
[root@host test]# ll
total 0
[root@host test]# touch test.txt
[root@host test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  7 03:16 test.txt
[root@host test]# mv test.txt test2.txt
[root@host test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  7 03:16 test2.txt
[root@host test]# cp test2.txt test.txt
[root@host test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  7 03:16 test2.txt
-rw-r--r-- 1 root root 0 Nov  7 03:17 test.txt
[root@host test]# rm test2.txt
rm: remove regular empty file ‘test2.txt’? y
[root@host test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  7 03:17 test.txt
[root@host test]#
  • 获取路径文件名及目录名
[root@host test]# pwd
/usr/local/kusy/test
[root@host test]# basename $PWD
test
[root@host test]# dirname $PWD
/usr/local/kusy
[root@host test]# 
  • 查看文件内容
[root@host test]# echo abcdefg123456>test.txt 
[root@host test]# cat test.txt 
abcdefg123456
[root@host test]# od test.txt 
0000000 061141 062143 063145 030547 031462 032464 005066
0000016
[root@host test]# nl test.txt 
     1    abcdefg123456
[root@host test]# 
  •  文件搜索
    • which:搜索PATH路径下可执行文件
    • whereis:特定目录下搜索文件
    • find:全盘搜索
[root@host test]# ll
total 4
-rw-r--r-- 1 root root 14 Nov  7 03:23 test.txt
[root@host test]# which pwd
/usr/bin/pwd
[root@host test]# whereis test.txt
test: /usr/bin/test /usr/share/man/man1/test.1.gz
[root@host test]# find test.txt
test.txt
  • 查看磁盘使用情况:df
[root@host test]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          247420       0    247420   0% /dev
tmpfs             258040       0    258040   0% /dev/shm
tmpfs             258040   12712    245328   5% /run
tmpfs             258040       0    258040   0% /sys/fs/cgroup
/dev/sda2       10882760 3469384   6840868  34% /
/dev/sda1         388480  145230    222770  40% /boot
overlay         10882760 3469384   6840868  34% /var/lib/docker/overlay2/1eae537748a1b16e224390ea20b70dea76e1bd0f9f56bfa451be833fca59ad48/merged
shm                65536       0     65536   0% /var/lib/docker/containers/27f5c03b4828514fadf18e6ee8babbf39adfb8b730f14ea122fefeed331acc6d/mounts/shm
tmpfs              51608       0     51608   0% /run/user/0
[root@host test]# 
  • 硬链接(指向同一个inode。PS:每个文件占用一个inode,文件内容由inode记录来指向)与软链接(相当于快捷方式)
[root@host test]# ll -i -R
.:
total 4
528553 drwxr-xr-x 2 root root 4096 Nov  7 04:19 cc

./cc:
total 4
528556 -rw-r--r-- 1 root root 14 Nov  7 03:23 test.txt
[root@host test]# ln cc/test.txt .
[root@host test]# ll -i -R
.:
total 8
528553 drwxr-xr-x 2 root root 4096 Nov  7 04:19 cc
528556 -rw-r--r-- 2 root root   14 Nov  7 03:23 test.txt

./cc:
total 4
528556 -rw-r--r-- 2 root root 14 Nov  7 03:23 test.txt
[root@host test]# ln -s cc/test.txt test2.txt
[root@host test]# ll -i -R
.:
total 8
528553 drwxr-xr-x 2 root root 4096 Nov  7 04:19 cc
528559 lrwxrwxrwx 1 root root   11 Nov  7 04:21 test2.txt -> cc/test.txt
528556 -rw-r--r-- 2 root root   14 Nov  7 03:23 test.txt

./cc:
total 4
528556 -rw-r--r-- 2 root root 14 Nov  7 03:23 test.txt
[root@host test]# 
  • 文件打包与压缩
    • tar:打包不压缩
    • zip,gzip,bzip2,xz:一般仅对一个文件进行压缩
    • .tar.gz:tar程序打包后,gzip压缩
[root@host test]# tar cvf test.tar test.txt
test.txt
[root@host test]# ll
total 36
drwxr-xr-x 2 root root  4096 Nov  7 04:19 cc
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
-rw-r--r-- 2 root root 10240 Nov  7 21:28 test.txt
[root@host test]# rm -f test.txt
[root@host test]# gzip -v test.tar
test.tar:     99.5% -- replaced with test.tar.gz
[root@host test]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov  7 04:19 cc
lrwxrwxrwx 1 root root   11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root  132 Nov  7 21:28 test.tar.gz
[root@host test]# zcat test.tar.gz 
test.txt0000644000000000000000000002400013370717510011274 0ustar  rootroot[root@host test]# 
[root@host test]# gzip -d test.tar.gz 
[root@host test]# ll
total 24
drwxr-xr-x 2 root root  4096 Nov  7 04:19 cc
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
[root@host test]# tar xf test.tar 
[root@host test]# ll
total 36
drwxr-xr-x 2 root root  4096 Nov  7 04:19 cc
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
-rw-r--r-- 1 root root 10240 Nov  7 21:28 test.txt
[root@host test]# tar tvf test.tar
-rw-r--r-- root/root     10240 2018-11-07 21:28 test.txt
[root@host test]# 
  • vi文本编辑器,vim程序编辑器
    • 一般模式:打开文件后即进入此模式,可以进行方向键移动,删除、复制、粘贴等操作
      • gg:移到首行
      • G:移至尾行
      • n+Enter:光标向下移动n行
      • /word:向下寻找word字符串
      • dd:删除整行
      • yy:复制整行
      • p:将复制内容粘贴到光标下一行
      • P:将复制内容粘贴到光标上一行
      • u:撤销
      • Ctrl+r:前进
    • 编辑模式:进行编辑,按键i、o、a都可进入此模式,ESC退出模式
    • 命令模式:从一般模式下按冒号进入
      • [:w] :编辑内容写入
      • [:q] :退出vi
      • [:q!] :不保存退出
      • [:wq]、[ZZ] :保存退出
  • ~/.bash_history 记录本次登录以前执行的命令(本次登录执行的命令暂存在内存中,注销后记录到文件里)
[root@host ~]# cat ~/.bash_history
cd /
ls
free
passwd root
clear
pwd
ll
yum install java
java
java -version
python
ls
sudo yum remove docker                   docker-client                   docker-client-latest                   docker-common                   docker-latest                   docker-latest-logrotate                   docker-logrotate                   docker-selinux                   docker-engine-selinux                   docker-engine
sudo yum install -y yum-utils   device-mapper-persistent-data   lvm2
sudo yum-config-manager     --add-repo     https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce
service docker start
docker ps
docker images
docker ps
sudo docker run hello-world
clear
docker ps
docker imagwees
clear
docker images
docker run hello-world
docker pull oddrationale/docker-shadowsocks
docker run -d -p 1991:1991 oddrationale/docker-shadowsocks -s 0.0.0.0 -p 1991 -k 123456 -m aes-256-cfb
docker ps
....
View Code
  • 命令及文件名补齐:TAB键

  • 命令别名设定:alias
[root@host ~]# alias eo='echo'
[root@host ~]# eo hello linux
hello linux
[root@host ~]# 
  • 通配符*,匹配任意字符
[root@host test]# ll
total 28
drwxr-xr-x 2 root root  4096 Nov  7 04:19 cc
-rw-r--r-- 1 root root    44 Nov  7 22:13 test
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
[root@host test]# ll te*
-rw-r--r-- 1 root root    44 Nov  7 22:13 test
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
[root@host test]# ll te*.tar
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar
[root@host test]# ll *
-rw-r--r-- 1 root root    44 Nov  7 22:13 test
lrwxrwxrwx 1 root root    11 Nov  7 04:21 test2.txt -> cc/test.txt
-rw-r--r-- 1 root root 20480 Nov  7 21:28 test.tar

cc:
total 12
-rw-r--r-- 1 root root 10240 Nov  7 21:28 test.txt
[root@host test]# 
View Code
  • 查询命令是否是系统内建命令:type
[root@host test]# type -a ll
ll is aliased to `ls -l --color=auto'
[root@host test]# type pwd
pwd is a shell builtin
[root@host test]# 
  • 变量赋值时等号两边不能有空格
[root@host test]# varA = 123
-bash: varA: command not found
[root@host test]# varA=123
[root@host test]# echo ${varA}
123
[root@host test]# 
  • 使用反引号 ` (键盘上Esc键下面那个)括起的命令会执行
[root@host ~]# echo `echo a`
a
[root@host ~]# echo echo a
echo a
[root@host ~]# 
  • 使用unset取消变量
[root@host ~]# kusy=wangjz
[root@host ~]# echo $kusy
wangjz
[root@host ~]# unset kusy
[root@host ~]# echo $kusy

[root@host ~]# 
  • 不同用户的环境变量值不同
[root@host ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host ~]# su - kusy
Last login: Thu Nov  8 01:52:32 EST 2018 on pts/0
-bash-4.2$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin
-bash-4.2$ 
  • export把程序中的变量输出,在子shell中可以使用
[root@host shl]# cat test1.sh 
export kusy=wangjz
./test2.sh
[root@host shl]# cat test2.sh
echo $kusy
[root@host shl]# ./test1.sh
wangjz
[root@host shl]# 
---------------------------------------------------------
[root@host shl]# cat test1.sh test2.sh 
kusy=wangjz
./test2.sh
echo $kusy
[root@host shl]# ./test1.sh

[root@host shl]# 
  • env:列出环境变量及值
[root@host shl]# env
XDG_SESSION_ID=268
HOSTNAME=host.localdomain
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
...
  • set:查看环境变量及自定义变量信息
[root@host shl]# set
BASH=/bin/bash
...
_=
colors=/root/.dircolors
kusy=wangjz
  • PS1:设置提示字符内容
[root@host shl]# PS1='[u@h W]__$'
[root@host shl]__#
  • $:打印当前shell的PID
[root@host shl]#echo $$
2336
[root@host shl]#cat test1.sh
echo $$
[root@host shl]#./test1.sh
2432
[root@host shl]#
  • ?:命令执行返回的状态码
[root@host shl]#echo a
a
[root@host shl]#echo $?
0
[root@host shl]#xxss
-bash: xxss: command not found
[root@host shl]#echo $?
127
[root@host shl]#
  • history:显示历史命令
[root@host shl]#history 1
    4  history 1
[root@host shl]#history
    1  history
    2  history -1
    3  clear
    4  history 1
    5  history
[root@host shl]#history -c
[root@host shl]#history
    1  history
[root@host shl]#
  • source:读入环境配置文件,无需注销登入

  • /dev/null:空设备,把不要的信息丢进去
[root@host shl]#xxx>11.txt 2>22.txt
[root@host shl]#ll
total 12
-rw-r--r-- 1 root root  0 Nov  8 03:54 11.txt
-rw-r--r-- 1 root root 30 Nov  8 03:54 22.txt
-rwxrwx--- 1 root root  8 Nov  8 02:52 test1.sh
-rwxrwx--- 1 root root 11 Nov  8 02:21 test2.sh
[root@host shl]#xxx>11.txt 2>/dev/null
[root@host shl]#ll
total 12
-rw-r--r-- 1 root root  0 Nov  8 03:55 11.txt
-rw-r--r-- 1 root root 30 Nov  8 03:54 22.txt
-rwxrwx--- 1 root root  8 Nov  8 02:52 test1.sh
-rwxrwx--- 1 root root 11 Nov  8 02:21 test2.sh
[root@host shl]#
  • 将正确信息与错误信息写入同一个文件时: 2>&1(设备号:0=stdin1=stdout2=stderr
[root@host shl]#echo aaa>aa 2>&1
[root@host shl]#bbb>bb 2>&1
[root@host shl]#cat aa
aaa
[root@host shl]#cat bb
-bash: bbb: command not found
[root@host shl]#
  • 输入/输出重定向 :<、<<; >、>>  PS:单箭头覆盖内容,双箭头追加内容
[root@host shl]#cat aa
aa
[root@host shl]#echo bb>aa
[root@host shl]#cat aa
bb
[root@host shl]#echo cc>>aa
[root@host shl]#cat aa
bb
cc
[root@host shl]#cat >bb <aa
[root@host shl]#cat bb
bb
cc
[root@host shl]#
  • &&与||
    • &&:命令1执行正确才执行命令2
    • ||:命令1执行错误才执行命令2
[root@host shl]#echo a && xxx
a
-bash: xxx: command not found
[root@host shl]#xxx && echo a
-bash: xxx: command not found
[root@host shl]#echo a || xxx
a
[root@host shl]#xxx || echo a
-bash: xxx: command not found
a
[root@host shl]#
  • 管道 |:仅处理stout,且能接收成为stdin继续处理的命令才支持,例如:less、more、head、tail命令
[root@host shl]#echo aaaaaaaaaaaaaaaaaa|tail
aaaaaaaaaaaaaaaaaa
  • cut:切取字符,-d代表分隔符,-f指分割后的第几段
[root@host shl]#echo aa-bb-cc | cut -d '-' -f 1
aa
[root@host shl]#echo aa-bb-cc | cut -d '-' -f 1,3
aa-cc
  • grep:查找
[root@host shl]#echo abcdefg|grep 'ef'
abcdefg
[root@host shl]#cat aa
bb
cc
[root@host shl]#grep 'bb' aa
bb
[root@host shl]#
  • wc:查看文件行数,字符数信息
[root@host shl]#cat aa
bb
cc
[root@host shl]#cat aa |wc
      2       2       6(依次是行数、字数、字符数)
[root@host shl]#wc aa
2 2 6 aa
[root@host shl]#wc aa -l
2 aa
[root@host shl]#
  •  tee:将数据同时输出到文件和屏幕
[root@host shl]#echo aa>aa
[root@host shl]#echo bb|tee bb
bb
[root@host shl]#ll
total 16
-rw-r--r-- 1 root root  3 Nov  8 04:51 aa
-rw-r--r-- 1 root root  3 Nov  8 04:52 bb
-rwxrwx--- 1 root root  8 Nov  8 02:52 test1.sh
-rwxrwx--- 1 root root 11 Nov  8 02:21 test2.sh
[root@host shl]#
  • tr:删除或替换文字
[root@host shl]#echo abcdef|tr -d 'cd'
abef
[root@host shl]#echo abcdef|tr 'cd' 'ee'
abeeef
[root@host shl]#
  •  shell脚本基础使用
[root@host shl]#cat test.sh 
#!/bin/bash
#Program Name:test.sh
#Function:some test of shell
#History:
#  Auto     Date      Version
#  Kusy   2018-11-09   1.0
read -p 'input your score:' score
score=$(($score+2))
#test loop
while [ 1 == 1 ]
do
    echo while loop
    break
done
for (( i=1; i<4; i=i+1 ))
do
    echo for loop $i
done

function tellresult(){
    if [ $1 -gt 100 ];then
        echo perfect!
    elif [ $1 -gt 90 ];then
        echo good!
    else
        echo general!
    fi
}
#test condition
tellresult $score
[root@host shl]#sh test.sh 
input your score:0
while loop
for loop 1
for loop 2
for loop 3
general!
[root@host shl]#sh test.sh 
input your score:98
while loop
for loop 1
for loop 2
for loop 3
good!
[root@host shl]#sh test.sh 
input your score:100
while loop
for loop 1
for loop 2
for loop 3
perfect!
[root@host shl]#
  • 用户管理:useradd,passwd,userdel
[root@host shl]#useradd testguest
[root@host shl]#ll /home/testguest -d
drwx------ 2 testguest testguest 4096 Nov  8 22:27 /home/testguest
[root@host shl]#passwd testguest
Changing password for user testguest.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@host shl]#userdel -r testguest
[root@host shl]#id testguest
id: testguest: no such user
  •  子进程与父进程
[root@host shl]#bash
[root@host shl]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  2336  2334  0  80   0 - 28896 wait   pts/0    00:00:00 bash
4 S     0  3012  2336  0  80   0 - 28895 wait   pts/0    00:00:00 bash
0 R     0  3022  3012  0  80   0 - 37249 -      pts/0    00:00:00 ps
[root@host shl]# exit
exit
[root@host shl]#ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  2336  2334  0  80   0 - 28896 wait   pts/0    00:00:00 bash
0 R     0  3023  2336  0  80   0 - 37249 -      pts/0    00:00:00 ps
[root@host shl]#
  • 观察自己bash的进程ps -l ,观察系统所有进程 ps aux(TTY:?表示与终端机无关,tty1-tty6表示是本机登入者进程,pts/0表示由网络连接进主机的进程)
[root@host shl]#ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  2336  2334  0  80   0 - 28896 wait   pts/0    00:00:00 bash
0 R     0  3074  2336  0  80   0 - 37249 -      pts/0    00:00:00 ps
[root@host shl]#ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.6  43280  3128 ?        Ss   Oct28   0:16 /usr/lib/systemd/systemd --system --des
root         2  0.0  0.0      0     0 ?        S    Oct28   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        S<   Oct28   0:00 [kworker/0:0H]
...
  • top:持续监控进程状态,-d n 每n秒刷新一次,-p pid指定监控进程的pid
[root@host shl]#top -d 2 -p 2336
top - 00:48:30 up 11 days,  2:48,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.5 us,  0.0 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   516080 total,   220880 free,    61420 used,   233780 buff/cache
KiB Swap:   135164 total,    55896 free,    79268 used.   415428 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                             
 2336 root      20   0  115584   3456   2972 S  0.0  0.7   0:00.53 bash     
  • kill -9 pid:强制杀进程
[root@host ~]# ps aux | grep 'python'
root      1069  0.0  0.5 553160  2712 ?        Ssl  Oct28   1:58 /usr/bin/python -Es /usr/sbin/tuned -l -P
root      3144  0.3  1.5 129544  8064 pts/0    S+   01:01   0:00 python
root      3147  0.0  0.4 112716  2224 pts/1    R+   01:01   0:00 grep --color=auto python
root     19728  0.0  1.4  42724  7316 ?        Ss   Oct28   0:26 /usr/bin/python /usr/local/bin/ssserver -s 0.0.0.0 -p 1991 -k 123456 -m aes-256-cfb
[root@host ~]# kill -9 3144
[root@host ~]# ps aux | grep 'python'
root      1069  0.0  0.5 553160  2712 ?        Ssl  Oct28   1:58 /usr/bin/python -Es /usr/sbin/tuned -l -P
root      3149  0.0  0.4 112716  2200 pts/1    S+   01:01   0:00 grep --color=auto python
root     19728  0.0  1.4  42724  7316 ?        Ss   Oct28   0:26 /usr/bin/python /usr/local/bin/ssserver -s 0.0.0.0 -p 1991 -k 123456 -m aes-256-cfb
[root@host ~]# 
  • free:查看内存使用情况(-m 代表以M为单位,默认Kb)
[root@host ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            503          62         207           4         234         403
Swap:           131          77          54
[root@host ~]# 
  • uptime:显示系统开机多长时间,及1、5、15分钟平均负载
[root@host ~]# uptime
 01:08:11 up 11 days,  3:08,  2 users,  load average: 0.00, 0.00, 0.00
[root@host ~]# 
  • netstat:网络监控相关
[root@host ~]# netstat -atunlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1652/master         
tcp        0      0 0.0.0.0:29770           0.0.0.0:*               LISTEN      1076/sshd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      29865/nginx: master 
tcp        0      0 144.34.157.238:29770    210.22.142.220:49325    ESTABLISHED 2334/sshd: root@pts 
tcp        0     52 144.34.157.238:29770    210.22.142.220:57794    ESTABLISHED 3127/sshd: root@pts 
tcp6       0      0 ::1:25                  :::*                    LISTEN      1652/master         
tcp6       0      0 :::1991                 :::*                    LISTEN      19707/docker-proxy  
tcp6       0      0 :::29770                :::*                    LISTEN      1076/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           845/dhclient        
udp        0      0 0.0.0.0:49232           0.0.0.0:*                           845/dhclient        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           757/chronyd         
udp6       0      0 :::53154                :::*                                845/dhclient        
udp6       0      0 ::1:323                 :::*                                757/chronyd         
[root@host ~]# 
  • yum:软件安装,升级,删除
[root@host ~]# rpm -q httpd
package httpd is not installed
[root@host ~]# yum install httpd
Loaded plugins: fastestmirror
base                                                                             | 3.6 kB  00:00:00     
...      
Complete!
[root@host ~]# yum list httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fileplanet.com
 * elrepo-kernel: repos.lax-noc.com
 * extras: repos-lax.psychz.net
 * updates: mirror.fileplanet.com
Installed Packages
httpd.x86_64                               2.4.6-80.el7.centos.1                                @updates
[root@host ~]# yum update httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.fileplanet.com
 * elrepo-kernel: repos.lax-noc.com
 * extras: repos-lax.psychz.net
 * updates: mirror.fileplanet.com
No packages marked for update
[root@host ~]# yum remove httpd
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be erased
...
Removed:
  httpd.x86_64 0:2.4.6-80.el7.centos.1                                                                  

Complete!
##清除已下载的软件数据
[root@host ~]# yum clean all Loaded plugins: fastestmirror Cleaning repos: base docker-ce-stable elrepo-kernel extras jenkins nginx updates Cleaning up everything Cleaning up list of fastest mirrors
  • 源码安装软件(wget下载源码,configure 生成Makefile,然后make & make install)
[root@host mysoftware]# wget http://linux.vbird.org/linux_basic/0520source/ntp-4.2.8p3.tar.gz
--2018-11-09 02:16:21--  http://linux.vbird.org/linux_basic/0520source/ntp-4.2.8p3.tar.gz
Resolving linux.vbird.org (linux.vbird.org)... 140.116.44.180
Connecting to linux.vbird.org (linux.vbird.org)|140.116.44.180|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7099575 (6.8M) [application/x-gzip]
Saving to: ‘ntp-4.2.8p3.tar.gz’

100%[==============================================================>] 7,099,575   2.15MB/s   in 3.3s   

2018-11-09 02:16:24 (2.05 MB/s) - ‘ntp-4.2.8p3.tar.gz’ saved [7099575/7099575]
[root@host mysoftware]# tar -zxvf ntp-4.2.8p3.tar.gz
...
[root@host mysoftware]# cd ntp-4.2.8p3
[root@host ntp-4.2.8p3]# ./configure --prefix=/usr/local/kusy/mysoftware/ntp
[root@host ntp-4.2.8p3]# ll Make*
-rw-r--r-- 1 root 6011 34549 Nov  9 02:27 Makefile
-rw-rw-r-- 1  427 6011  3778 Jun 29  2015 Makefile.am
-rw-rw-r-- 1  427 6011 35586 Jun 29  2015 Makefile.in
[root@host ntp-4.2.8p3]# ll /usr/local/kusy/mysoftware/ntp
total 0
[root@host ntp-4.2.8p3]# make clean & make
...
[root@host ntp-4.2.8p3]# make check
...
[root@host ntp-4.2.8p3]# make install
...
[root@host ntp-4.2.8p3]# ll /usr/local/kusy/mysoftware/ntp
total 16
drwxr-xr-x 2 root root 4096 Nov  9 02:35 bin
drwxr-xr-x 2 root root 4096 Nov  9 02:35 libexec
drwxr-xr-x 2 root root 4096 Nov  9 02:35 sbin
drwxr-xr-x 5 root root 4096 Nov  9 02:35 share
[root@host ntp-4.2.8p3]#

参考资料:《鸟哥的linux私房菜》,百度

原文地址:https://www.cnblogs.com/kusy/p/9914678.html