ifconfig命令
ifconfig [interface]
# ifconfig -a
# ifconfig IFACE [up|down]
ifconfig interface [aftype] options | address …
# ifconfig IFACE IP/mask [up]
# ifconfig IFACE IP netmask MASK
注意:立即生效;
启用混杂模式:[-]promisc
route命令
路由管理命令
查看:route -n
添加:route add
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
目标:192.168.1.3 网关:172.16.0.1
~]# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0
目标:192.168.0.0 网关:172.16.0.1
~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
~]# route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
默认路由,网关:172.16.0.1
~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
~]# route add default gw 172.16.0.1
[root@localhost ~]# route add -host 0.0.0.0 gw 192.168.21.3 dev ens33 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.21.3 255.255.255.255 UGH 0 0 0 ens33 0.0.0.0 192.168.21.2 0.0.0.0 UG 100 0 0 ens33 192.168.21.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
删除:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
目标:192.168.1.3 网关:172.16.0.1
~]# route del -host 192.168.1.3
[root@localhost ~]# route del -host 0.0.0.0
目标:192.168.0.0 网关:172.16.0.1
~]# route del -net 192.168.0.0 netmask 255.255.255.0
[root@localhost ~]# route del -net 0.0.0.0 netmask 255.255.255.255
DNS服务器指定
/etc/resolv.conf
nameserver DNS_SERVER_IP1
nameserver DNS_SERVER_IP2
nameserver DNS_SERVER_IP3
[root@localhost ~]# vim /etc/resolv.conf
正解:FQDN–>IP
# dig -t A FQDN
# host -t A FQDN
[root@localhost ~]# dig -t A www.baidu.com
反解:IP–>FQDN
# dig -x IP
# host -t PTR IP
[root@localhost ~]# dig -x 119.75.217.109
FQDN: www.magedu.com.
netstat命令:
netstat – Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
显示网络
netstat [–tcp|-t] [–udp|-u] [–raw|-w] [–listening|-l]连接: [–all|-a] [–numeric|-n] [–extend|-e[–extend|-e]] [–program|-p]
-t: tcp协议相关
-u: udp协议相关
-w: raw socket相关
-l: 处于监听状态
-a: 所有状态
-n: 以数字显示IP和端口;
-e:扩展格式
-p: 显示相关进程及PID
常用组合:
-tan, -uan, -tnl, -unl
例一:显示UDP协议相关的网络信息
[root@localhost ~]# netstat -uan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:58076 0.0.0.0:* udp 0 0 0.0.0.0:5353 0.0.0.0:* udp 0 0 127.0.0.1:323 0.0.0.0:* udp 0 0 192.168.21.137:40986 106.185.31.36:123 ESTABLISHED udp 0 0 192.168.122.1:53 0.0.0.0:* udp 0 0 192.168.21.137:33856 173.255.246.13:123 ESTABLISHED udp 0 0 0.0.0.0:67 0.0.0.0:* udp 0 0 0.0.0.0:68 0.0.0.0:* udp 0 0 0.0.0.0:35967 0.0.0.0:* udp6 0 0 ::1:323 :::* udp6 0 0 :::7026 :::*
显示路由表:
netstat {–route|-r} [–numeric|-n]网络状态信息
-r: 显示内核路由表
-n: 数字格式
显示接口统计数据:
netstat {–interfaces|-I|-i} [iface] [–all|-a] [–extend|-e] [–program|-p] [–numeric|-n]
# netstat -i
# netstat -I IFACE统计数据
nux网络配置(2)
例一:列出所有监听 tcp 端口
[root@localhost ~]# netstat -lt
配置Linux网络属性:ip命令
ip命令:
ip – show / manipulate routing, devices, policy routing and tunnels
例一:表示网络已启用的一些接口信息
[root@localhost ~]# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:be:f9:0b brd ff:ff:ff:ff:ff:ff 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000 link/ether 52:54:00:2b:50:e8 brd ff:ff:ff:ff:ff:ff 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000 link/ether 52:54:00:2b:50:e8 brd ff:ff:ff:ff:ff:ff
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route }
link OBJECT:
ip link – network device configuration
set
dev IFACE
可设置属性:
up and down:激活或禁用指定接口;
例一:禁用指定端口
[root@localhost ~]# ip link set dev ens33 down
show
[dev IFACE]:指定接口
[up]:仅显示处于激活状态的接口
例一:显示指定指定接口的信息
[root@localhost ~]# ip link show dev ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:be:f9:0b brd ff:ff:ff:ff:ff:ff
ip address – protocol address management
ip addr { add | del } IFADDR dev STRING
例一:添加IP地址
[root@localhost ~]# ip addr add 192.168.21.23/24 dev ens33
[label LABEL]:添加地址时指明网卡别名
例二:添加别名
[root@localhost ~]# ip addr add 192.168.21.23/24 dev ens33 label 'ens33:12'
[scope {global|link|host}]:指明作用域
global: 全局可用;
link: 仅链接可用;
host: 本机可用;
[broadcast ADDRESS]:指明广播地址
ip address show – look at protocol addresses查看某一类地址
[dev DEVICE]
[label PATTERN]
[primary and secondary]
ip address flush – flush protocol addresses清除一类型地址
使用格式同show
ip route – routing table management
ip route add
添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP
例一:添加路由
[root@localhost ~]# ip route add 192.168.1.3 via 0.0.0.0 dev ens33
TARGET:
主机路由:IP
网络路由:NETWORK/MASK
添加网关:ip route add defalt via GW dev IFACE
ip route delete
删除路由:ip route del TARGET
例一:删除路由
[root@localhost ~]# ip route delete 0.0.0.0
例一:删除路由
[root@localhost ~]# ip route del default
ip route show
ip route flush
[dev IFACE]
[via PREFIX]
ss命令:
格式:ss [OPTION]… [FILTER]
选项:
-t: tcp协议相关
-u: udp协议相关
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息
例一:显示所有端口信息
[root@localhost ~]# ss -a
FILTER := [ state TCP-STATE ] [ EXPRESSION ]
TCP的常见状态:
tcp finite state machine:
LISTEN: 监听
ESTABLISHED:已建立的连接
FIN_WAIT_1
FIN_WAIT_2
SYN_SENT
SYN_RECV
CLOSED
EXPRESSION:
dport =
sport =
示例:’( dport = :ssh or sport = :ssh )’
常用组合:
-tan, -tanl, -tanlp, -uan
Linux网络属性配置(3): 修改配置文件
IP、MASK、GW、DNS相关配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE
例一:配置文件
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
路由相关的配置文件:/etc/sysconfig/network-scripts/route-IFACE
/etc/sysconfig/network-scripts/ifcfg-IFACE:
DEVICE:此配置文件应用到的设备;
HWADDR:对应的设备的MAC地址;
BOOTPROTO:激活此设备时使用的地址配置协议,常用的dhcp, static, none, bootp;
NM_CONTROLLED:NM是NetworkManager的简写;此网卡是否接受NM控制;CentOS6建议为“no”;
ONBOOT:在系统引导时是否激活此设备;
TYPE:接口类型;常见有的Ethernet, Bridge;
UUID:设备的惟一标识;
IPADDR:指明IP地址;
NETMASK:子网掩码;
GATEWAY: 默认网关;
DNS1:第一个DNS服务器指向;
DNS2:第二个DNS服务器指向;
例一:配置IP地址
BOOTPROTO=static IPADDR=172.16.100.12 NERMASK=255.255.0.0 GATEWAY=172.16.0.1 DNS1=172.16.0.1
USERCTL:普通用户是否可控制此设备;
PEERDNS:如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中;
/etc/sysconfig/network-scripts/route-IFACE
两种风格:
(1) TARGET via GW
例一:给配置文件指定路由地址
1 0.0.0.0/24 via 192.168.21.3
(2) 每三行定义一条路由
ADDRESS#=TARGET
NETMASK#=mask
GATEWAY#=GW
例二:给配置文件指定路由地址
1 ADDRESS0=0.0.0.0 2 NETMASK0=0.0.0.0 3 GATEWAY0=192.168.21.3
给网卡配置多地址:
ifconfig:
ifconfig IFACE_ALIAS
ip
ip addr add
配置文件:
ifcfg-IFACE_ALIAS
DEVICE=IFACE_ALIAS
例一:编辑网卡别名
[root@localhost network-scripts]# vim ifcfg-ens33:12
注意:网关别名不能使用dhcp协议引导;
Linux网络属性配置的tui(text user interface):
system-config-network-tui
也可以使用setup找到;
注意:记得重启网络服务方能生效;
配置当前主机的主机名:
hostname [HOSTNAME]
例一:配置名称
[root@localhost ~]# hostname www
/etc/sysconfig/network
HOSTNAME=
网络接口识别并命名相关的udev配置文件:
/etc/udev/rules.d/70-persistent-net.rules
卸载网卡驱动:
modprobe -r e1000
装载网卡驱动:
modprobe e1000
原创文章,作者:kang,如若转载,请注明出处:http://www.178linux.com/76956