Ubuntu 配置网络命令

本文提供了一个详细的指南,介绍如何在Ubuntu系统中配置网络接口,包括使用DHCP自动获取IP地址、手动设置静态IP地址、添加虚拟IP地址、设置主机名及DNS配置等内容,并附带了关闭Ubuntu和CentOS防火墙的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以eth0为例


1. 以DHCP方式配置网卡

编辑文件: /etc/network/interfaces:

sudo vi /etc/network/interfaces


并用下面的行来替换有关eth0的行:

# The primary network interface, 
# use DHCP to find our address
auto eth0
iface eth0 inet dhcp



用下面的命令使网络设置生效:

sudo /etc/init.d/networking restart


当然,也可以在命令行下直接输入下面的命令来获取地址

sudo dhclient eth0



2. 为网卡配置静态IP地址

编辑文件: /etc/network/interfaces:

sudo vi /etc/network/interfaces


并用下面的行来替换有关eth0的行:

# The primary network interface, 
# use Static to find our address
auto eth0
iface eth0 inet static
address 192.168.1.98            
gateway 192.168.1.1             
netmask 255.255.255.0           
network 192.168.1.0
broadcast 192.168.1.255


用下面的命令使网络设置生效:

sudo /etc/init.d/networking restart

注: 生效后,需要配置DNS后才可上网(步骤5)



3. 设定第二个IP地址(虚拟IP地址)

编辑文件/etc/network/interfaces:

sudo vi /etc/network/interfaces


在该文件中添加如下的行:

auto eth0:1
iface eth0:1 inet static
address 192.168.1.60
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x

根据你的情况填上所有诸如address,netmask,network,broadcast和gateways等信息. 


用下面的命令使网络设置生效:

sudo /etc/init.d/networking restart



4. 设置主机名称(hostname)

使用下面的命令来查看当前主机的主机名称:

sudo /bin/hostname


使用下面的命令来设置当前主机的主机名称:

sudo /bin/hostname newname

系统启动时,它会从/etc/hostname来读取主机的名称.

关于设置主机名称的更多信息,请访问这里



5. 配置DNS

首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址

要访问DNS 服务器来进行查询,需要设置/etc/resolv.conf文件. 


例如: 假设DNS服务器的IP地址是192.168.1.1

编辑文件: /etc/hosts

sudo vi /etc/hosts


编辑内容如下:

nameserver 192.168.1.1



或者,编辑/etc/resolv.conf,添加内容如下:

sudo  vi  /etc/resolv.conf



用下面的命令使网络设置生效:

sudo /etc/init.d/networking restart


Ubuntu 关闭防火墙

root@ubuntu:/home/homer# ufw status            // 查看状态
Status: inactive
root@ubuntu:/home/homer#
ufw enable          // 打开防火墙
Firewall is active and enabled on system startup
root@ubuntu:/home/homer#
ufw status
Status: active
root@ubuntu:/home/homer# ufw disable          // 关闭防火墙
Firewall stopped and disabled on system startup
root@ubuntu:/home/homer#
ufw status
Status: inactive

1、关闭ubuntu的防火墙 
      ufw disable
2、卸载了iptables
       apt-get remove iptables
3、关闭ubuntu中的防火墙的其余命令
        iptables -P INPUT ACCEPT
        iptables -P FORWARD ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -F


CentOS 关闭防火墙

/etc/init.d/iptables stop

[root@homeros logs]# /etc/init.d/iptables status       // 查看防火墙状态
iptables: Firewall is not running.
[root@homeros logs]# /etc/init.d/iptables start          // 开启防火墙
iptables: Applying firewall rules:                         [  OK  ]
[root@homeros logs]# /etc/init.d/iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

[root@homeros logs]# /etc/init.d/iptables stop          // 关闭防火墙
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@homeros logs]# /etc/init.d/iptables status
iptables: Firewall is not running.



推荐参考:

英文原文:http://www.ubuntugeek.com/ubuntu-networking-configuration-using-command-line.html

centos 永久关闭防火墙方法


### 如何在 Ubuntu 系统中配置 IP 地址 #### 使用命令行工具临时设置 IP 地址 对于需要快速测试或临时更改的情况,可以利用 `ifconfig` 或者更现代的 `ip addr` 命令来分配动态或静态IP地址。例如: ```bash sudo ip addr add 192.168.1.10/24 dev eth0 ``` 这条指令会向网卡eth0添加一个IPv4地址192.168.1.10以及子网掩码255.255.255.0。 为了使改动立即生效并重启网络服务,可执行如下命令: ```bash sudo systemctl restart networking.service ``` 或者使用较新版本中的NetworkManager服务替代上述操作[^1]。 #### 配置静态 IP 地址 (适用于Ubuntu 17.10及以上) 自Ubuntu 17.10起,默认采用Netplan作为网络管理工具。要永久设定静态IP地址,则需编辑位于 `/etc/netplan/` 下的相关YAML格式配置文件。通常情况下,该目录内存在名为`*.yaml` 的文件,具体名称可能依据安装环境而异。打开此文件后按照样例修改成所需的网络参数[^2]: ```yaml network: version: 2 renderer: networkd ethernets: ens33: dhcp4: no addresses: - 192.168.1.10/24 gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] ``` 保存变更之后运行下面两条命令应用新的配置并确认其有效性: ```bash sudo netplan apply ping -c 4 www.example.com ``` 如果一切正常,应该能够成功解析域名并通过互联网访问外部资源。 #### 图形界面下的简单配置选项 除了通过终端进行复杂的手动调整外,在桌面环境中还可以借助于GNOME控制中心内的“网络”面板轻松完成基本的有线/Wi-Fi连接属性定制工作。只需点击对应设备旁边的齿轮图标进入详情页面即可找到用于指定固定IP等相关选项的位置[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值