Linux commands: Common commands for network configuration

Source: Internet
Author: User
Tags nameserver

Network Configuration Common commands Introduction:

The function of the network kernel in Linux

1. Execute command generated address:(effective immediately after execution, service restart after the failure)

RHEL5:/etc/modprobe.conf
RHEL6:/etc/udev/rules.d/70-persistent-net.rules
Alias

Ifconfig [EthX]-A: Shows the configuration of all interfaces
Ifconfig EthX Ip/mask [Up|down] immediately after the configuration, network service or host failure after restart


Network services:
RHEL5:/etc/init.d/network {start|stop|restart|status}
RHEL6:/etc/init.d/networkmanager {start|stop|restart|status} not commonly used

Eg1. Ifconfig eth1

EG2. Ifconfig eth1 172.16.1.88/24 up


2. execute command to generate Gateway:(Effective immediately after execution, after service restart)
Route
Add: Adding
-host: Host Routing
-net: Network Routing
-net 0.0.0.0
Route Add-net | -host DEST GW Nexthop
Route Add default GW Nexthop

Eg1. Route Add-net | -host DEST GW Nexthop

EG2. Route add-net 10.0.0.0/8 GW 192.168.10.1


Del: Delete
-host: Host Routing

-net: Network Routing


Route Del-net 10.0.0.0/8
Route Del-net 0.0.0.0
Route del Default
Changes made to restart the Network service or host after the failure;

route-n: Digitally display information about each host or port

3. After modifying the configuration file , restart the service generated address:(after the configuration file does not take effect, after the service restarts)
3.1. File Address:/etc/sysconfig/network

Network interface Profile: does not take effect immediately, but restarting the network service or host will take effect


3.2./etc/sysconfig/network-scripts/ifcfg-interface_name

Device=: The associated device name , to be consistent with the second half of the file name "Interface_name";
BOOTPROTO={STATIC|NONE|DHCP|BOOTP}: Boot protocol;
To use static addresses , use static or none;DHCP indicates the use of a DHCP server to obtain an address;

Ipaddr=: IP Address
netmask=: Subnet Mask
Gateway=: Set default gateway;
Onboot=: Whether the network interface is activated automatically when booting;
Hwaddr=: Hardware address, to be consistent with the address in the hardware;
Userctl={yes|no}: Whether to allow ordinary users to control this interface ;
Peerdns={yes|no}: Whether the DNS address specified by the DHCP server is accepted when Bootproto is DHCP;


[Email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
Device=eth0
Bootproto=none
Hwaddr=00:50:56:ba:6a:fa
Onboot=yes
netmask=255.255.252.0
ipaddr=110.109.131.15
gateway=110.109.128.1
Type=ethernet

[Email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
Device=eth1
Onboot=yes
Bootproto=dhcp
hwaddr=00:50:56:ba:6f:9e


4. After you have configured the routing file , restart the service
/etc/sysconfig/network-scripts/route-ethx
Add format one: DEST via Nexthop

Add format two:
address0=
netmask0=
gateway0=


eg1.192.168.10.0 via 192.168.10.1

EG2. address0=192.168.10.0

netmask0=255.255.255.0
gateway0=192.168.10.1


The DNS server specifies only one method:/etc/resolv.conf
NameServer dns_ip_1
NameServer dns_ip_2

Specify local resolution:/etc/hosts DNS-->/ETC/HOSTS-->DNS Cache/Host

Host IP host name host Alias
172.16.0.1 www.magedu.com www


5. Configure host Name:
hostname hostname effective immediately, but not permanently;

Vi/etc/sysconfig/network Permanent Effect

Eg1. [Email protected] ~]# cat/etc/sysconfig/network
Networking=yes
Networking_ipv6=no
Hostname=localhost.localdomain


RHEL5:
Setup:system-config-network-tui
System-config-network-gui

6. Address Management (ifconfig, old)

Ip
Link: Network Interface properties
Addr: Protocol Address
Route: Routing

Link
Show Ip-s Link Show
Set IP link set DEV {up|down}

Addr
Add IP addr add ADDRESS dev Dev
Del ip addr del address dev Dev
show ip addr Show Dev dev to PREFIX
Flush IP addr Flush Dev dev to PREFIX


A NIC can use multiple addresses, network devices can alias: eth0 ethx:x, eth0:0, Eth0:1, ...
Configuration method: Non-primary address cannot use DHCP dynamic acquisition (global secondary IP)

Ifconfig ethx:x Ip/netmask
/etc/sysconfig/network-scripts/ifcfg-ethx:x
Device=ethx:x


7. usage examples :

Eg1.ip addr Add 10.3.3.3 dev eth1

Eg2.ip addr Add 10.2.2.2 dev eth1 label eth1:1

Eg3.ip addr del 10.2.2.2 Dev eth1

Eg4.ip addr Show eth1 to 172/8


Eg5. Remove all 10.* addresses : IP addr flush eth1 to 10/8

[Email protected] ~]#IP Addr Show
3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast Qlen 1000
Link/ether 00:50:56:9c:05:59 BRD FF:FF:FF:FF:FF:FF
inet 172.16.1.80/24 BRD 172.16.1.255 Scope Global eth1
inet 10.2.2.2/32 Scope Global eth1
inet 10.3.3.3/32 Scope Global Eth1:1
inet 192.168.11.2/24 Scope Global Eth1:2
inet 172.16.1.81/24 BRD 172.16.1.255 scope Global Secondary eth1:0
Inet6 FE80::250:56FF:FE9C:559/64 Scope link
Valid_lft Forever Preferred_lft Forever
[Email protected] ~]#IP Addr FlushEth1 to 10/8
[[Email protected] ~]# IP addr Show
3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast Qlen 1000
Link/ether 00:50:56:9c:05:59 BRD FF:FF:FF:FF:FF:FF
inet 172.16.1.80/24 BRD 172.16.1.255 Scope Global eth1
inet 192.168.11.2/24 Scope Global Eth1:2
inet 172.16.1.81/24 BRD 172.16.1.255 scope Global Secondary eth1:0
Inet6 FE80::250:56FF:FE9C:559/64 Scope link
Valid_lft Forever Preferred_lft Forever


