Ubuntu网络设置方法

时间:2025-02-28 15:01:46

在使用Ubuntu时经常会遇到网络设置问题,一般情况只需在桌面上进行设置即可。但有时也会遇到没有桌面、无法使用桌面或不方便使用桌面的情况,这时就只能使用命令行来进行设置,本文介绍几个用于设置Ubuntu网络的命令。

1. ifconfig

  • 配置eth0的IP地址,同时激活该设备
ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  • 配置eth0别名设备eth0的IP地址
ifconfig eth0 192.168.1.3
  • 激活设备
ifconfig eth0 up
  • 禁用设备
fconfig eth0 down
  • 查看指定的网络接口的配置。
ifconfig eth0
  • 查看所有的网络接口配置。
ifconfig

2. route

可以使用route命令来配置并查看内核路由表的配置情况。

  • 添加到主机的路由
route add –host 192.168.1.2 dev eth0:0
route add –host 10.20.30.148 gw 10.20.30.40
  • 添加到网络的路由
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
  • 添加默认网关
route add default gw 192.168.1.1
  • 查看内核路由表的配置
route
  • 删除路由
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

3. netstat

可以使用netstat命令来显示网络状态信息。

  • 显示网络接口状态信息
netstat –i
  • 显示所有监控中的服务器的Socket和正使用Socket的程序信息
netstat –lpe
  • 显示内核路由表信息
netstat –r
netstat –nr
  • 显示TCP/UDP传输协议的连接状态
netstat –t
netstat –u

4. hostname

查看主机名

# 直接运行
hostname

# 或者
cat /etc/hostname

修改主机名 

hostnamectl set-hostname xxx

参考文献

/zxouxuewei/p/

Ubuntu 网络配置方法 - 背对背依靠 - 博客园