Linux网络相关

10.11 Linux网络相关

ifconfig

ifconfig查看网卡状况以及IP等 //yum install -y net-tools安装ifconfig

ip addr用于查看所有的网卡

ifdown ens33 //关闭网卡ens33
ifup ens33 //打开网卡ens33,用这种方式可以单独重启指定的网卡,而不是重启整个网络服务

ifdown ens33 &&ifup ens33 //这样重启的效果更好

给一个网卡设定多个IP

[root@centos-01 ~]# cd /etc/sysconfig/network-scripts
[root@centos-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33:1  //复制一个配置文件
[root@centos-01 network-scripts]# vim ifcfg-ens33:1  //编辑配置文件,device名和name名后加:1,并更改IP地址
[root@centos-01 network-scripts]# ifdown ens33&&ifup ens33
[root@centos-01 network-scripts]# ifconfig         //重启后可以看到多了一个网卡ens33:1
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.128  netmask 255.255.255.0  broadcast 192.168.27.255
        inet6 fe80::57c0:b138:acc2:fc0f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1c:97:11  txqueuelen 1000  (Ethernet)
        RX packets 1132  bytes 103087 (100.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 596  bytes 93024 (90.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.27.150  netmask 255.255.255.0  broadcast 192.168.27.255
        ether 00:0c:29:1c:97:11  txqueuelen 1000  (Ethernet)

查看网卡连接状态

[root@centos-01 network-scripts]# mii-tool ens33 //link ok表示连接正常
ens33: negotiated 1000baseT-FD flow-control, link ok


[root@centos-01 network-scripts]# ethtool ens33  //末尾link detected:yes 表示连接正常
Settings for ens33:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                   drv probe link
    Link detected: yes

更改主机名

[root@centos-01 network-scripts]# hostname  //查看主机名
centos-01
[root@centos-01 network-scripts]# hostname centos-011 //更改主机名,但是这仅仅只是保存在内存中,重启后失效
[root@centos-01 network-scripts]# hostname
centos-011

vim /etc/hostname   //更改这个文件才是永久的办法

[root@centos-01 network-scripts]# hostnamectl set-hostname centos-011  //这个命令可以直接更改/ect/hostname文件
[root@centos-01 network-scripts]# hostname
centos-011
[root@centos-01 network-scripts]# cat /etc/hostname  //显示文件已更改
centos-011

改DNS

在网卡配置文件中改DNS是永久更改, /etc/sysconfig/network-scripts/ifcfg-ens33

但是在/etc/resolv.conf中也可以更改DNS,这里的更改是临时的。一般可以在此设置多个DNS,以做备用。

[root@centos-01 network-scripts]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29

/etc/hosts文件可以临时更改域名指向的IP,有时候会很有用。例如:

[root@centos-01 network-scripts]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.5 www.qq.com  //添加qq的域名

[root@centos-01 network-scripts]# ping www.qq.com //ping qq.com发现指向了刚刚我们指定的ip
PING www.qq.com (192.168.3.5) 56(84) bytes of data.

/hosts文件中,一行只能有一个ip, 一个ip后面可以跟多个域名,如果多行中出现同样的域名(ip不同),会以写在前面的为准

Linux系统中,如果你想要打开并配置名为ens33网络接口,绑定静态IP地址,并最终重启网络服务,可以按照以下步骤操作: 1. **查看网络接口**: 首先,你需要确认ens33是否已经存在。运行命令 `ifconfig` 或者 `ip addr` 查看所有网络接口及其状态。 2. **启用接口**: 如果ens33处于禁用状态,你可以使用 `sudo ifup ens33` 来启用它。 3. **配置静态IP**: 使用 `sudo nano /etc/network/interfaces` (对于旧版本的Ubuntu) 或 `sudo nano /etc/netplan/01-netcfg.yaml` (对于新版本的Ubuntu) 打开网络配置文件。在对应的ens33配置部分,添加以下内容,替换 `XX.XX.XX.XX` 和 `YY.YY.YY.YY` 为你的静态IP、子网掩码、网关和DNS服务器地址: ``` auto ens33 iface ens33 inet static address XX.XX.XX.XX netmask YY.YY.YY.YY gateway ZZ.ZZ.ZZ.ZZ dns-nameservers DD.DD.DD.DD ``` 4. **保存并应用更改**: 按 `Ctrl+X`,然后输入 `y` 确认保存,接着输入 `n` 选择不自动启动,因为接下来我们会手动重启。 5. **重启网络服务**: 使用 `sudo service networking restart` 或 `sudo systemctl restart network` 重启网络服务,以应用新的配置。 6. **验证配置**: 最后,你可以再次运行 `ifconfig` 或 `ip addr` 确认ens33接口的新IP配置。 **相关问题--:** 1. 我的ens33接口没有出现在ifconfig结果里,怎么办? 2. 如果我想设置多个DNS服务器,应该如何修改配置? 3. 如果静态IP配置失败,应该检查哪些方面的问题?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值