Reverse tethering to Android device (non-ad-hoc)?

What is the easiest way to share my laptop’s network connection with my Android phone?

Using Suse12.3 and KDE 4.10.5, I tried to add a “shared” connection with Networkmanager, but my Android does not see it. Googling revealed that Android phones (or at least the Samsung brand) does not see “ad hoc”-wlan networks, which the “shared” option seems to create as far as I could find out.

I looked for a workaround to set up a genuine wlan from my laptop, but the only instructions that I found required disabling networkmanager. This does not work for me, since

  1. networkmanager seems very useful to me, as the laptop is moved around quite a lot
  2. the laptop connects to the net via a second wlan device itself, managed by networkmanager

I have no signal whatsoever inside that flat, so I connected a usb-wlan adapter to an external antenna mounted outside the house and to the laptop’s docking station. Now I want to use the laptop’s internal wlan adapter (which is unused at the flat) or maybe bluetooth or a usb cable to connect my android phone as well, whenever the laptop is docked there. I don’t need a high-speed connection, but something that is easy to switch on/off, as it is only needed when docked at home, but not at work.

I had a somewhat similar problem. I use an Acer netbook when travelling in remote areas (Australian outback), and connect to the 3G network with a USB data modem fed from an external antenna. This works well. Unfortunately my phone (a knock-off Samsung Galaxy) has a pitiful internal antenna and no jack to connect to my external 3G antenna. So its useless out of the city.

My solution was to set-up a wifi hotspot on the netbook and connect to it with the phone. This way I have full internet access on the phone and I can make phone calls and send SMS from it through my VOIP provider.

I use an ASUS N13 USB wifi adaptor for the hotspot and control it via a script linked to a desktop icon. This way, the internal wifi adaptor and the 3G data modem operate as normal via the Network Manager.

There is a bit of work setting up initially but, once done, it is reliable and simple to use - plug in the wifi adaptor, click the desktop icon. The phone connects automatically.

I hope these comments are helpful. Let me know if you need details of what I did.

Graeme

Yes, this would be the ideal solution! I would be highly interested in how to set this up, so please post more details. And it does not mess up the system otherwise when not in use?

I can read scripts, but I only know the basic theoretic principles of Networks & WLAN, but I have no clue how it is actually done (e.g. thus far, all my networking needs, such as setting up hostnames, firewalls settings, port forwarding, etc. were done through graphical user interfaces like YaST and NetworkManager).

No, it won’t affect your system if you do it the way I have done with a separate USB wifi adapter. I suggest you proceed as follows:

Firstly, plug in the wifi adapter you intend to use for the hotspot and determine its device name. It will probably be wlan1 but you can check with the following command.

sudo /usr/sbin/iwconfig

Note: the adapter doesn’t need to be configured with Yast.

Secondly, install the following 3 packages and whatever dependencies they pull in: [FONT=courier new]hostapd, dnsmasq and yad. hostapd allows you to create the hotspot (access point) and dnsmasq provides dhcp services on the sub-network associated with hotspot. Note that I chose 192.168.9.0 as my subnet to avoid clashing with other wifi providers. Use this, unless you have a real reason to change.

Then write configuration files for hostapd and dnsmasq as shown below.[/FONT]

grb@pippin:~> cat /etc/hostapd.conf
interface=wlan1
driver=nl80211
ssid=pippinAP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=pipperone
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP


grb@pippin:~> cat /etc/dnsmasq.conf 
#Name:Default
#Type:DNSMASQ
# disables dnsmasq reading any other files like /etc/resolv.conf for nameservers
no-resolv
# Interface to bind to
interface=wlan1
# Specify starting_range,end_range,lease_time
dhcp-range=192.168.9.2,192.168.9.10,12h
# dns addresses to send to the clients (NB: google servers)
server=8.8.8.8
server=8.8.4.4

Adjust the parameters in blue to suit your set-up. Check that both services start OK.

Thirdly, add the following script as ~/bin/hotspot.sh and make it executable.

grb@pippin:~> cat ./bin/hotspot.sh
#!/bin/bash
### configure
# setup for my ASUS N13 usb dongle
wifi_interface='wlan1'
adapter_id='0b05:1784'

# choose a subnet for the hotspot
# and an IP for the interface
subnet='192.168.9.0/24'
IP='192.168.9.1/24'

###########################################################
# try and locate the external interface automatically
ext_interface=$(ip route | grep default | cut -d' ' -f5)

user=$(whoami)
logname=$(logname)

### Return codes
# 0   - all OK
# 4   - user had insufficient privileges
# 150 - no adapter found

function check_user {
    ### check the script is being run with root privileges
    if  $user != 'root' ]; then
        kdialog --title='Hotspot' \
                --sorry "This script must be run with root privileges. \
                        
Please use 'sudo $(basename $0)'"
        exit 4
    fi
}

function check_adapter {
    ### check the wifi adapter is attached
    adapter_attached=$(lsusb | grep -ic $adapter_id)
    if  $adapter_attached -ne 1 ]; then
            su -l $logname -c "kdialog --display=:0.0 \
                                --title 'Hotspot' \
                                --sorry 'Cannot find the wifi adapter. 
                                        
Please connect the ASUS N-13 adapter and retry.'"
            exit 150
    fi
}

