Get Connected Wi-Fi Network Password on Ubuntu

Get Connected Wi-Fi Network Password on Ubuntu

Connecting to a Wi-Fi network is an everyday necessity for most users, facilitating seamless access to the internet and online services. However, there are instances where recalling the Wi-Fi password becomes essential, especially when attempting to connect a new device or troubleshoot network issues. This tutorial demonstrates how to get connected Wi-Fi network password on Ubuntu.

The following command can be used to extract the Wi-Fi password (PSK - Pre-Shared Key) for a currently connected network from NetworkManager system connections configuration files:

sudo awk -F= '/psk=/{print $2}' /etc/NetworkManager/system-connections/$(iwgetid -r)*

Let's break down the command step by step:

  • awk - is a text processing tool on Linux.
  • -F= - sets the field separator to =, meaning it will split each line into fields based on the = character.
  • /psk=/ - this is the pattern to match lines that contain psk=.
  • {print $2} - this is the action to perform when the pattern is matched. It prints the second field.
  • /$(iwgetid -r) - gets the name of the wireless network the device is connected to (the -r option indicates the wireless network name). This name is then used as part of the file path.
  • * - acts as a placeholder to match any characters that follow the wireless network name. This is because file names in the /etc/NetworkManager/system-connections/ directory typically correspond to network configurations, and they often start with the network name.

Leave a Comment

Cancel reply

Your email address will not be published.