Unix与Linux中设置IP地址的方法分享

发布时间:2021-01-10编辑:脚本学堂
本文介绍下,在linux与unix中设置IP的方法,包括Linux、Solaris等系统中设置IP地址。有需要的朋友参考下。

一,修改IP地址和主机名
 
1、linux
 
1,修改固定IP
 

复制代码 代码示例:
# cat  /etc/sysconfig/network-scripts/ifcfg-eth1

2,重载网络
 

复制代码 代码示例:
# service network reload

3,更改主机名:
 

第一步:
#hostname oratest
第二步:
修改/etc/sysconfig/network中的hostname
第三步:
修改/etc/hosts文件

2,Solaris
方法一: ifconfig命令
 

复制代码 代码示例:
检测网络端口状态
#ifconfig -a
配置网络端口地址
#ifconfig le0 172.16.255.1 netmask 255.255.255.0
配置网络端口状态
#ifconfig le0 up/down
配置网络端口是否可用
#ifconfig le0 plumb/unplumb

方法二:
可以修改以下文件:
 

/etc/hosts
/etc/hostname.xxx
/etc/defaultrouter(默认网关)
/etc/netmasks(掩码)
/etc/inet/ipnodes

然后重新启动系统,或用ifconfig 来添加临时的IP地址.用route add default xxx.xxx.xxx.xxx来添加临时的默认网关,用这些命令更改的设置,在系统重启后就不会保存的。

注意修改IP,主机名的涉及的文件:
1,/etc/hostname.interface
Interface是网卡的型号,有le、hme等。Le是十兆网卡,hme为百兆网卡等等。后面跟一个数字,第一个十兆网卡为le0,第二个为le1;第二个百兆网卡为hme0,第二个为hme1等等。
文件的内容是这块网卡的名字,如Sunrise、Sunny。
 

复制代码 代码示例:
#more /etc/hostname.le0
Sunrise
# more /etc/hostname.hme0
Sunny

2,/etc/hosts文件
系统名与IP地址的映射
与/etc/hostname.interface 协同工作,配置本机网卡地址
 

复制代码 代码示例:
# more /etc/hosts
127.0.0.1 localhost loghost
172.16.255.1 Sunrise
172.18.255.1 Sunny

系统名不是机器名,机器名是唯一的,要更改机器名,用命令:hostname。
有了/etc/hostname.interface和/etc/hosts两个文件,系统就知道如何配置网卡了,如第一个百兆网卡的名字是Sunny,其对应的地址是172.18.255.1。

3,/etc/netmasks文件
将网络的IP地址与网络地址联系一起,划分子网
如果是标准网段,则不需要配置

标准网段:
 

A类网:0-127(127用于本地地址) 掩码:255..0.0.0
B类网:128-191 掩码:255.255.0.0
C类网:192-223 掩码:255.255.255.0
D类网:224-254(用于多址广播)

要配一个B类地址:172.16.255.1掩码为:255.255.255.0,则在/etc/netmasks文件中写:172.16.255.0 255.255.255.0

4,ifconfig命令
 

复制代码 代码示例:
检测网络端口状态
#ifconfig -a
配置网络端口地址
#ifconfig le0 172.16.255.1 netmask 255.255.255.0
配置网络端口状态
#ifconfig le0 up/down
配置网络端口是否可用
#ifconfig le0 plumb/unplumb

5,ping命令
检测网络状态
测试网络速度
就介绍这些吧,希望有助于大家理解linux中IP及相关网络配置。