19

I'm wondering how to display a connection status using nmcli. I understand that the following will display a list of configured connections:

nmcli con show

And I also understand that the following will show only active connections:

nmcli con show --active

And that the following will display all settings for a connection (which is a very long list):

nmcli con show {connection_name}

My question is: Is there a quick way to display the status of a connection? Something similar to:

nmcli con status {connection_name}

Noting that the above is actually not a valid option on CentOS or Fedora.

3
  • 1
    Did you look at the --fields option? Something like nmcli -f field_name con show {connection_name}. If you give an invalid value for field_name some versions will print all valid field names. Depending on what you get you can possibly further refine by piping through grep or sed or the like.
    – B Layer
    Commented May 5, 2018 at 18:37
  • @B Layer Tried that but the --fields option only prints specific columns in the output, so if the argument --f ACTIVE is given, for example, and there are two active connections, it will print yes yes on two separate lines. That is it. Commented Jun 9, 2018 at 3:34
  • 1
    What do you want it to print in that case? It might be a good idea to put an example of the output you want in your question. BTW, did you try exactly what I metioned? nmcli -f {field_name} con show {connection_name}? And did try the other suggestion where you give an invalid field name?
    – B Layer
    Commented Jun 9, 2018 at 4:27

2 Answers 2

17

As user B Layer suggested in their comment, you can specify a field name with nmcli.

I think the most relevant field in your case is GENERAL.STATE:

nmcli -f GENERAL.STATE con show {connection_name}

For my current connection, this yields:

GENERAL.STATE: activated

1
4

If you need a true/false state, you could use nmcli's -g option in combination with grep's -q as follows:

nmcli -g GENERAL.STATE c s interface|grep -q -E '\bactiv'

Note the lack of e at the end of the word active. It is meant for future proofing if the word activated changes to for example active or activity or some other form at some point.

3
  • Thanks for sharing this. I adapted it slightly with grep -q -E '\bactiv' to make sure it doesn't match when nmcli might decide to print deactived if it's down ;-)
    – xpac
    Commented Sep 22, 2023 at 19:09
  • @xpac inactive connections would return nothing, at least on my PC.
    – annahri
    Commented Feb 16 at 2:04
  • @annahri You would interpret the return value of the process. Try adding ;echo "${?}" at the end.
    – GGets
    Commented Apr 24 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .