In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of network configuration commands in CentOS, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
When we configure the network of CentOS, we usually start with IP address (IPADDR), subnet mask (NETMASK), gateway (Gateway), hostname (HOSTNAME), DNS server and so on. In CentOS, there are different commands or configuration files to complete these configuration operations. Next, we will briefly analyze the methods of network configuration from three aspects: ifcfg commands, iproute2 commands and configuration files.
1. Ifcfg commands
The ifcfg commands include ifconfig,route,netstat and hostname.
1. Ifconfig command
Used to configure a network interface. It is used in the following format:
A): ifconfig [- a] [interface]
-a: indicates the interface that displays all network interface information, including inactive status
~] # ifconfig-a
B): ifconfig IFACE (network interface name, same below) IPADDR netmask NETMASK [up | down]
It means that the ip address and netmask of the interface can be modified directly through the ifconfig command, and the interface can be started and stopped.
This command immediately sends the configuration parameters to the TCP/IP protocol stack in the kernel, so it takes effect immediately, but it has no effect when the service is restarted.
~] # ifconfig eth2 172.16.7.14 netmask 255.255.0.0 ~] # ifconfig eth2 172.16.7.14 + 16 down
2. Route command
Displays and configures the ip routing table. It is used in the following format:
A): route [- n]
Means to view all routing tables and display ip addresses in numbers
~] # route-n
B): route add [- net |-host] target [netmask Nm] [gw Gw] [[dev] If]
Add routes for the appropriate interfac
~] # route add-host 172.16.7.14 gw 172.16.0.1 dev eth2 # Host Route ~] # route add-net 172.16.7.0 Universe 24 gw 172.16.0.1 dev eth2 # Network Segment Route ~] # route add default gw 172.16.0.1 dev eth2 # default Route
C): route del [- net |-host] target [netmask Nm] [[dev] If]
Delete a rout
~] # route del-host 172.16.7.14 dev eth2 ~] # route del-net 172.16.7.0 take 24 dev eth2 ~] # route del default dev eth2
3. Netstat command
Displays network connections, routing tables, interface status, etc. It is used in the following format:
Netstat [- n] [- r] [- p] [- e] [- a] [--tcp |-t] [--udp |-u] [--raw |-w] [--listening |-l] [- I] [- I]
-n: displays ip and port number in numeric format without address translation
-r: displays the kernel routing table
-p: displays related processes and PID
-e: show extended format
-a: displays all connection status
-t: displays the status of related connections to the tcp protocol
-u: displays the status of related connections for the udp protocol
-w: displays the status of raw socket-related connections
-l: displays connections in the listening state
-I: displays the status of all interfaces
-I: displays the status of specific interfaces
The above options can be combined, commonly used combinations are-tan,-uan,-tln,-uln,-tunlp, etc.
~] # netstat-tan ~] # netstat-Ieth2
4. Hostname command
Displays the configuration hostname. It is used in the following format:
Hostname: displays the current hostname
Hostname HOSTNAME: modify the current hostname to HOSTNAME, which is valid only. Restart is invalid.
~] # hostname ~] # hostname TEST
5. Hostnamectl command (added to CentOS7)
Displays the configuration hostname. It is used in the following format:
Hostnamectl [status]: displays the current hostname information
Hostnamectl set-hostname: sets the hostname, which is permanent
~] # hostnamectl set-hostname CentOS7
II. Iproute2 commands
Iproute2 commands include ip link,ip addr,ip route,ip netns, ss and so on.
1. Ip link command
Configure network interface properties. It is used in the following format:
A): ip link set [dev] IFACE [up | down] [multicast on | off] [name IFACE] [mtu NUMBER]
Up and down: enables or disables the selected network interface
Multicast on or multicast off: enable or disable Multicast
Name NAME: rename the interface (you need to down the interface before naming)
Mtu NUMBER: sets the mtu size of the API. Default is 1500.
~] # ip link set eth2 name eth3 up ~] # ip link set eth3 multicast off mtu 1000
B): ip link [show | list]
Displays the properties of all network interfaces.
~] # ip link show
2. Ip addr command
Configure the ip address of the network interface. It is used in the following format:
A): ip addr add IPADDR dev IFACE [label NAME] [broadcast ADDRESS] [scope SCOPE_VALUE]
Add an ip address.
[label NAME]: indicates the interface alias for the additional address
[broadcast ADDRESS]: it will be calculated automatically based on ip and netmask, no need to fill in
[scope SCOPE_VALUE]: gloal is available globally; link API is available; host: native is available
~] # ip addr add 172.16.7.14 + 24 dev eth2 label eth2:0
B): ip addr delete IPADDR dev IFACE
Delete an ip address.
~] # ip addr del 172.16.7.14 + 24 dev eth2:0
C): ip addr show [dev IFACE | label IFACE:#]
Only the specified interface address is displayed.
~] # ip addr show label eth2:0 ~] # ip addr show eth2
D): ip addr flush [dev] {IFACE | label IFACE:#}
Clears all addresses or aliases specified.
~] # ip addr flush label eth2:0 ~] # ip addr flush eth2
3. Ip route command
Configure the routing table information for the interface. It is used in the following format:
A): ip route add TYPE PREFIX (target network) via GateWay [dev IFACE] [src SOURCE_IP]
Add a routing table.
~] # ip route add 172.16.7.14 via 172.16.0.1 dev eth2 ~] # ip route add 172.16.7.0 dev eth2 24 via 172.16.0.1 dev eth2 ~] # ip route add d efault via 172.16.0.1 dev eth2
B): ip route del TYPE PREFIX
Delete the routing table.
~] # ip route del 172.16.7.14 ~] # ip route del 172.16.7.0Universe 24
C): ip route show
Displays the routing table.
~] # ip route show
D): ip route flush dev IFACE
Clears all routing tables for the specified interface.
~] # ip route flush dev eth2
4. Ss command
The same as the netstat command to view the status of the network, the option parameters are basically the same as the netstat command, but the query speed is faster than the netstat command. It is used in the following format:
Ss [options] [filter]
Filter: filter FILTER: = [state TCP-STATE] [EXPRESSION]
Options:
-related connections of t:TCP protocol
-u:UDPF related connections
-w:raw socket related connections
-l: a connection in the listening state
-a: all statu
-n: display IP and Port in digital format
-e: extended format
-p: displays related processes and PID
-m: memory usage
-o: timer information
EXPRESSION:
Dport: target port
Sport: source port
'(dport =: 22 or sport =: 22)'
~] # ss-tan # View the connection status of the current network so tcp
III. Network configuration file
On the CentOS system, we can configure various properties of the network interface by modifying different network configuration files, but to make it permanent, we need to make the kernel reread the modified configuration file.
1. Configuration files for attributes such as IP/NETMASK/GW/DNS:
Its path is: / etc/sysconfig/network-scripts/ifcfg-IFACE
Its format is:
DEVICE: the name of the device corresponding to this profile
ONBOOT: whether to activate this interface during system boot
NETBOOT: whether network boot is supported or not
UUID: unique identification of the Devic
IPv6INT: whether to initialize IPv6
BOOTPROTO: what protocol is used to configure properties when activating this interface? dhcp, bootp, static, none are commonly used.
TYPE: interface type, such as ethernet and bridge
DNS1: the first DNS server points to
DNS2: the alternate DNS server points to
DOMAIN:DNS search domain
GATEWAY: default gateway
IPADDR: native ip address
NETMASK: subnet mask; CentOS7 supports the use of PREFIX to indicate the subnet mask by length
USERCTL: whether to allow ordinary users to control this device
PEERDNS: if the value of BOOTPROTO is "dhcp", whether to run the dns server assigned by dhcp server to override the manually specified DNS server locally; default is allowed
HWADDR: the MAC address of the device
NM_CONTROLLED: whether to use NetworkManager services to control the interface
Network-scripts] # cat ifcfg-eth2 DEVICE= "eth2" ONBOOT=yes NETBOOT=yes IPADDR=172.16.7.14 DNS=172.16.0.1 GATEWAY=172.16.0.1 NETMASK=255.255.0.0 IPV6INIT=no BOOTPROTO=none TYPE=Ethernet
2. Configuration files related to routing:
Its path is: / etc/sysconfig/network-scripts/route-IFACE
Its format is: (not to be mixed)
A): one route entry per line:
TARGET via GW
B): one route entry for every three lines:
ADDRESS#=TARGET
NETMASK#=MASK
GATEWAY#=NEXTHOP
Network-scripts] # cat route-eth2 172.16.7.0/24 via 172.16.0.1
3. Configuration files related to hostname:
Its path is: / etc/sysconfig/network
Its format is:
HOSTNAME=
Sysconfig] # cat network # Created by anaconda HOSTNAME=centos7.1
4. The DNS server points to the configuration file:
Its path is: / etc/resolv.conf
Its format is:
Nameserver DNS_SERVER_IP
Etc] # cat resolv.conf
# Generated by NetworkManager
Search magelinux.com
Nameserver 172.16.0.1
IV. Conclusion
On the way of learning Linux, the network is a very important link, and its commands are constantly improving. Many commands have the same function, so we can choose to memorize relatively simple and efficient commands and parameters first. In addition to the above commands, there are also commands in CentOS7 that directly display the graphical interface, such as nmtui.
The above is all the contents of the article "sample Analysis of Network configuration commands in CentOS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.