linux获取ip地址命令(linux获取ip地址)
在Linux/unix系統(tǒng)下怎么查詢ip地址?
在Linux/unix系統(tǒng)下查詢ip地址可以使用如下的方法
1. 如果有圖形界面,直接選擇網(wǎng)絡(luò)管理器查看即可
2. 使用終端的命令行來獲取ip地址
輸入命令: /sbin/ifconfig。終端中會顯示大段網(wǎng)絡(luò)信息。如果出現(xiàn)錯誤信息,提示你缺少管理權(quán)限,請輸入sudo /sbin/ifconfig。
如果你使用的是Solaris系統(tǒng)或其他Unix版本,你需要輸入/sbin/ifconfig -a來顯示多個設(shè)備的信息
如果你不確定是哪個設(shè)備,可以查看最常用的、接入“eth0”端口的設(shè)備,也就是系統(tǒng)識別的第一個網(wǎng)絡(luò)適配器。如果你只想查看“eth0”的信息,請輸入ifconfig eth0 命令。
java獲取linux ip?
代碼示例:
/**
* 獲取Linux下的IP地址
*
* @return IP地址
* @throws SocketException
*/
public static String getLinuxLocalIp() throws SocketException {
String ip = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
String name = intf.getName();
if (!name.contains("docker") && !name.contains("lo")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress = inetAddress.getHostAddress().toString();
if (!ipaddress.contains("::") && !ipaddress.contains("0:0:")
&& !ipaddress.contains("fe80")) {
ip = ipaddress;
}
}
}
}
}
} catch (SocketException ex) {
System.out.println("獲取ip地址異常");
ex.printStackTrace();
}
System.out.println("IP:" + ip);
return ip;
}
Linux:怎樣獲取服務(wù)器IP地址?
第一步:激活網(wǎng)卡 系統(tǒng)裝好后默認(rèn)的網(wǎng)卡是eth0,用下面的命令將這塊網(wǎng)卡激活。
# ifconfig eth0 up 第二步:設(shè)置網(wǎng)卡進入系統(tǒng)時啟動想要每次開機就可以自動獲取IP地址上網(wǎng),就要設(shè)置網(wǎng)絡(luò)服務(wù)在系統(tǒng)啟動時也啟動。Linux有一點與windows不同的是很多服務(wù)默認(rèn)是停止的,而且你在某次使用該服務(wù)時啟動這個服務(wù),但是沒設(shè)置它為默認(rèn)啟動,則下次進入系統(tǒng)這個服務(wù)依然是停止的。下面是設(shè)置網(wǎng)絡(luò)服務(wù)在系統(tǒng)啟動時也啟動的方法。使用chkconfig命令讓網(wǎng)絡(luò)服務(wù)在系統(tǒng)啟動級別是2345時默認(rèn)啟動。# chkconfig --level 2345 network on第三步:修改網(wǎng)卡文件ifcfg-eth0修改ifcfg-eth0文件,設(shè)置ONBOOT的值為yes,讓網(wǎng)絡(luò)服務(wù)啟動時使用該網(wǎng)卡。設(shè)置BOOTPROTO的值為dhcp,讓網(wǎng)卡從DHCP服務(wù)器自動獲取IP地址。# vi /etc/sysconfig/network-scripts/ifcfg-eth0ONBOOT=yesBOOTPROTO=dhcp總結(jié):通常情況下第三步是最重要的,因為大部分Linux系統(tǒng)默認(rèn)網(wǎng)絡(luò)服務(wù)是系統(tǒng)啟動時也啟動的,網(wǎng)卡也是啟用的,只要設(shè)置好第三步,然后使用下面的命令啟動網(wǎng)絡(luò)服務(wù)就OK。# service network start配置靜態(tài)IP地址上網(wǎng)與動態(tài)IP地址上網(wǎng)同出一轍,修改ifcfg-eth0,然后用命令service network retart重啟網(wǎng)絡(luò)服務(wù)。DEVICE=eth0
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS=8.8.8.8
BOOTPROTO=static
ONBOOT=yesdebian: 編輯/etc/network/interfaces# The loopback network interfaceauto loiface lo inet loopback
# The primary network interface
allow-hotplug eth0auto eth0iface eth0 inet static
address 192.168.1.36
netmask 255.255.255.0
gateway 192.168.1.10 #公司的網(wǎng)關(guān)是192.168.1.10,一般是192.168.1.1
保存編輯結(jié)果,并重啟網(wǎng)絡(luò)服務(wù)
總結(jié)
以上是生活随笔為你收集整理的linux获取ip地址命令(linux获取ip地址)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯基础模块6_2:定时器进阶
- 下一篇: 备案 英文(备案书英文)