Unix平台下的常用命令技巧之二 TCP/IP网络配置

论坛 期权论坛 脚本     
匿名技术用户   2020-12-30 09:34   11   0

Unix平台下的常用命令技巧之二 TCP/IP网络配置<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

李守亮 slli@founder.com.cn

Last version2003-2-18

简述

遇到网址变动,服务器借用等原因,经常需要改变服务器的IP或主机名。遇到某些软件需绑定特殊IP或必须使用多IP的情况,本节将提供暂时和永久几种方法供参考。同时,涉及一些和TCP/IP配置相关的一些问题。本节以Sun Solaris 5.8为主,其他Unix系统可供参考。

欢迎在方正集团公司内部转发、传播,欢迎补充,欢迎指正。

目录

1、如何在Unix上使用ifconfig命令配置IP address #立即生效,重启失效

2、如何使用sys-unconfig 还原Sun Sorlaris的配置?

3、如何在Unix上通过修改文件配置IP address#永久方法

4、如何修改主机名(hostname)

5、修改网关后如何使之立即生效?

6、如何用命令方式实现单网卡多IP,以及如何删除 #立即生效,重启失效

7、在Solaris上如何实现单网卡永久使用多个IP #重启机器后仍有效

8ifconfig的一些常用用法。

9、如何在Solaris x86上安装网卡驱动?

10、如何在Sun上安装多块网卡?例如,如何在Sun工作站上安装3块网卡?

11、如何在程序中获取本机MAC地址? #本节摘录,仅供参考

12、在Solaris/FreeBSD/Linux平台上如何确定网卡Capability/Speed

13、如何直接联接其他Unix机器名(不用IP)?

14DNS服务器的设置参见单独文档

15、待补充

内容

=========================================================

1、如何在Unix上使用ifconfig命令配置IP address?(立即生效,重启失效)

语法:

ifconfig <interface> <addess> netmask <mask> broadcast <broad_addr>

示例:

#ifconfig hme0 162.105.183.12 netmask 255.255.255.0 broadcast 162.105.183.255

#ifconfig –a

lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1

inet 127.0.0.1 netmask ff000000

hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.12 netmask ffffff00 broadcast 162.105.183.255

ether 0:3:ba:1f:f2:9d

=========================================================

2、如何使用sys-unconfig 还原Sun Sorlaris的配置?

root身份下,运行#sys-unconfig,还原所有sun配置。系统会重新启动, 重新配置各项参数。运行此命令式一定要小心,sys-unconfig做以下事情:

o Saves current /etc/inet/hosts file information in /etc/inet/hosts.saved.

o If the current /etc/vfstab file contains NFS mount entries, saves the /etc/vfstab file to /etc/vfstab.orig.

o Restores the default /etc/inet/hosts file.

o Removes the default hostname in /etc/hostname.interface files for all interfaces con-figured when this command is run. To determine which interfaces are configured, run the command 'ifconfig- a'. The /etc/hostname.interface files corresponding to all of the interfaces listed in the resulting out- put, with the exception of the loopback interface (lo0), will be removed.

o Removes the default domainname in /etc/defaultdomain.

o Restores the timezone to PST8PDT in /etc/TIMEZONE.

o Disables the Network Information Service (NIS) and Network Information Service Plus (NIS+) if either NISor NIS+ was configured. Network Information Service Plus (NIS+) if either NIS or NIS+ was configured.

o Removes the entries for this host in /etc/net/*/hosts.

o Removes the file /etc/inet/netmasks.

o Removes the file /etc/defaultrouter.

o Removes the password set for root in /etc/shadow.

o Removes the file /etc/.rootkey.

o Executes all system configuration applications. These applications are defined by prior executions of a sysidconfig -a application. (See sysidconfig(1M)). When sys-unconfig is run, all system configuration applications are passed one argument, -u.

o Removes the file /etc/resolv.conf/

When sys-unconfig is finished, it performs a system shut- down. sys-unconfig is a potentially dangerous utility and can only be run by the super user.

相关文件:

/etc/default/init

process control initialization

/etc/defaultdomain

/etc/defaultrouter

/etc/hostname.interface

/etc/inet/hosts

host name database

/etc/inet/netmasks

network mask database

/etc/net/*/hosts

/etc/nodename

/etc/nodename

/etc/.rootkey

super-user's secret key

/etc/shadow

shadow password file

/etc/vfstab

virtual file system table

/var/nis/NIS_COLD_START

