Linux/Wifi

From Omnia
Jump to: navigation, search

Common Tasks

Wireless interface tools:

ifconfig  # set interface ip address and up/down state
iwconfig  # configure wireless settings (SSID, key, mode)
iwlist    # scan wireless
dhclient  # dhcp client

Set ESSID:

iwconfig [interface] essid [essid]
iwconfig eth1 essid toast

Set managed mode:

# note: interface may need to be down to change (ifconfig eth1 down)
iwconfig eth1 mode Managed

Set WEP key:

iwconfig eth1 key 0123-4567-89
iwconfig eth1 key off

WPA:

# type password, hit enter
wpa_passphrase [SSID] >> /etc/wpa_supplicant/wpa_supplicant.conf

network={
        ssid="testnetwork"
        #psk="testpassword"
        psk=ed5b87ba31bf9d6611576f94c57e848696005ea2d5743baab976a48ed11d1b2a
}
# start wpa_supplicant daemon
wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlan0 -D madwifi
# debug mode:
wpa_supplicant -c wpa.conf -i wlan0 -d

DHCP client:

dhclient [interface]  # get DHCP address
dhclient -r [interface]  # release DHCP address
dhclient [interface] -H [hostname]  # get DHCP address and set hostname

Other examples:

iwconfig wlan0 mode managed
iwpriv wlan0 set AuthMode=WPAPSK
iwpriv wlan0 set EncrypType=TKIP
iwpriv wlan0 set WPAPSK="AP's wpa-preshared key"
iwconfig wlan0 essid "AP's SSID"

Scan:

iwlist [interface] scan

Common wireless interfaces:

eth1, wlan0, ath0

madwifi

See madwifi

Wireless WEP Authentication

Shared WEP key:

iwconfig ath0 key <yourkey>

# operating in a restricted, shared-key environment
iwpriv ath0 authmode 2

# operating in open system key (which is often considered more secure)
iwpriv ath0 authmode 1

Connecting to AP

Connecting to an open AP:

iwconfig ath0 essid "eddie"
iwconfig ath0 ap 00:06:25:E8:3A:05

Connect to first available open AP:

iwconfig ath0 ap any
iwconfig ath0 ap auto

Connecting with DHCP

DHCP Connection:

dhclient ath0
dhcpcd ath0

Connect without DHCP

ifconfig ath0 <IP address> netmask <netmask> up

route add default gw <gateway hostname>
route add default gw <gateway address>

vi /etc/resolv.conf
 search t0e.org
 nameserver 10.10.10.1

CentOS interface setup

/etc/sysconfig/network-scripts/ifcfg-ath0

DEVICE=ath0
BOOTPROTO=dhcp
HWADDR=00:0F:20:95:6A:9A
ONBOOT=no
DHCP_HOSTNAME=kencent

Ubuntu interface setup

/etc/network/interfaces:

iface lo inet loopback
    auto lo

iface eth0 inet dhcp

iface wlan0 inet manual
allow-hotplug wlan0
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp


Connecting to an AP with WEP

using shared WEP key:

iwpriv ath0 authmode 2

using open WEP key (which is often considered more secure):

iwpriv ath0 authmode 1

Specify WEP key:

iwconfig ath0 key <wep key (in hex)>
iwconfig ath0 key <s:ASCII string of key>

Connecting to an AP with WPA

Make PSK:

wpa_passphrase My_WPA_Protected_AP_ESSID "Some_Decent_PassPhrase_of_up_64_Characters" >> /etc/wpa_supplicant.conf

Edit /etc/wpa_supplicant.conf and add the lines into the network section created above:

    key_mgmt=WPA-PSK
#    proto=WPA
   proto=RSN

Uncomment the proto=WPA line (and comment out the RSN line) to enable WPA. Leave as is for WPA2 (stronger encryption).

Secure:

chmod 640 /etc/wpa_supplicant.conf

Full example:

