zoukankan      html  css  js  c++  java
  • 网络相关配置

    1.查看网卡信息和网络相关配置

    [root@localhost ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 #ens33:物理网卡名称 网卡最大传输单元(字节)
            inet 192.168.86.132  netmask 255.255.255.0  broadcast 192.168.86.255 #ip地址(动态dhcp或静态) 子网掩码 
            inet6 fe80::20c:29ff:fec2:ce44  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:c2:ce:44  txqueuelen 1000  (Ethernet) #MAC地址
            RX packets 114  bytes 12256 (11.9 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 121  bytes 16352 (15.9 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 #ip地址(默认是127.0.0.1)
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 1  (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地址

    [root@localhost ~]# ifconfig ens33 192.168.86.137/24

    DNS配置文件:

    [root@localhost ~]# cat /etc/resolv.conf
    ; generated by /usr/sbin/dhclient-script
    search localdomain
    nameserver 192.168.86.2 #DNS服务器地址

    配置网关:

    [root@localhost ~]# route add default gw 192.168.86.1 netmask 255.255.255.0
    [root@localhost ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.86.1    255.255.255.0   UG    0      0        0 ens33 #访问255.255.255.0子网掩码下的所有ip地址都通过网关192.168.86.1
    192.168.86.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33 #访问255.255.255.0下的192.168.86.0区段ip不通过网关

    [root@localhost ~]# route del default gw 192.168.86.1
    [root@localhost ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.86.0    0.0.0.0         255.255.255.0   U     0      0        0 ens33

    以上修改在重启network.service之后失效:

    [root@localhost ~]# systemctl restart network.service
    [root@localhost ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.86.132  netmask 255.255.255.0  broadcast 192.168.86.255
            inet6 fe80::20c:29ff:fec2:ce44  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:c2:ce:44  txqueuelen 1000  (Ethernet)
            RX packets 205  bytes 22558 (22.0 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 256  bytes 35087 (34.2 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    2.永久性修改网络配置

    网络配置文件保存在/etc/sysconfig/network-scripts/文件夹下,每一块网卡对应的配置文件名称为ifcfg-网卡名。

    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
    TYPE=Ethernet #以太网协议
    BOOTPROTO=dhcp #通过dhcp服务器获取ip地址
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=ens33
    UUID=93020a64-1466-4aa0-8a25-debde3b0fd9d
    DEVICE=ens33
    ONBOOT=yes #网络服务启动时自动加载

    将ip地址改为静态

    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
    TYPE=Ethernet
    BOOTPROTO=static
    IPADDR=192.168.86.132
    NETMASK=255.255.255.0
    GATEWAY=192.168.86.2
    DNS1=172.16.23.168
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=ens33
    UUID=93020a64-1466-4aa0-8a25-debde3b0fd9d
    DEVICE=ens33
    ONBOOT=yes
    [root@localhost ~]# systemctl restart network.service

     3.本地DNS解析

    DNS服务器会自动把域名解析成ip地址:

    [root@localhost ~]# ping www.baidu.com
    PING www.baidu.com (119.75.216.20) 56(84) bytes of data.

    可以通过/etc/hosts文件进行本地解析:

    [root@localhost ~]# vim /etc/hosts
    [root@localhost ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.86.133 python-web1

    [root@localhost ~]# ping python-web1
    PING python-web1 (192.168.86.133) 56(84) bytes of data.

    4.查看和设置主机名

    [root@python-server ~]# hostname
    python-server
    [root@python-server ~]# hostnamectl set-hostname python-main

    重新打开终端后会发现左边的主机名已更改:

    [root@python-main ~]# hostname
    python-main

    4.ssh服务

    通过ssh可以连接到其他主机:

    [root@localhost .ssh]# ssh 192.168.86.133
    The authenticity of host '192.168.86.133 (192.168.86.133)' can't be established.
    ECDSA key fingerprint is SHA256:ZhlE0+oLA8/4USl1OP0++VOTYymFpcFkpu9rh+5V1To.
    ECDSA key fingerprint is MD5:57:6c:7b:22:6e:bf:88:82:08:4c:c1:01:2b:5c:74:21.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.86.133' (ECDSA) to the list of known hosts.
    root@192.168.86.133's password:
    Last login: Sat Oct 14 21:34:14 2017 from python-web1
    mount: /dev/sr0 写保护,将以只读方式挂载
    mount: /dev/sr0 已经挂载或 /mnt 忙
           /dev/sr0 已经挂载到 /mnt 上

    如果在/etc/hosts中指定了域名和ip地址,可以通过域名连接:

    [root@localhost ~]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.86.133 python-web1
    192.168.86.137 python-main
    [root@localhost ~]# ssh python-web1
    The authenticity of host 'python-web1 (192.168.86.133)' can't be established.
    ECDSA key fingerprint is SHA256:ZhlE0+oLA8/4USl1OP0++VOTYymFpcFkpu9rh+5V1To.
    ECDSA key fingerprint is MD5:57:6c:7b:22:6e:bf:88:82:08:4c:c1:01:2b:5c:74:21.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'python-web1' (ECDSA) to the list of known hosts.
    root@python-web1's password:
    Last login: Sat Oct 14 21:34:52 2017 from python-web1
    mount: /dev/sr0 写保护,将以只读方式挂载
    mount: /dev/sr0 已经挂载或 /mnt 忙
           /dev/sr0 已经挂载到 /mnt 上

    可以通过ssh向别的主机上传文件或者从别的主机下载文件:

    [root@localhost ~]# scp /etc/hosts 192.168.86.133:/ect/hosts
    修改ssh服务的配置文件:

    [root@localhost ~]# vim /etc/ssh/sshd_config

    可以修改ssh软件的端口号(默认端口号22)

    修改端口号之后,再用ssh连接该主机就需要指定端口号了:

    [root@python-main ~]# ssh python-main -p 22

    使用密钥的方式连接:

    [root@localhost ~]# ssh-keygen

    将密钥发送给另一台主机

    [root@localhost ~]# ssh-copy-id -i python-web1

    之后再连接该主机就不需要输入密码

  • 相关阅读:
    jsp 特殊标签
    poj 1753 Flip Game 高斯消元 异或方程组 求最值
    zoj 3155 Street Lamp 高斯消元 异或方程组 求方案数
    poj1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组 模板
    zoj 3930 Dice Notation 模拟
    zoj 3157 Weapon 线段树求逆序对数
    hdu 1242 Rescue BFS+优先队列
    hdu 3466 Proud Merchants 贪心+01背包
    zoj 3689 Digging 贪心+01背包
    hdu 2602 Bone Collector 01背包模板
  • 原文地址:https://www.cnblogs.com/Icarus1900/p/7668349.html
Copyright © 2011-2022 走看看