Linux网络配置基本用法

  • 查看网卡信息

Linux下网卡命名规律:eth0,eth1。第一块以太网卡,第二块。lo为环回接口,它的IP地址固定为127.0.0.1,掩码8位。它代表你的机器本身。


[root@adx ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:22:19:D4:CE:3B  
          inet addr:10.1.172.10  Bcast:10.1.172.255  Mask:255.255.255.0
          inet6 addr: fe80::222:19ff:fed4:ce3b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:110027562 errors:0 dropped:12 overruns:0 frame:0
          TX packets:84382846 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:16325696720 (15.2 GiB)  TX bytes:19737558214 (18.3 GiB)
          Interrupt:169 Memory:dfdf0000-dfe00000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:61461561 errors:0 dropped:0 overruns:0 frame:0
          TX packets:61461561 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5468511079 (5.0 GiB)  TX bytes:5468511079 (5.0 GiB)


配置网络,单网卡单ip,编辑/etc/sysconfig/network-scripts/ifcfg-eth[x]文件([x]为使用的网卡)

[root@adx ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Broadcom Corporation NetXtreme BCM5721 Gigabit Ethernet PCI Express    //网卡名称,不用改
DEVICE=eth0
BOOTPROTO=none
BROADCAST=10.1.172.255                                                  // 广播地址
HWADDR=00:22:19:d4:ce:3b                                                //网卡mac地址,不用改
IPADDR=10.1.172.10                                                      // ip地址
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0                                                   // 子网掩码
NETWORK=10.1.172.0                                                     // 网络地址
ONBOOT=yes                                                             // 开机自启动
GATEWAY=10.1.172.254                                                   // 网关地址
TYPE=Ethernet

  • 配置eth0的IP地址, 同时激活该设备
    [root@adx ~]# ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
           注:手工配置linux网络只限于临时使用,配置不保存。

  • 显示eth0的IP地址
    [root@localhost workspace]# ifconfig eth0 | grep "inet addr" | awk -F[:" "]+ '{print $4}'
    192.168.1.102
    awk语句会以空格和":"为分隔符,然后打印出第4列。

  • 摘录:
    例如:
    
    (1) 添加到主机的路由。
    
    #route add –host 192.168.1.2 dev eth0:0
    
    #route add –host 10.20.30.148 gw 10.20.30.40
    
    (2) 添加到网络的路由。
    
    #route add –net 10.20.30.40 netmask 255.255.255.248 eth0
    
    #route add –net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
    
    #route add –net 192.168.1.0/24 eth1
    
    (3) 添加默认网关。
    
    #route add default gw 192.168.1.1
    
    (4) 查看内核路由表的配置。
    
    #route
    
    (5)删除路由。
    
    #route del –host 192.168.1.2 dev eth0:0
    
    #route del –host 10.20.30.148 gw 10.20.30.40
    
    #route del –net 10.20.30.40 netmask 255.255.255.248 eth0
    
    #route del –net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
    
    #route del –net 192.168.1.0/24 eth1
    
    #route del default gw 192.168.1.1
    
    对于1和2两点可使用下面的语句实现:
    
    Ifconfig eth0 172.16.19.71 netmask 255.255.255.0
    
    Route 0.0.0.0 gw 172.16.19.254
    
    Service network restart

  • 添加默认网关
    [root@adx ~]# route add default gw 192.168.1.1
  • 删除默认网关路由
    [root@adx ~]# route del default gw 192.168.1.1
  • 配置DNS
    [root@adx ~]# vim /etc/resolv.conf 
    
      1 #nameserver 222.246.129.80
      2 #nameserver 8.8.8.8
      3 #search localdomain
      4 #nameserver 222.246.129.80
      5 #nameserver 59.51.78.210
      6 nameserver 10.1.201.110
      7 nameserver 114.114.114.114
  • 激活设备
    [root@adx ~]# ifconfig eth0 up
  • 禁用设备
    [root@adx ~]# ifconfig eth0 down
    注:单个网络接口的配置文件进行修改后,不必每次都从新启动network服务使配置文件生效,可用以上命令。

  • 主机名称配置命令(当前生效)
    [root@adx ~]# hostname 123

  • 修改机器hostname
    [root@localhost workspace]# vim /etc/sysconfig/network
    
    NETWORKING=yes
    HOSTNAME=localhost.wangmeng

  •  traceroute命令显示当前主机与目的主机之间经过的所,有网络节点的地址,以及当前主机到每个中间结点的连接状态。
    [root@adx ~]# traceroute 172.31.23.148
    traceroute to 172.31.23.148 (172.31.23.148), 30 hops max, 40 byte packets
     1  10.1.172.254 (10.1.172.254)  4.273 ms  4.771 ms  5.459 ms
     2  172.31.0.133 (172.31.0.133)  0.444 ms  0.710 ms  0.421 ms
     3  172.31.0.129 (172.31.0.129)  9.919 ms  10.354 ms  10.784 ms
     4  172.31.23.148 (172.31.23.148)  0.680 ms * *

  • 查看当前主机名
    [root@adx ~]# hostname
    adx

  • ping命令测试与其它主机的网络链接
    [root@adx ~]# ping 172.31.23.148
    PING 172.31.23.148 (172.31.23.148) 56(84) bytes of data.
    64 bytes from 172.31.23.148: icmp_seq=1 ttl=61 time=0.829 ms
    64 bytes from 172.31.23.148: icmp_seq=2 ttl=61 time=0.696 ms
    64 bytes from 172.31.23.148: icmp_seq=3 ttl=61 time=0.619 ms
    
    --- 172.31.23.148 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2001ms
    rtt min/avg/max/mdev = 0.619/0.714/0.829/0.092 ms

  • route命令查看主机路由信息
    [root@adx ~]# route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    10.1.172.0      *               255.255.255.0   U     0      0        0 eth0
    169.254.0.0     *               255.255.0.0     U     0      0        0 eth0
    default         10.1.172.254    0.0.0.0         UG    0      0        0 eth0

  • 网络服务启动脚本
    /etc/init.d/network 从新启动才能生效
    
    [root@adx ~]# /etc/rc.d/init.d/network start      // 启动network
    [root@adx ~]# /etc/rc.d/init.d/network stop       // 停止network
    [root@adx ~]# /etc/rc.d/init.d/network restart    // 重启network

  • 主机名称配置文件
    [root@adx ~]# cat /etc/sysconfig/network
    NETWORKING=yes
    NETWORKING_IPV6=yes
    HOSTNAME=localhost.localdomain

  • 本地主机名称解析文件
    [root@adx ~]# cat /etc/hosts
    127.0.0.1		localhost.localdomain localhos

  • 端口号和服务器名之间的映射
    [root@adx ~]# more /etc/services

  • arp命令来配置并查看arp缓存
    [root@adx ~]# arp
    Address                  HWtype  HWaddress           Flags Mask            Iface
    10.1.172.25              ether   00:50:56:94:32:FF   C                     eth0
    10.1.172.254             ether   70:54:F5:8F:A4:98   C                     eth0

  • nslookup命令查询一台机器的ip地址和与其对应的域名
    [root@localhost workspace]# nslookup
    > mail.163.com
    Server:		58.20.126.98
    Address:	58.20.126.98#53
    
    Non-authoritative answer:
    mail.163.com	canonical name = mail163.yxgslb.netease.com.
    Name:	mail163.yxgslb.netease.com
    Address: 123.125.50.28
    Name:	mail163.yxgslb.netease.com
    Address: 123.125.50.7
    Name:	mail163.yxgslb.netease.com
    Address: 123.125.50.26
    > exit
    
    [root@localhost workspace]# 

  • dig命令查看
    [root@localhost workspace]# dig sina.com.cn
    
    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> sina.com.cn
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51600
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    
    ;; QUESTION SECTION:
    ;sina.com.cn.			IN	A
    
    ;; ANSWER SECTION:
    sina.com.cn.		43	IN	A	202.108.33.60
    
    ;; Query time: 2 msec
    ;; SERVER: 58.20.126.98#53(58.20.126.98)
    ;; WHEN: Sat Apr 25 07:08:17 2015
    ;; MSG SIZE  rcvd: 45
    
    [root@localhost workspace]# 

  • lsof -i查看特定端口的情况,比如22端口由哪些程序占用
    [root@localhost workspace]# lsof -i:22
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    sshd    1967 root    3u  IPv4  14379      0t0  TCP *:ssh (LISTEN)
    sshd    1967 root    4u  IPv6  14385      0t0  TCP *:ssh (LISTEN)
    sshd    2705 root    3r  IPv4  22506      0t0  TCP 192.168.1.102:ssh->192.168.1.101:56867 (ESTABLISHED)
    sshd    2709   wm    3u  IPv4  22506      0t0  TCP 192.168.1.102:ssh->192.168.1.101:56867 (ESTABLISHED)

  • pgrep命令查找当前运行的进程,并列出匹配给定条件进程的pid
    [root@localhost workspace]# pgrep auditd
    435
    2582

  • killall命令通过程序名字直接杀死所有进程。
  • 系统日志syslog.conf的配置详解。/etc/syslog.conf
  • 数据备份,如果用scp,建议尽量用它的限速参数;而rsync则尽量选择在非业务时间段执行。
  • 配置tcp参数
    [root@localhost ~]# vim /etc/sysctl.conf 

你可能感兴趣的:(Linux)