modprobe ath_pci # if running pre-r1408 Madwifi code, then also: wlanconfig ath0 create wlandev wifi0 wlanmode sta
iwconfig ath0 essid "My_WPA_Protected_AP_ESSID"
ifconfig ath0 192.168.0.100 up
/usr/local/bin/wpa_supplicant -dd -Dmadwifi -iath0 -c/etc/wpa_supplicant.conf 

See if connected:

iwconfig ath0

Automate wpa_supplicant:

#!/bin/sh
modprobe ath_pci  (If running pre-r1408 Madwifi code, then also: wlanconfig ath0 create wlandev wifi0 wlanmode sta)
iwconfig ath0 essid "My_WPA_Protected_AP_ESSID"
ifconfig ath0 192.168.0.100 up
wpa_supplicant -Bw -Dmadwifi -iath0 -c/etc/wpa_supplicant.conf

old note:

wpa_supplicant -Bw -Dwext -iath0 -c/etc/wpa_supplicant.conf

# if you are using a Linux version smaller than 2.6.14
wpa_supplicant -Bw -Dmadwifi -iath0 -c/etc/wpa_supplicant.conf


References:

wifi init.d

#!/bin/sh
#
# chkconfig: - 99 01
# description: Starts and stops the Wireless
#
# lock:    /var/run/wifi
# config:  /etc/sysconfig/wifi
# config:  /etc/wpa_supplicant.conf


# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

# /etc/sysconfig/wifi
# WIFI_SSID=ssid
# WIFI_DHCP=yes
# WIFI_AUTH=wpa|wep
if [ ! -e /etc/sysconfig/wifi ] ; then
  echo "ERROR: /etc/sysconfig/wifi is missing"
  exit 1
else
  . /etc/sysconfig/wifi
fi

# WPA
# wpa_passphrase "My_WPA_Protected_AP_ESSID" "Some_Decent_PassPhrase_of_up_64_Characters" >> /etc/wpa_supplicant.conf


LOCKFILE=/var/run/wifi

case "$1" in
  start)
    echo -n "Starting wifi: "
    if [ -e $LOCKFILE ] ; then
      echo -n "Wifi already running"
      failure ; echo ; exit 1
    fi
    touch $LOCKFILE
    modprobe ath_pci
    iwconfig ath0 essid "${WIFI_SSID}"
    if [ "${WIFI_AUTH}" = "wpa" ] ; then
      wpa_supplicant -Bw -Dmadwifi -iath0 -c/etc/wpa_supplicant.conf
    else
      echo -n "code not written yet!"
      failure ; echo ; exit 1
    fi
    if [ "${WIFI_DHCP}" = "yes" ] ; then
      dhclient ath0 &> /dev/null
      if [ "$?" != "0" ] ; then
        echo -n "unable to obtain ip address!"
        failure ; echo ; exit 1
      fi
    else
      echo "code not written yet!"
      failure ; echo ; exit 1
    fi
    success ; echo ; exit 0
    ;;
  stop)
    echo -n "Stopping wifi:"
    rm -f $LOCKFILE
    dhclient -r ath0 &>/dev/null
    killall wpa_supplicant &>/dev/null
    modprobe -r ath_pci
    success ; echo ; exit 0
    ;;
  restart)
        $0 stop
        $0 start
        ;;
  status)
    if [ -e $LOCKFILE ] ; then
      echo "Wifi is running"
      exit 1
    else
      echo "Wifi is stopped"
      exit 1
    fi
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 2
esac

Notes

Restart network:

service network restart

Shut down madwifi:

ifconfig ath0 down
ifconfig wifi0 down


iwconfig ath0 mode master
iwconfig ath0 essid paradiso
iwconfig ath0 channel 9
iwpriv ath0 mode 2
ifconfig ath0 up

References:

WPA with Linux

wpa_passphrase YOURSSID yourpassphrase > /etc/wpa_supplicant.conf
chmod 640 /etc/wpa_supplicant.conf
wpa_supplicant -Bw -Dwext -iath0 -c/etc/wpa_supplicant.conf
# or use -Dmadwifi instead of -Dwext

References: