最最常用的网络相关的Linux命令

134 阅读4分钟
  1. ps -ef | grep 进程名 根据进程名查找PID
[root@** idt-app]# ps -ef | grep java
root      2612     1  0 Feb09 ?        00:22:30 java -Xms7000m -Xmx7000m -jar idt-shelf-db-0.0.1-SNAPSHOT.jar
root     24674 24610  0 23:11 pts/3    00:00:00 grep --color=auto java

其中2612就是PID

  1. netstat -nap | grep PID 根据PID查看开启的端口
[root@** idt-app]# netstat -nap | grep 2612
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN      2612/java   
unix  2      [ ]         STREAM     CONNECTED     215368   2612/java            
unix  2      [ ]         STREAM     CONNECTED     166613   2612/java

其中18080就是端口

  1. netstat -nap |grep port 根据端口查找对应的进程
[root@** ~]# netstat -nap |grep 18080
tcp        0      0 0.0.0.0:18080           0.0.0.0:*               LISTEN      24680/java          
[root@** ~]# ps -ef |grep java
root     24680 24610  3 23:17 pts/3    00:00:06 java -jar idt-shelf-db-0.0.1-SNAPSHOT.jar
root     24738 24713  0 23:20 pts/4    00:00:00 grep --color=auto java
  1. ps -elf | grep PID 根据pid查看进程的详细信息
[root@** ~]# ps -elf|grep 24680
0 S root     24680 24610  1  80   0 - 1130740 futex_ 23:17 pts/3  00:00:06 java -jar idt-shelf-db-0.0.1-SNAPSHOT.jar
0 S root     24740 24713  0  80   0 - 28177 pipe_w 23:23 pts/4    00:00:00 grep --color=auto 24680
  1. ifconfig -a 用于查看ip等信息
[root@** ~]# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.21.111.111  netmask 255.255.240.0  broadcast 172.21.111.255
        ether 00:16:3e:12:5f:e2  txqueuelen 1000  (Ethernet)
        RX packets 8315054  bytes 1864693178 (1.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7667386  bytes 3912383720 (3.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  1. ping 用于查看网络故障,及测试一台主机到另外一台主机的网络是否连通
[root@** ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=52 time=8.44 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=52 time=8.58 ms
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 8.446/8.516/8.586/0.070 ms
  1. telnet IP PORT 本地计算机连接上远程主机

nslookup 检测网络中DNS服务器能否正确解析域名

windowns 自带的工具,linux下需要另外安装 yun install -y bind-uitls

[root@** ~]# nslookup baidu.com
Server:         100.100.2.136
Address:        100.100.2.136#53

Non-authoritative answer:
Name:   baidu.com
Address: 220.181.38.148
Name:   baidu.com
Address: 220.181.38.251

image.png

  1. traceroute 查看网络路由链路 链路从1开始,每行记录代表网络一跳(进过网关一次或者路由一次,后面的单位为毫秒,指的是路由一次需要多少时间。
[root@*** ~]# traceroute imagedt.com
traceroute to imagedt.com (47.107.15.88), 30 hops max, 60 byte packets
 1  * * *
 2  * * *
 3  11.0.50.25 (11.0.50.25)  1.638 ms 11.0.50.33 (11.0.50.33)  1.991 ms 11.0.50.41 (11.0.50.41)  1.734 ms
 4  * * *
 5  116.251.107.178 (116.251.107.178)  17.668 ms  17.676 ms *
 6  * * *
 7  * * *
  1. nmap 查看本机有哪些端口对外提供服务,或者哪些端口对外开放
[root@geoNode1 ~]# nmap -v -A 127.0.0.1

Starting Nmap 6.40 ( http://nmap.org ) at 2022-04-01 23:29 CST
NSE: Loaded 110 scripts for scanning.
NSE: Script Pre-scanning.
Initiating SYN Stealth Scan at 23:29
Scanning localhost (127.0.0.1) [1000 ports]
Discovered open port 22/tcp on 127.0.0.1
Completed SYN Stealth Scan at 23:29, 0.02s elapsed (1000 total ports)
Initiating Service scan at 23:29
Scanning 1 service on localhost (127.0.0.1)
Completed Service scan at 23:29, 0.01s elapsed (1 service on 1 host)
Initiating OS detection (try #1) against localhost (127.0.0.1)
NSE: Script scanning 127.0.0.1.
Initiating NSE at 23:29
Completed NSE at 23:29, 0.05s elapsed
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000013s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 7.4 (protocol 2.0)
| ssh-hostkey: 2048 5a:20:3e:64:b6:76:16:50:06:b3:5f:50:31:21:c3:8d (RSA)
|_256 83:ec:60:45:58:be:75:0f:96:61:98:2e:f7:5a:18:76 (ECDSA)
Device type: general purpose
Running: Linux 3.X
OS CPE: cpe:/o:linux:linux_kernel:3
OS details: Linux 3.7 - 3.9
Uptime guess: 1.811 days (since Thu Mar 31 04:01:25 2022)
Network Distance: 0 hops
TCP Sequence Prediction: Difficulty=262 (Good luck!)
IP ID Sequence Generation: All zeros

NSE: Script Post-scanning.
Initiating NSE at 23:29
Completed NSE at 23:29, 0.00s elapsed
Read data files from: /usr/bin/../share/nmap
OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 1.87 seconds
           Raw packets sent: 1022 (45.778KB) | Rcvd: 2043 (87.024KB)