Linux 修改IP和hostname

 今日装了Redhat5,装完后要配置IP,网上找了资料,并亲试之。

效果不错,故记载之,以备后用。

修改IP地址及MAC地址:

a)        vim /etc/sysconfig/network-scripts/ifcfg-eth0

b)        DEVICE=eth0

c)         BOOTPROTO=static---静态还是动态

d)        BROADCAST=192.168.1.255

e)         IPADDR=192.168.1.35---IP地址

f)         NETMASK=255.255.255.0---子网掩码

g)        NETWORK=192.168.1.0---网络ID

h)        ONBOOT=yes 网开是否开启:

i)          HWADDR=00:00:00:00:00:00

j)         GATEWAY=192.168.1.2---网关

 

修改linux的服务器名:  

1. # vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=yourname //在这修改hostname
NISDOMAIN=eng-cn.platform.com

2.修改/etc/hosts里面的名字
# vi /etc/hosts
127.0.0.1 localhost.localdomain localhost
172.17.33.169 yourname //在这修改hostname

3.最后在终端下执行:
#hostname ***** //*****为修改的hostname

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

以下为修改网卡配置等

 修改网关

a)        编辑 /etc/sysconfig/network

b)        NETWORKING=yes

c)         HOSTNAME=vmlinux---主机名

d)        修改HOSTNAME的同时必须修改 /etc/hosts 文件

 

修改DNS

a)         Vim  /etc/resolv.conf 

b)        nameserver 202.102.192.68   

c)         nameserver 58.242.2.2 

 

创建虚拟网卡  地址在:/etc/sysconfig/network-scripts/     

a)        cp ifcfg-eth0 ifcfg-eth0:0       cp ifcfg-eth0 ifcfg-eth0:1

b)        ifcfg-eth0:0的文件里写入

c)         DEVICE=eth0:0 BOOTPROTO=static

IPADDR=192.168.168.2 NETMASK=255.255.255.0 O NBOOT=yes

d)        ifcfg-eth0:1的文件里写入

e)         DEVICE=eth0:0 BOOTPROTO=static

f)         IPADDR=192.168.168.3 NETMASK=255.255.255.0 ONBOOT=yes

然后reboot 重起后用ifconfig 查看就会发现多了一块虚拟网卡

 

你可以使用 Python 中的 `socket` 和 `subprocess` 模块来收集 Linux 系统上的 IP 地址和 MAC 地址。以下是一个示例代码: ```python import socket import subprocess # 获取主机名 host_name = socket.gethostname() # 获取 IP 地址 ip_address = socket.gethostbyname(host_name) # 获取所有网络接口信息 ifconfig_result = subprocess.run(['ifconfig'], stdout=subprocess.PIPE).stdout.decode('utf-8') # 解析出每个网络接口的 IP 地址和 MAC 地址 interface_info = {} for line in ifconfig_result.split('\n'): if line.startswith(' '): # 处理多行输出的情况 interface_info[interface_name]['output'].append(line.strip()) continue if ':' in line: # 解析出网络接口名称和 MAC 地址 parts = line.split() interface_name = parts[0].strip(':') mac_address = parts[1] interface_info[interface_name] = {'mac_address': mac_address, 'output': [line.strip()]} else: interface_name = None # 输出结果 print('Hostname:', host_name) print('IP address:', ip_address) for interface_name, info in interface_info.items(): print('Interface:', interface_name) print(' MAC address:', info['mac_address']) print(' Output:') for line in info['output']: print(' ', line) ``` 这段代码可以输出主机名、主机的默认 IP 地址,以及每个网络接口的 IP 地址和 MAC 地址。注意,如果一个网络接口有多个 IP 地址,这段代码只会输出其中的一个 IP 地址。如果需要获取所有 IP 地址,需要对代码进行相应的修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值