Eg6. Remove All 10.* routes : IP route flush to 10/8

[[Email protected] ~]# IP routeAdd to 10.0.0.0/8Dev eth0 via 110.109.128.1
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.0 110.109.128.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 110.109.128.1 0.0.0.0 UG 0 0 0 eth0
[[Email protected] ~]# IP routeFlush to 10/8
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
0.0.0.0 110.109.128.1 0.0.0.0 UG 0 0 0 eth0



Attach a. Linux network configuration frequently used commands

1) #ifconfig//View the configured NIC/sbin/ifconfig
2) # vi/etc/resolv.conf//Modify the DNS resolv.conf keywords are mainly four, respectively:
NameServer #定义DNS服务器的IP地址
Domain #定义本地域名
Search #定义域名的搜索列表
Sortlist #对返回的域名进行排序
The most important is the nameserver keyword, if you do not specify nameserver can not find the DNS server, other keywords are optional.
3) #vi/etc/sysconfig/network//Modify Gateway
Networking=yes (Indicates whether the system uses a network and is generally set to Yes.)

If no, the network cannot be used, and many system service programs will fail to start.
Hostname=centos (Set the hostname of this machine, the host name set here should correspond to the hostname set in/etc/hosts)
gateway=192.168.1.1 (Sets the IP address of the gateway for the native connection. For example, the gateway is 10.0.0.2)
4) #vi/etc/sysconfig/network-scripts/ifcfg-eth0//Modify NIC Configuration
Device=eth0 #描述网卡对应的设备别名, for example, in Ifcfg-eth0 file it is eth0
Bootproto=static #设置网卡获得ip地址的方式, the possible options are static,dhcp or BOOTP,

IP addresses that are obtained by the DHCP protocol, respectively, for statically specified IP addresses, via the BOOTP protocol
broadcast=192.168.0.255 #对应的子网广播地址
HWADDR=00:07:E9:05:E8:B4 #对应的网卡物理地址
ipaddr=12.168.1.2 #如果设置网卡获得 IP address is statically specified, this field specifies the IP address of the NIC
Ipv6init=no
Ipv6_autoconf=no
netmask=255.255.255.0 #网卡对应的网络掩码
network=192.168.1.0 #网卡对应的网络地址
Onboot=yes #系统启动时是否设置此网络接口, yes when the device is activated when the system starts.

If you have only one network card and set to No, you will not be able to surf the Internet.


attached two. Linux Virtual Machine network configuration (application)

1) using NAT (Simple and practical)
1. Locate the virtual Machine Device "network adapter. Double-click Open Network adapter to customize the specified virtual network selection VMnet8 (Nat).
2. Start the Linux virtual machine and go to the Linux command line. If you enter the Xwindow interface, switch to the command line interface CTRL+ALT+SHIFT+F1
3. #ifconfig See if you use the NIC, Vi/etc/sysconfig/network-scripts/ifcfg-eth0 modify the NIC configuration. Set the BOOTPROTO=DHCP.

Remove ipaddr, we use the automatic acquisition of IP and DNS. (If there is no clear can refer to above (I. 4))
4. #service Network restart//restart network. This way the Linux virtual machine will be able to surf the Internet. Let's test it.
#ping www.baidu.com//command line input, you can ping the line.
2) using bridging (slightly)
3) Adopt host-only (slightly)


attached three. Standalone Linux host Internet configuration (usually internet via router)

1) Obtain IP and DNS automatically:
#vi/etc/sysconfig/network//Modify the gateway to remove the gateways, as we are using automatic access
#vi/etc/sysconfig/network-scripts/ifcfg-eth0//Modify NIC Configuration
Modify BOOTPROTO=DHCP, delete ipaddr. (If there is no clear can refer to above (I. 4))
#service Network restart//restart network. Pass the ping test.

2) Use assigned IP network configuration
#vi/etc/resolv.com//Modify DNS Primary settings keyword nameserver=8.8.8.8 This is easier to ignore.
#vi/etc/sysconfig/network//Modify the gateway, add gateway=192.168.1.0 to see which segment you belong to.
#vi/etc/sysconfig/network-scripts/ifcfg-eth0//Modify NIC Configuration
Modify Bootproto=static, add (or set) ipaddr=192.168.1.4. (Assigning IP addresses)
#service Network restart//restart network. Pass the ping test.


---end---

Linux commands: Common commands for network configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.