linux常用命令
查看系统信息查看内核/操作系统/CPU信息 uname -a查看操作系统版本 cat /etc/redhat-release查看CPU信息 cat /proc/cpuinfo查看计算机名 hostname查看环境变量 env
查看系统运行资源查看内存信息 free -h查看各分区使用情况 df -h查看指定目录的大小 du -sh ./*查看内存相关信息 cat /proc/meminfo查看系统运行时间、用户数、负载
查看网络信息查看所有网络接口的属性 ifconfig / ip addr查看防火墙设置 iptables -L查看iptable规则设置情况打开端口 8089
查看防火墙设置 systemctl status firewalld查看路由表 route -nnetstat查看所有监听端口 netstat -lntp查看所有已经建立的连接 netstat -antp查看网络统计信息 netstat -s
查看进程查看所有进程 ps -ef实时显示进程状态 top
查看系统安装程序查看所有安装的软件包 rpm -qa
远程拷贝
查看系统信息
查看内核/操作系统/CPU信息 uname -a
[root@localhost log
]
-a, --all 以如下次序输出所有信息。其中若-p 和
-i 的探测结果不可知则被省略:
-s, --kernel-name 输出内核名称
-n, --nodename 输出网络节点上的主机名
-r, --kernel-release 输出内核发行号
-v, --kernel-version 输出内核版本
-m, --machine 输出主机的硬件架构名称
-p, --processor 输出处理器类型或
"unknown"
-i, --hardware-platform 输出硬件平台或
"unknown"
-o, --operating-system 输出操作系统名称
--help 显示此帮助信息并退出
--version 显示版本信息并退出
[root@localhost log
]
Linux localhost.docker.demo 3.10.0-957.el7.x86_64
[root@localhost log
]
Linux
[root@localhost log
]
localhost.docker.demo
[root@localhost log
]
3.10.0-957.el7.x86_64
[root@localhost log
]
[root@localhost log
]
x86_64
[root@localhost log
]
x86_64
[root@localhost log
]
x86_64
[root@localhost log
]
GNU/Linux
查看操作系统版本 cat /etc/redhat-release
[root@localhost ~
]
CentOS Linux release 7.6.1810
(Core
)
查看CPU信息 cat /proc/cpuinfo
[root@localhost ~
]
processor
: 0
vendor_id
: GenuineIntel
cpu family
: 6
model
: 142
model name
: Intel
(R
) Core
(TM
) i5-8250U CPU @ 1.60GHz
stepping
: 10
microcode
: 0xc6
cpu MHz
: 1800.000
cache size
: 6144 KB
physical
id : 0
siblings
: 1
core
id : 0
cpu cores
: 1
apicid
: 0
initial apicid
: 0
fpu
: yes
fpu_exception
: yes
cpuid level
: 22
wp
: yes
flags
: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
bogomips
: 3600.00
clflush size
: 64
cache_alignment
: 64
address sizes
: 42 bits physical, 48 bits virtual
power management:
查看计算机名 hostname
[root@localhost ~
]
localhost.docker.demo
查看环境变量 env
查看系统运行资源
查看内存信息 free -h
[root@localhost ~
]
-h, --human show human-readable output
-m, --mega show output
in megabytes
-l, --lohi show detailed low and high memory statistics
-t, --total show total
for RAM + swap
-s N, --seconds N repeat printing every N seconds
[root@localhost ~
]
total used
free shared buff/cache available
Mem: 1.8G 1.0G 68M 345M 688M 185M
Swap: 2.0G 503M 1.5G
[root@localhost ~
]
total used
free shared buff/cache available
Mem: 1819 1062 68 342 688 188
Swap: 2047 502 1545
[root@localhost ~
]
total used
free shared buff/cache available
Mem: 1.8G 1.0G 67M 345M 688M 184M
Low: 1.8G 1.7G 67M
High: 0B 0B 0B
Swap: 2.0G 503M 1.5G
Total: 3.8G 1.5G 1.6G
total: 内存总数 used: 已经使用内存数 free: 完全空闲内存 shared: 多个进程共享的内存 buffers: 用于块设备数据缓冲,记录文件系统metadata(目录,权限,属性等) cached: 用于文件内容的缓冲 available:真正剩余的可被程序应用的内存数
查看各分区使用情况 df -h
[root@localhost ~
]
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 17G 27M 100% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 90M 821M 10% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
overlay 17G 17G 27M 100% /var/lib/docker/overlay2/a2888dd274ee0f4731a73eb1c758656e66091a4f4fbd19f0a5e980d70a055950/merged
shm 64M 0 64M 0% /var/lib/docker/containers/b1882b92772afb5ae7400b5ce0a4211246f9172e9acab5139334add9f9f58d72/shm
tmpfs 182M 0 182M 0% /run/user/0
overlay 17G 17G 27M 100% /var/lib/docker/overlay2/51dbcc0f009bb99993cae6ada341592c57efc7065390fde5b73fcbc28ab221ec/merged
shm 64M 0 64M 0% /var/lib/docker/containers/ee8cb63a90c9d474c0efc81239b2262f21ab82a573d58a3f2868ba286c5edd7c/shm
overlay 17G 17G 27M 100% /var/lib/docker/overlay2/d38d8ef89307aa0081aac876a898b0b39cb2ba51991ff731e97af01d840fb437/merged
shm 64M 0 64M 0% /var/lib/docker/containers/115e3a6d06011d6a393ab130424afb44055eb9810462fe267019a9224a556b4d/shm
查看指定目录的大小 du -sh ./*
[root@localhost src
]
-h, --human-readable print sizes
in human readable
format (e.g., 1K 234M 2G
)
-s, --summarize display only a total
for each argument
[root@localhost src
]
nginx-1.16.0 nginx-1.16.0.tar.gz nginx_related solr-7.7.2.tgz
[root@localhost src
]
215M ./
[root@localhost src
]
19M ./nginx-1.16.0
1012K ./nginx-1.16.0.tar.gz
32M ./nginx_related
164M ./solr-7.7.2.tgz
查看内存相关信息 cat /proc/meminfo
[root@localhost ~
]
MemTotal: 1863224 kB
MemFree: 78908 kB
MemAvailable: 209124 kB
Buffers: 0 kB
Cached: 611012 kB
SwapCached: 56960 kB
查看系统运行时间、用户数、负载
[root@localhost ~
]
19:12:02 up 2:35, 2 users, load average: 0.02, 0.09, 0.07
查看网络信息
查看所有网络接口的属性 ifconfig / ip addr
查看防火墙设置 iptables -L
查看iptable规则设置情况
[root@localhost ~
]
Chain INPUT
(policy ACCEPT
)
Chain FORWARD
(policy DROP
)
Chain OUTPUT
(policy ACCEPT
)
--list -L [chain [rulenum]] List the rules in a chain or all chains --numeric -n numeric output of addresses and ports 用数字的形式输出 ip地址和端口
打开端口 8089
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
Chain INPUT
(policy ACCEPT
)
target prot opt
source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8089
Chain OUTPUT
(policy ACCEPT
)
target prot opt
source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:8089
--append -A chain Append to chain 添加一条 INPUT或者OUTPUT 的规则 --protocol -p proto protocol: by number or name, eg. tcp’ 指定协议,常用是tcp 协议 或者udp 协议,比如53端口的DNS要使用udp协议 --dport 目标端口 当数据从外部进入服务器为目标端口 --sport 源端口 数据从服务器出去 --jump -j target target for rule (may load target extension) -j 就是指定是 ACCEPT 接收 或者 DROP 不接收
查看防火墙设置 systemctl status firewalld
centos 7 放开端口:3306 删除端口:9999 接口转发:客户端访问80端口,服务器由80端口转发到9999端口。 删除转发端口。 查看所有放开的端口。 查看firewalld防火墙状态 重启firewalld防火墙
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
[root@localhost ~
]
需重启 firewalld
查看路由表 route -n
[root@localhost ~
]
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.109.2 0.0.0.0 UG 100 0 0 ens33
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.109.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
netstat
[root@localhost ~]# netstat --help -l, --listening display listening server sockets -n, --numeric don’t resolve names -p, --programs display PID/Program name for sockets -s, --statistics display networking statistics (like SNMP) ={-t|–tcp} {-u|–udp}
查看所有监听端口 netstat -lntp
[root@localhost ~
]
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 7159/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7394/master
tcp6 0 0 :::3306 :::* LISTEN 17664/docker-proxy-
tcp6 0 0 :::22 :::* LISTEN 7159/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7394/master
查看所有已经建立的连接 netstat -antp
[root@localhost ~
]
Active Internet connections
(servers and established
)
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 7159/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7394/master
tcp 0 0 192.168.109.33:22 192.168.109.1:59242 ESTABLISHED 18344/sshd: root@pt
tcp 0 0 192.168.109.33:22 192.168.109.1:55208 ESTABLISHED 18128/sshd: root@no
tcp6 0 0 :::22 :::* LISTEN 7159/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7394/master
查看网络统计信息 netstat -s
[root@localhost ~
]
Ip:
60270 total packets received
47563 forwarded
0 incoming packets discarded
12292 incoming packets delivered
59745 requests sent out
Icmp:
1 ICMP messages received
0 input ICMP message failed.
ICMP input histogram:
destination unreachable: 1
2 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 2
IcmpMsg:
InType3: 1
OutType3: 2
Tcp:
26 active connections openings
4 passive connection openings
0 failed connection attempts
0 connection resets received
2 connections established
12210 segments received
31695 segments send out
3 segments retransmited
0 bad segments received.
0 resets sent
Udp:
79 packets received
2 packets to unknown port received.
0 packet receive errors
81 packets sent
0 receive buffer errors
0 send buffer errors
查看进程
查看所有进程 ps -ef
Basic options:
-A, -e all processes
-a all with tty, except session leaders
Selection by list:
-u, U, --user
<UID
> effective user
id or name
Output formats:
-f full-format, including
command lines
PID: pid %CPU: 占用的 CPU 使用率 %MEM: 占用的记忆体使用率 VSZ: 占用的虚拟记忆体大小 RSS: 占用的记忆体大小 TTY: 终端的次要装置号码 (minor device number of tty) STAT: 该行程的状态: TIME: 执行的时间
[root@localhost ~
]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 9月04 ? 00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 9月04 ? 00:00:00
[kthreadd
]
root 3 2 0 9月04 ? 00:00:02
[ksoftirqd/0
]
root 5 2 0 9月04 ? 00:00:00
[kworker/0:0H
]
root 7 2 0 9月04 ? 00:00:00
[migration/0
]
root 8 2 0 9月04 ? 00:00:00
[rcu_bh
]
root 9 2 0 9月04 ? 00:00:04
[rcu_sched
]
实时显示进程状态 top
top - 00:12:14 up 7:35, 2 users, load average: 0.19, 0.19, 0.16
Tasks: 150 total, 1 running, 149 sleeping, 0 stopped, 0 zombie
%Cpu
(s
): 2.0 us, 1.0 sy, 0.0 ni, 92.3 id, 4.7 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem
: 1863224
`total`, 72620
`free`, 1066292 used, 724312 buff/cache
KiB Swap: 2097148 total, 1453308 free, 643840 used. 194168 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10569 200 20 0 3472052 717020 5808 S 2.0 38.5 7:03.37 java
18731 root 20 0 162016 2028 1316 S 0.7 0.1 0:27.74
top
3141 root 20 0 38956 4160 3988 S 0.3 0.2 2:15.14 systemd-journal
1 root 20 0 128024 4344 2460 S 0.0 0.2 0:03.27 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:02.21 ksoftirqd/0
...
702 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
查看系统安装程序
查看所有安装的软件包 rpm -qa
远程拷贝
[root@localhost ~
]
tomcat@11.8.127.198's password:
tomcat28080.tar.gz 100% 43MB 72.5MB/s 00:00
把本地文件:/home/tomcat/tomcat28080.tar.gz 拷贝到远程服务器:tomcat@11.8.127.198 登录用户是tomcat,需要输入tomcat用户的密码