1、设置ubuntu的ip地址
临时修改
1. 设置IP sudo ifconfig eth0 203.171.239.155 netmask 255.255.255.224 这样就算设置好了网卡eth0的IP地址和子网掩码
2. 设置网关 sudo route add default gw 203.171.239.129
3. 设置DNS 修改/etc/resolv.conf,在其中加入
nameserver DNS的地址1
nameserver DNS的地址2
不过,这样设置之后,下次开机时候IP又不存在了。
永久修改
直接修改系统配置文件 ubuntu的网络配置文件是:/etc/network/interfaces
打开后里面可设置DHCP或手动设置静态ip。前面auto eth0,让网卡开机自动挂载.
1. 以DHCP方式配置网卡编辑文件,
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地址编辑文件
sudo vi /etc/network/interfaces
并用下面的行来替换有关eth0的行:
# The primary network interface
auto eth0
iface eth0 inet static
address 203.171.239.155
gateway 203.171.239.129
netmask 255.255.255.224
#network 203.171.239.128 #broadcast 192.168.3.159
将上面的ip地址等信息换成你自己就可以了.
用下面的命令使网络设置生效: sudo /etc/init.d/networking restart
执行完后用ifconfig -a查看了一下发现也没有变,又用下面命令试验了一下
sudo ifdown enp3s0;ifup enp3s0 重新开关网卡
最后也不知道哪个好使,可能表面不行,实际已经生效
2. 设定第二个IP地址(虚拟IP地址) 编辑文件
sudo vi /etc/network/interfaces
在该文件中添加如下的行:
auto eth0:1 iface eth0:1 inet static
address 192.168.1.155
netmask 255.255.255.0
work 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
3. 设置主机名称(hostname)
使用下面的命令来查看当前主机的主机名称:
sudo /bin/hostname
使用下面的命令来设置当前主机的主机名称:
sudo /bin/hostname newname 系统启动时,它会从/etc/hostname来读取主机的名称.
4. 配置DNS
首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址,这是简单使用本机的静态查询.
更多的网址需要访问DNS 服务器来进行查询
假设DNS服务器的IP地址是203.171.230.6
vim /etc/resolv.conf
添加内容应为:
nameserver 203.171.230.6
重启网络:/etc/init.d/networking restart
但是DNS的这种设置只是临时设置,重新开机后会被重置。
要想永久更改需要
sudo vi /etc/network/interfaces
加入如下方式的内容:
dns-nameservers xxx.xxx.xxx.xxx
5,使用代理服务器上网
vim /etc/profile 最后一行加入
http_proxy=http://192.168.X.X:8087
https_proxy=http://192.168.X.X:8087
ftp_proxy=http://192.168.X.X:8087
no_proxy=http://192.168.20.
#访问局域网地址192.168.20.0/24网段时不使用代理,可以用逗号分隔多个地址
在系统安装时输入的代理服务器会出现在这个文件中
/etc/apt/apt.conf
6,测试
检验是否可以连通,就使用ping命令ping 网关,开始的时候总是显示unreachable
7,参考资料
Dynamic IP Address Assignment (DHCP Client)
To configure your server to use DHCP for dynamic address assignment, add the dhcp method to the inet address family statement for the appropriate interface in the file /etc/network/interfaces. The example below assumes you are configuring your first Ethernet interface identified as eth0.
auto eth0 iface eth0 inet dhcp
By adding an interface configuration as shown above, you can manually enable the interface through the ifup command which initiates the DHCP process via dhclient.
sudo ifup eth0
To manually disable the interface, you can use the ifdown command, which in turn will initiate the DHCP release process and shut down the interface.
sudo ifdown eth0
Static IP Address Assignment
To configure your system to use a static IP address assignment, add the static method to the inet address family statement for the appropriate interface in the file /etc/network/interfaces. The example below assumes you are configuring your first Ethernet interface identified as eth0. Change the address, netmask, and gateway values to meet the requirements of your network.
auto eth0 iface eth0 inet static address 10.0.0.100 netmask 255.255.255.0 gateway 10.0.0.1
By adding an interface configuration as shown above, you can manually enable the interface through the ifup command.
sudo ifup eth0
To manually disable the interface, you can use the ifdown command.
sudo ifdown eth0