function stop_wifi_ap {
    ### stop services dhcpd server and hostapd
    systemctl stop hostapd
    systemctl stop dnsmasq
 
    ### disable IP forwarding
    sysctl -w net.ipv4.ip_forward=0 1> /dev/null
    iptables -t nat -D POSTROUTING -s $subnet -o $ext_interface -j MASQUERADE 2>/dev/null

    ### remove static IP address from the wifi interface
    ip address del $IP dev $wifi_interface 2> /dev/null
}

function start_wifi_ap { 
    ### give a static IP to the wifi interface
    ip link set dev $wifi_interface up
    ip address add $IP dev $wifi_interface
 
    ### enable IP forwarding
    iptables -t nat -A POSTROUTING -s $subnet -o $ext_interface -j MASQUERADE
    sysctl -w net.ipv4.ip_forward=1 1> /dev/null
 
    ### start services - dhcpd server and hostapd
    systemctl start hostapd
    systemctl start dnsmasq
}

function start_notifier {
    su -l $logname -c 'yad  --display=:0.0 \
                            --notification \
                            --image="/home/grb/scripts/hotspot/ap_up.png" \
                            --text "Click to close Hotspot"'
}

function stop_notifier {
    pkill yad
}

function stop {
    stop_wifi_ap
    stop_notifier
    exit
}

function start {
    check_adapter
    ### regard this as a forced start so kill any current processes first    
    stop_wifi_ap
    stop_notifier
    start_wifi_ap
    # causes script to block until icon is clicked
    start_notifier  
    stop
}

function auto {
    # Does nothing at this stage. See comment below
    :
}

### main
check_user

if  $# -lt 1 ]; then
    auto
else
    ### start/stop wifi access point
    case "$1" in
        start) start ;;
        stop)  stop  ;;
        *)     echo "Usage: $0 {start|stop}"
                exit 1       ;;
    esac
fi


############################################
# The intention was to have an 'auto' mode in addition to 
# 'start' and 'stop'.
# In auto mode, the notifier would be started as a sub-process (i.e. non blocking)
# and this script would listen for clicks on the notifier.

Again, adjust the parameters in blue.

The adapter_id can be found by running the command

grb@pippin:~> lsusb
Bus 001 Device 010: ID 0b05:1784 ASUSTek Computer, Inc. USB-N13 802.11n Network Adapter (rev. A1) [Ralink RT3072]
Bus 002 Device 008: ID 0402:7675 ALi Corp. 
Bus 003 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
....

Download a suitable icon and save it somewhere in your home directory. I used this icon http://www.veryicon.com/icon/64/System/Crystal%20Clear%20Actions/Irkick%20Flash.png and saved it as ~/scripts/ap_up.png. Adjust the script above to suit what you do.

At this stage you should be able to test that it all works by running the following command, in a terminal window (Konsole)

sudo hotspot.sh start

After entering the root password, the icon downloaded above should appear in the system tray and the light on the adapter should start to flash. All being well, you will be able to connect to the hotspot from your phone.

To bring the hotspot down, either click on the icon in the system tray or hit CTL-c in the terminal.

To make everything simple I added a desktop icon containing the following code

grb@pippin:~> cat ./Desktop/Hotspot.desktop
 [Desktop Entry]
Comment[en_US]=
Comment=
Comment[en_GB]=
Exec=sudo hotspot.sh start
GenericName[en_US]=
GenericName=
GenericName[en_GB]=
Icon=/home/grb/scripts/hotspot/ap_up.png
MimeType=
Name=Hotspot
Name[en_GB]=Hotspot
Path=
StartupNotify=false
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

Again, adjust the code to suit the icon name/location you chose.

Finally, to avoid having to provide a password each time, I used YAST to add the command [FONT=courier new]/home/USERNAME/bin/hotspot.sh start to the list of sudo rules. So now I simply click on the desktop icon to bring up the hotspot, and click on the tray icon to bring it down again.

I set my system up a while ago, so I hope I haven’t left out too much. Anyway let me know if you run into problems.

Graeme[/FONT]

Thanks for the nice instructions! :slight_smile: I will try that!

It does not work. :(I followed your steps, and I do see the icon in my tray, but my phone does not pick anything up. Unfortunately I have no other device to check whether the WLAN is there or not.I entered each command of the start_wifi_ap manually at the command line as root, but I did not get any error messages, except for iptables, which delivers:

iptables: No chain/target/match by that name.

I only changed the blue parts as instructed. Since my adapter for the AP is built-in, but not a usb device, I set adapter_id to the USB-WLAN device that connects to the internet.

wlan2: usb-adapter connected to internet with networkmanager
wlan0: built-in adapter that I want to use as access point

 # iwconfig wlan2     IEEE 802.11bgn  ESSID:"FRITZ!Box Fon WLAN 7270"  Nickname:"rtl_wifi"
          Mode:Managed  Frequency:2.412 GHz  Access Point: BC:05:43:94:D7:A9   
          Bit Rate:150 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Encryption key:****-****-****-****-****-****-****-****   Security mode:open
          Power Management:off
          Link Quality=53/100  Signal level=46/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0


lo        no wireless extensions.


em1       no wireless extensions.


wlan0     IEEE 802.11abgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=15 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off

So it is setup as access point, but why is the SSID not set or off?

Note that for now, I just did everything from a root console to avoid problems due to insufficient right, i.e. I have not set up sudo nor a menu entry yet.