Ubuntu网络连接配置

2016-06-29

Ubuntu网络连接配置

Ubuntu 22.04配置静态IP

编辑文件 /etc/netplan/00-installer-config.yaml (无线网卡为:00-installer-config-wifi.yaml)

network:
  ethernets:
    enp2s0:
      dhcp4: no
      addresses:
        - 192.168.1.10/24
      routes:
        - to: 0.0.0.0/0
          via: 192.168.1.2
      nameservers:
        addresses: [119.29.29.29,61.177.7.1]
  version: 2

重启网卡

netplan apply

查看dns

resolvectl 

Ubuntu 20.04配置静态IP

编辑文件 /etc/netplan/网卡名.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:                               #配置的网卡的名称
      addresses: [192.168.0.22/24]       #配置的静态ip地址和掩码
      dhcp4: no                          #关闭DHCP,如果需要打开DHCP则写yes
      optional: true
      gateway4: 192.168.0.1             #网关地址
      nameservers:
         addresses: [192.168.0.1,114.114.114.114]    #DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
  version: 2
  renderer: networkd

Ubuntu 20.04修改时区

tzselect     #选择需要的时区
cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

Ubuntu命令行模式下连接WIFI

iwconfig key方式适用于WEP

iwconfig wlan0 essid "TP-link" key 1234-5678

该方法对WPA加密方式不支持,解决方法是用wpasupplicant软件:

wpa_passphrase ESSID PWD > xxx.conf
wpa_supplicant -B -i wlan0 -Dwext -c ./xxx.conf
iwconfig wlan0
dhclient wlan0

Ubuntu命令行模式下配置网络连接

1.打开ubuntu的/etc/network/interfaces文件默认的内容如下:

auto lo
iface lo inet loopback

动态获取的配置方法:

auto eth0
iface eth0 inet dhcp 静态分配的配置方法:

auto eth0
iface eth0 inet static
address 192.168.0.2
netmask  255.255.255.0
gateway  192.168.0.1

2.添加域名服务器:打开/etc/resolv.conf文件

添加这行:

nameserver 202.99.166.4

3.重启下网络

$/etc/init.d/networking restart  (新版不能用)

或者重启指定网卡

$ifdown eth0 && ifup eth0

注:14.04 传统的service重启和停止网络已经不再支持了,需要通过使用ifdown & ifup来实现相应的操作。

直接配置网卡:

设置IP和掩码

$ifconfig eth0 192.168.0.2 netmask 255.255.255.0

设置网关

$route add default gw 192.168.0.1

修改MAC

有些时候需要修改网卡的硬件地址:

$ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX