Which terminal command to get just IP address and nothing else?

后端 未结 29 1313
春和景丽
春和景丽 2020-12-04 06:16

I\'m trying to use just the IP address (inet) as a parameter in a script I wrote.

Is there an easy way in a unix terminal to get just the IP address, rather than loo

29条回答
  •  清歌不尽
    2020-12-04 06:33

    Command ifconfig is deprected and you should use ip command on Linux.

    Also ip a will give you scope on the same line as IP so it's easier to use.

    This command will show you your global (external) IP:

    ip a | grep "scope global" | grep -Po '(?<=inet )[\d.]+'
    

    All IPv4 (also 127.0.0.1):

    ip a | grep "scope" | grep -Po '(?<=inet )[\d.]+'
    

    All IPv6 (also ::1):

    ip a | grep "scope" | grep -Po '(?<=inet6 )[\da-z:]+'
    

提交回复
热议问题