/var/yp/binding/*/ypservers

相关文档参见:#man sys-unconfig

=========================================================

3、如何在Unix上通过修改文件配置IP address #永久方法

如遇到网址变动,服务器借用等原因,需要改变服务器的IP或主机名,临时方法重新启动机器就会失效,若需要永久的改变,使用sys-unconfig命令未免有些大炮打蚊子的感觉,重装系统更不值,可以参见下面方法实现。

Solaris 改地址方法:

修改以下文件。注:/etc下的hostsnetworksnetmasks实际上是对/etc/inet下的文件作了符号联接。

1). /etc/hosts #加入(修改)机器名和地址

例如:

127.0.0.1 localhost

162.105.183.12 sllisun loghost

2). /etc/ networks

例如:

loopback 127

arpanet 10 arpa # Historical

3). /etc/netmasks #修改子网掩码

例如:

162.105.183.0 255.255.255.0

4). /etc/net/*/hosts #*ticltsticotsticotsord三个目录, 改三个目录下的hosts

例如:

#ident "@(#)hosts 1.2 92/07/14 SMI" /* SVr4.0 1.2 */

# RPC Hosts

sllisun sllisun

5). /etc/nodename #nodename为主机名,这个文件很容易漏改

例如:

sllisun

6). /etc/hostname.<interface> #interface为网卡名字

例如:

文件/etc/hostname.hme0

sllisun

7). /etc/defaultrouter #加入网关地址

一般这个文件安装时不存在,如果不存在,需要手工创建。

例如:

162.105.183.1

8. /etc/resolv.conf #DNS客户机的设置,改域名

例如:

domain rd.corp.founder.com.cn

nameserver 162.105.170.59

如果有可用的domainnameserver,设置好DNS客户机,你不但可以直接ping 通网上邻居的主机名,还可以通过sun上网、收发mail了。

Linux上有专用的工具netconfig配置,在此不再遨述。

============================================================

4 如何修改主机名(hostname)

1).需要修改如下文件

/etc/hosts

/etc/hostname.<interface>

/etc/nodename

