网络管理常用命令
ifconfig
查看当前处于活动状态的所有网络接口
[root@localhost yum.repos.d]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 21537 bytes 25164903 (23.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10645 bytes 1026509 (1002.4 KiB)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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
仅查看eth0网卡状态
[root@localhost network-scripts]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 5531 bytes 511537 (499.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7288 bytes 916789 (895.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看所有网卡状态信息, 包括禁用和启用
[root@localhost ~]# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 6962 bytes 641700 (626.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9261 bytes 1147273 (1.0 MiB)
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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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
ip
link | 网络接口属性 |
---|---|
addr | 协议地址 |
route | 路由 |
查看网络接口所有地址
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
显示报文统计信息
[root@localhost ~]# ip -s link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1029496 11223 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1836685 15127 0 0 0 0
启用或禁用网络接口ip link set DEV {up|down}
[root@localhost ~]# ip link set lo down
[root@localhost ~]# ip link set lo up
ip addr add ADDRESS dev DEV:添加IP地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1255sec preferred_lft 1255sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.1.1/24 dev eth0
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1077sec preferred_lft 1077sec
inet 192.168.1.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ip addr del ADDRESS dev DEV:删除IP地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic eth0
valid_lft 1697sec preferred_lft 1697sec
inet 192.168.1.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr del 192.168.1.1/24 dev eth0
[root@localhost ~]# ip addr show ens33
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic eth0
valid_lft 1658sec preferred_lft 1658sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ip addr show DEV:查看网络接口的地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1462sec preferred_lft 1462sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ss
ss是一种网络状态查看工具,取代netstat
-t | tcp协议相关 |
---|---|
-u | udp协议相关 |
-w | 裸套接字相关 |
-x | unix套接字相关 |
-l | listen状态的连接 |
-a | 所有 |
-n | 数字格式 |
-p | 相关的程序及pid |
-e | 扩展的信息 |
-m | 内存用量 |
-o | 显示计时器信息 |
常用组合:
[root@localhost ~]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1078,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1365,fd=13))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=1078,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=1365,fd=14))
[root@localhost ~]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 36 192.168.72.128:22 192.168.72.1:54438
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# ss -anu
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 127.0.0.1:323 *:*
UNCONN 0 0 *:68 *:*
UNCONN 0 0 ::1:323 :::*
常见端口:
http | 80/tcp |
---|---|
https | 443/tcp |
ssh | 22/tcp |
ftp | 20,21/tcp |
mysql | 3306/tcp |
rsync | 873/rsync |
redis | 6379/tcp |