/etc/net/*/hosts (3 files, man -s 7D ticotsord)

2).如果你运行在VxVM下,则应该

# vxdctl hostid <new_name>

# vxdctl init <new_name>

=========================================================

5、修改网关后如何使之立即生效?

/etc/目录下有一个网关配置文件defaultrouter。一般安装系统后,这个文件不存在,如手工创建或修改后,不重启机器使之立即生效,可用下面命令。

例如:

1).创建文件/etc/defaultrouter

#touch /etc/defaultrouter

#echo 162.105.183.1 >/etc/defaultrouter

2).#route –f add default `cat /etc/defaultrouter` 1

=========================================================

6、如何用命令方式实现单网卡多IP,以及如何删除?#立即生效,重启失效

使用ifconfig命令,增加IP

语法:ifconfig <interface> addif <ip> netmask <netmask> broadcast <broadcast>

示例:

1)检查网卡类型

# ifconfig -a

lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1

inet 127.0.0.1 netmask ff000000

hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.12 netmask ffffff00 broadcast 162.105.183.255

ether 0:3:ba:1f:f2:9d

2)增加ip 地址162.105.183.249 到网卡hme0

#ifconfig hme0 addif 162.105.183.249

Created new logical interface hme0:1

# ifconfig -a

lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1

inet 127.0.0.1 netmask ff000000

hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.12 netmask ffffff00 broadcast 162.105.183.255

ether 0:3:ba:1f:f2:9d

hme0:1: flags=1000842<BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.249 netmask ffff0000 broadcast 162.105.255.255

同样,增加其他掩码和广播地址用下列命令:

# ifconfig hme0 addif 162.105.183.250 netmask 255.255.255.0 broadcast 162.105.183.255

Created new logical interface hme0:2

使用ifconfig命令,删除多IP

语法:ifconfig <interface> removeif <ip>

示例:

# ifconfig hme0 removeif 162.105.183.249

=========================================================

7、在Solaris上如何实现单网卡永久使用多个IP

下面以lance ethernet (le0) 设备为例

成功案例示例:

1) 编辑/etc/hosts文件

162.105.183.12 myhost

162.105.183.249 myhost2

162.105.183.250 myhost3

2) 创建/etc/hostname.le0:n文件,注意hostname.le0:0就是hostname.le0

/etc/hostname.le0 (内容为 myhost)

/etc/hostname.le0:1 (内容为myhost2)

/etc/hostname.le0:2 (内容为myhost3)

注意这种文件就一行内容,主机名。

3) 如果想立即生效

# ifconfig le0:1 up

# ifconfig le0:1 162.105.183.249

# ifconfig le0:2 up

# ifconfig le0:2 162.105.183.250

或重新启动服务器,自动增加新IP

4)使之失效

# ifconfig le0:1 down

# ifconfig le0:2 down

5ifconfig -a验证

注:以下为摘录,供参考。

摘录一:

Q: Solaris 8下如何给一块以太网卡赋予多个IP地址?

A:

ifconfig le0 plumb

ifconfig le0 ether 0:1:2:3:4:5

ifconfig le0:1 plumb

ifconfig le0:1 ... up

ifconfig le0:2 plumb

ifconfig le0:2 ... up

/etc/init.d目录下修改IP地址、子网掩码等设置。

D: scz <scz@nsfocus.com>

有三个文件需要注意,/etc/rcS.d/S30rootusr.sh(/etc/init.d/rootusr)/etc/rc2.d/S69inet(/etc/init.d/inetinit)/etc/rc2.d/S72inetsvc(/etc/init.d/inetsvc)

摘录二:

Q: 如何在一块物理网卡上绑定多个IP地址

A: Sun Microsystems 1997-10-27

所谓虚拟网络接口指一个物理接口多个不同IP地址,Solaris允许一个物理网络接口对应多个逻辑接口,换句话说,即使只有一块网卡,也可以配置多个IP地址。参看ifconfig(1M)手册页。对于Solaris 2.x,可以在一块网卡上绑定256个不同IP地址。

Sun OS 4.x(Solaris 1.x)不支持。

/usr/sbin/ndd -get /dev/ip ip_addrs_per_if

对于Solaris 2.6,通过ndd可以配置超过256(0-255)IP地址。

/usr/sbin/ndd -set /dev/ip ip_addrs_per_if 1-8192

将这条命令增加到/etc/rc2.d/S69inet启动脚本中去。

1) 编辑/etc/hosts文件(或者nis host map),为每个虚拟接口增加条目。别忘记修

NISNIS+DNS数据库。

2) 为每个接口创建/etc/hostname.<interface:#>文件,比如/etc/hostname.le0:1

hostname.le0:2hostname.le0:3 ... le0:255。文件内容为单行IP地址或者主

机名。比如创建如下文件

/etc/hostname.le0:1 (不要使用le0:0,那就是le0)

/etc/hostname.le0:2

Solaris 2.5.1下最多1024个虚拟接口。每个文件内容是自己对应的虚拟接口IP地址或者主机名。

3) 如果使用了子网,应该在/etc/netmasks中增加

network_address netmask

157.145.0.0 255.255.255.0

4) 重启系统

5) ifconfig -a验证

某些第三方应用程序此时可能会出问题。出于安全考虑,可以

ndd -set /dev/ip ip_forwarding 0

ndd -set /dev/ip ip_strict_dst_multihoming 1

参看RFC1112 - <<Host Extensions for IP Multicasting>>

如果因为配置虚拟接口出现不期望的路由,考虑手动"route delete"。可以增加一个启动脚本/etc/rc2.d/S99vif,用于完成这些任务。 对于Solaris 2.6,可能还需要ndd -set /dev/ip ip_enable_group_ifs 0 (2.6下缺省是17下缺省是0),将这条命令增加到/etc/rc2.d/S69inet启动脚本中去。

============================================================

8ifconfig的一些常用用法

其它用法参见# ifconfig

常用语法: ifconfig <interface> netmask <netmask> broadcast <broadcast>| -a|up|down

1)ifconfig命令使用-a选项查看机器中存在的所有接口,ifconfig命令中用具体接口代替-a选项时可查看该接口的所有参数:

示例:

# ifconfig -a

lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1

inet 127.0.0.1 netmask ff000000

hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.12 netmask ffffff00 broadcast 162.105.183.255

ether 0:3:ba:1f:f2:9d

# ifconfig hme0

hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2

inet 162.105.183.12 netmask ffffff00 broadcast 162.105.183.255

ether 0:3:ba:1f:f2:9d

2) ifconfig <interface> netmask <netmask> broadcast <broadcast>改变网络配置

示例:

#ifconfig hme0 162.105.183.12 netmask 255.255.255.0 broadcast 162.105.183.255

3)使hme0接口离线再回到在线

示例:

#ifconfig hme0 down

#ifconfig hme0 up

4)从分配的网络掩码和IP地址为hme0网络接口推算广播地址:

示例:

#ifconfig hme0 broadcast +

5)增加删除一个hme0接口的ip地址

示例:

#ifconfig hme0 addif 162.105.183.249 --------增加

#ifconfig hme0 removeif 162.105.183.249 --------删除

6)改为dhcp方式

语法# ifconfig <interface> | auto-dhcp | dhcp

其它用法参见# ifconfig

注意:使用ifconfig命令进行任何改变都会立即生效。

警告:如果通过telnet网络连接到主机上执行远程配置管理,一旦重新配置了接口就可能丢失联接。

============================================================

9 如何在Solaris x86上安装网卡驱动

不需要修改"pcn.conf"文件。如果只是"touch /reconfigure"Solaris x86检测到了网卡,但是"ifconfig -a"的时候只有loopback接口,于是我尝试如下步骤:

# drvconfig

# devlinks

# touch /reconfigure

重启动后一切ok

============================================================

10、如何在Sun上安装多块网卡?例如,如何在Sun工作站上安装3块网卡。

请遵循如下步骤:

1) Sun工作站上增加网卡

2) boot -r启动系统

3) 观察启动信息,确认每块网卡都被识别出来,比如这种信息

PCI-device: network@1,1, hme #0

SUNW,hme0 is /pci@1f,4000/network@1,1

实际中如果每块网卡都被识别出来,有hme0hme1 hme2,当然12可能不是这个名字。

4) /etc目录下创建hostname.hme0hostname.hme1 hostname.hme2。在每个文件中分别指定IP地址,编辑/etc/hosts文件增加相应入口。

5) 重启机器

============================================================

11 如何在程序中获取本机MAC地址 #摘录

Q: 如何在C代码中获取本机MAC地址,我用strace跟踪ifconfig

ioctl(4, SIOCGIFHWADDR, 0xbffffb80) = 0

ioctl(4, SIOCGIFADDR, 0xbffffb80) = 0

ioctl(4, SIOCGIFBRDADDR, 0xbffffb80) = 0

ioctl(4, SIOCGIFNETMASK, 0xbffffb80) = 0

D: Unix Programmer

gethostname()/gethostbyname()依赖于本机的域名解析系统,比如/etc/hosts文件、/etc/nsswitch.conf文件、/etc/resolv.conf文件。这样获取本机IP是不可靠的。如果/etc/hosts文件中没有指定本机IP,则依赖DNS是否配置了PTR资源记录。可靠的办法应该是strace ifconfigtruss ifconfig,实际就是照ifconfig的实现去获取本机IP

A: David Peter <dave.peter@eu.citrix.com>

straceLinux下的工具,由于HP-UX 10.20ioctl不支持SIOCGIFHWADDR,可能需要DLPI接口或者针对/dev/lan0NETSTAT ioctl,为了使用NETSTAT ioctl还需要重启动,而且HP不赞成继续使用NETSTAT ioctlHP-UX 11.00不再支持。

根据手头一个古老的工具,Digital Unixioctl支持SIOCRPHYSADDR。至于SGI上的IRIX,我想可能需要一个原始套接字,比如:

s = socket( PF_RAW, SOCK_RAW, RAWPROTO_SNOOP )

D: scz <scz@nsfocus.com> 2001-11-20 11:46

SPARC/Solaris下只有root用户才可以ifconfig -a看到本机MAC地址,普通用户并不能这样做,但可以尝试在dmesg输出中查找,由于dmesg使用的数据有可能被破坏,这个办法并不可靠。"arp <本机IP地址>"可以看到本机MAC地址,使用的技术实际上就是前面编程演示的ioctl( s, SIOCGARP, &arpreq )此外还可以用如下命令获取本机MAC地址

ndd /dev/arp arp_cache_report | grep MYADDR

============================================================

12 Solaris/FreeBSD/Linux平台上如何确定网卡Capability/Speed

1)Sun Solaris上:

# netstat -k hme0 | grep ifspeed

# ndd -get /dev/hme link_mode

0 半双工

1 全双工

# ndd -get /dev/hme link_status

0 Link Down

1 Link up

# ndd -get /dev/hme link_speed

0 10Mbps

1 100Mbps

2)FreeBSD上:

ifconfig命令就可以看到

status: active 网线接到一个HUB上了

status: no carrier 未接网线

3)Linux上:

在高版本的Linux系统中net-tools包中有一个mii-tool命令,可以用于检查这些数据,而不是溶合在ifconfig的输出中。

============================================================

13、如何直接联接Unix机器名(不用IP)?

UnixUnix,如果想通过hostname进行会话,需要配置入口文件/etc/hosts文件。

示例:/etc/hosts

127.0.0.1 localhost

162.105.183.12 sllisun loghost

162.105.183.8 sunserver1

162.105.183.9 sunserver2

162.105.183.10 newssun

162.105.183.13 zzssun

162.105.183.14 app250

162.105.183.15 enpsvr

162.105.183.35 aix

将对方IPhostname加到/etchosts中,就可以用hostname代替IP直接访问对方服务器了。

============================================================

14DNS服务器的设置参见单独文档

============================================================

15、待补充

李守亮 slli@founder.com.cn

2003-2-18

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP