Sharing Internet over Wifi Between Ubuntu and Android


I searched the internet alot for a solution to connect my PC’s internet to my android phone by creating a hotspot. In windows you have softwares like connectify to make your life easy, But in linux things are not so easy, and I was never a windows guy. I hated to logon to windows just to share the internet over wifi to my android phone. I spend many sleepless nights trying to configure hostapd in Ubuntu and make it connect to my Android. And finally after days of searching and experimenting that wonderful day of bliss occurred to me. I have combined information I received from various sites in this post.

Initially when I shared my problem in certain groups I got the advice to set up an ad-hoc wifi network in ubuntu, but Android 2.3 and lower versions do not detect ad-hoc network so that means you either have to root your phone and ad the necessary features or you have to go for an alternative way.

I was definite that there will be an alternative because in windows there are softwares like connectify that do the job, and in linux we have hostapd.

So let me get directly to the point.

Open a terminal and type

#lspci

It will show the network adapter available to you.

#lsmod

Shows that it uses the ath9k drivers (this information may help you to google for setup information specific to your machine.)

Also

#iwconfig

will give you the name of your wireless adapter like wlan0. Please notedown this name as you will be needing it later on.

Now its time to install the packages.

Either fire up the ubuntu software center and install hostapd and dhcp3-server, or fire up the terminal and type

#sudo apt-get install dhcp3-server
#sudo apt-get install hostapd

After this is done we have some serious editing to do so create a backup of all the files that are being edited.

In a terminal:

#sudo gedit /etc/hostapd/hostapd.conf

When the file is opened make the contents of the file as below.

interface=wlan0
driver=nl80211
ssid=geekpeek
channel=1
hw_mode=g
auth_algs=1
wpa=3
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

In this file we have declared that our wifi hotspot will be called geekpeek, it is using the adapter wlan0, and it should have a password which the connecting device has to enter. Save the file.

Next

#sudo gedit /etc/default/isc-dhcp-server

Edit the line that says

INTERFACES=”"

To

INTERFACES=”wlan0′′

Then edit

#sudo gedit /etc/dhcp/dhcpd.conf

Make sure the follow lines are Commented out ( put a hash “#” sign at the beginning of the line ) the

following lines:

# option definitions common to all supported networks...
#option domain-name “example.org”;
#option domain-name-servers ns1.example.org, ns2.example.org;
#default-lease-time 600;
#max-lease-time 7200;

Add the following lines to the file (copy and paste)

subnet 10.10.0.0 netmask 255.255.255.0 {
range 10.10.0.2 10.10.0.16;
option domain-name-servers 8.8.4.4, 8.8.8.8;
option routers 10.10.0.1;
}

Here we have specified the IP of our PC as 10.10.10.1 and 15 other PCs with addresses ranging from 10.10.0.2 to 10.10.0.16 can be connected to your PC.

Next edit

#sudo gedit /etc/network/interfaces

This is how my interfaces file looks:

To whatever is existing in the file add,

auto wlan0
iface wlan0 inet static
address 10.10.0.1
netmask 255.255.255.0

At this stage you can reboot. And hopefully any device / laptop should connect to your wifi hotspots network via the network manager applet. On these devices search for the network “geekpeek” and type in the password.

To Connect to the internet we have some more procedures to follow.

In a terminal type:

#ifconfig

It will display the adapter name being used for connecting to the internet. I was using the LAN so my adapter name was “eth0”, if you are using modems then it might be “ppp0”

First we have to allow ip forwarding:

#sudo gedit /etc/sysctl.conf

Make sure the following line is uncommented and reads

net.ipv4.ip_forward=1

Save and close gedit. (reboot)

After reboot: Connect to the Internet and In a terminal type:

#sudo gedit /etc/rc.local

add this line just before “exit 0′′

iptables -t nat -A POSTROUTING -s 10.10.0.0/16 -o ppp0 -j MASQUERADE

Then reboot the system.

The eth0 should be changed to whatever adapter you are using.

Once this is done you can make other laptops connect to to youe new wifi hotpsot by assingning them IP addreses in the range of 10.10.0.2 to 10.10.0.16

To make and Android detect your wifi hotspot.

Goto the wifi settings

press the options key ( the left most key)

Select Advanced>Use Static IP

Give the IP address as something between 10.10.0.2 to 10.10.0.16

Gateway the same IP as your PC, In this case it is 10.10.0.1

Netmask as 255.255.255.0

DNS1 as 8.8.4.4

DNS2 as 8.8.8.8

Once this much has been set your android phone should be able to connect to your wifi hotspot and you can access the internet connection on your PC through your phone.

Now when you turn on your PC go to terminal and type

#sudo hostapd /etc/hostapd/hostapd.conf

Now I have made an application in Qt4 that provides a GUI to turn on and off the wifi Hotspot, You can get it from the download section

References

http://thenewbieblog.wordpress.com/2012/05/01/wifi-hotspot-setup-on-ubuntu/

http://nims11.wordpress.com/2012/04/27/hostapd-the-linux-way-to-create-virtual-wifi-access-point/

Advertisement

54 thoughts on “Sharing Internet over Wifi Between Ubuntu and Android

  1. Jose manuel

    hello

    I have this error when typing in the terminal sudo hostapd / etc / hostapd / hostapd.conf

    Configuration file: / etc / hostapd / hostapd.conf
    nl80211: Failed to set interface wlan0 into AP mode
    nl80211 driver initialization failed.
    ELOOP: remaining socket: sock = 4 eloop_data = 0x9e41900 user_data = 0x9e41ea0 handler = 0x807c5e0
    ELOOP: remaining socket: sock = 6 eloop_data = 0x9e43ca8 user_data = (nil) handler = 0x8086770
     
    I followed the tutorial and I can connect from my android to my laptop with these 2 ip addresses off 10.10.0.1 (wlan0) and 192.168.0.100 (eth0) but can not get the internet to work on my android and not on my laptop

    Thanks for any help you can give me

    1. vineethkartha

      Dear friend, if my knowledge is correct, not all drivers support the Access Point (AP) Mode. so this might be one reason for the error

  2. Hi,
    I followed your tutorial, and everything has gone fine unless the last step.
    When I hit enter after writing

    sudo hostapd /etc/hostapd/hostapd.conf

    it gives me the error…

    Configuration file: /etc/hostapd/hostapd.conf
    nl80211: ‘nl80211’ generic netlink not found
    nl80211 driver initialization failed.

    And after that I checked my network manager applet and noticed that the wireless device is now showing not managed.

    my “iwconfig” returns

    lo no wireless extensions.

    eth1 IEEE 802.11bg ESSID:””
    Mode:Managed Frequency:2.412 GHz Access Point: Not-Associated
    Bit Rate:54 Mb/s Tx-Power:24 dBm
    Retry min limit:7 RTS thr:off Fragment thr:off
    Power Management:off
    Link Quality=5/5 Signal level=0 dBm Noise level=0 dBm
    Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
    Tx excessive retries:0 Invalid misc:0 Missed beacon:0

    eth0 no wireless extensions.

    and I changed wlan0 to eth1.

    I am using Ubuntu 12.04. Can you tell me what I have missed and what to check and what should I check on those first two commands.

  3. Gelo

    Thank you. Very good job.

    After following the described steps I can’t yet connect with my Samsung Galaxy S to Internet using my laptop’s WIFI.

    I’ve made only one change on the instructions:

    put eth0 instead ppp0 in the line

    iptables -t nat -A POSTROUTING -s 10.10.0.0/16 -o ppp0 -j MASQUERADE

    ————————————————————————————————
    $iwconfig

    wlan0 IEEE 802.11bgn ESSID:off/any
    Mode:Ad-Hoc Frequency:2.412 GHz Cell: Not-Associated
    Tx-Power=16 dBm
    Retry long limit:7 RTS thr:off Fragment thr:off
    Power Management:off

    lo no wireless extensions.

    eth0 no wireless extensions.
    ————————————————————————————————

    $lsmod

    Module Size Used by
    xt_state 12514 0
    ipt_REJECT 12485 0
    xt_tcpudp 12531 0
    iptable_filter 12706 0
    nf_nat_h323 12809 0
    nf_conntrack_h323 51810 1 nf_nat_h323
    nf_nat_pptp 12536 0
    nf_conntrack_pptp 13553 1 nf_nat_pptp
    nf_conntrack_proto_gre 13580 1 nf_conntrack_pptp
    nf_nat_proto_gre 12671 1 nf_nat_pptp
    nf_nat_tftp 12420 0
    nf_conntrack_tftp 12817 1 nf_nat_tftp
    nf_nat_sip 16945 0
    nf_conntrack_sip 24467 1 nf_nat_sip
    nf_nat_irc 12542 0
    nf_conntrack_irc 13112 1 nf_nat_irc
    nf_nat_ftp 12548 0
    nf_conntrack_ftp 13106 1 nf_nat_ftp
    ipt_MASQUERADE 12663 1
    iptable_nat 12977 1
    nf_nat 20316 9 nf_nat_h323,nf_nat_pptp,nf_nat_proto_gre,nf_nat_tftp,nf_nat_sip,nf_nat_irc,nf_nat_ftp,ipt_MASQUERADE,iptable_nat
    nf_conntrack_ipv4 14080 3 iptable_nat,nf_nat
    nf_conntrack 66307 18 xt_state,nf_nat_h323,nf_conntrack_h323,nf_nat_pptp,nf_conntrack_pptp,nf_conntrack_proto_gre,nf_nat_tftp,nf_conntrack_tftp,nf_nat_sip,nf_conntrack_sip,nf_nat_irc,nf_conntrack_irc,nf_nat_ftp,nf_conntrack_ftp,ipt_MASQUERADE,iptable_nat,nf_nat,nf_conntrack_ipv4
    nf_defrag_ipv4 12649 1 nf_conntrack_ipv4
    ip_tables 17791 2 iptable_filter,iptable_nat
    x_tables 21898 7 xt_state,ipt_REJECT,xt_tcpudp,iptable_filter,ipt_MASQUERADE,iptable_nat,ip_tables
    bnep 17707 2
    rfcomm 37276 0
    bluetooth 183228 10 bnep,rfcomm
    parport_pc 31968 0
    ppdev 12817 0
    snd_hda_codec_hdmi 31423 1
    snd_hda_codec_realtek 63493 1
    sp5100_tco 13417 0
    arc4 12473 2
    joydev 17161 0
    kvm_amd 54394 0
    kvm 357806 1 kvm_amd
    hid_generic 12445 0
    acer_wmi 27586 0
    sparse_keymap 13658 1 acer_wmi
    mxl5005s 45855 1
    snd_seq_midi 13132 0
    snd_rawmidi 25382 1 snd_seq_midi
    microcode 18209 0
    zl10353 17563 1
    ath9k 116557 0
    dvb_usb_ce6230 13133 0
    snd_hda_intel 32515 5
    dvb_usb 23606 1 dvb_usb_ce6230
    dvb_core 98654 1 dvb_usb
    snd_hda_codec 111547 3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel
    snd_hwdep 13272 1 snd_hda_codec
    rc_core 21266 1 dvb_usb
    uvcvideo 71277 0
    snd_seq_midi_event 14475 1 snd_seq_midi
    snd_seq 51255 2 snd_seq_midi,snd_seq_midi_event
    videobuf2_core 32070 1 uvcvideo
    mac80211 461161 1 ath9k
    videodev 95841 2 uvcvideo,videobuf2_core
    videobuf2_vmalloc 12756 1 uvcvideo
    ath9k_common 13783 1 ath9k
    snd_pcm 80163 3 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec
    psmouse 84843 0
    videobuf2_memops 13184 1 videobuf2_vmalloc
    usbhid 41702 0
    serio_raw 13031 0
    snd_seq_device 14137 3 snd_seq_midi,snd_rawmidi,snd_seq
    ath9k_hw 376155 2 ath9k,ath9k_common
    ath 19187 3 ath9k,ath9k_common,ath9k_hw
    cfg80211 175375 3 ath9k,mac80211,ath
    hid 82142 2 hid_generic,usbhid
    snd_timer 24411 2 snd_seq,snd_pcm
    i2c_piix4 12983 0
    k10temp 12958 0
    shpchp 32189 0
    snd 61991 20 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_rawmidi,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_seq,snd_pcm,snd_seq_device,snd_timer
    radeon 820734 4
    ttm 75534 1 radeon
    drm_kms_helper 47303 1 radeon
    drm 238768 6 radeon,ttm,drm_kms_helper
    soundcore 14599 1 snd
    i2c_algo_bit 13197 1 radeon
    snd_page_alloc 14036 2 snd_hda_intel,snd_pcm
    wmi 18590 1 acer_wmi
    mac_hid 13037 0
    video 18847 1 acer_wmi
    lp 13299 0
    parport 40753 3 parport_pc,ppdev,lp
    ums_realtek 17669 0
    uas 17556 0
    pata_atiixp 12999 0
    atl1e 32271 0
    usb_storage 39350 4 ums_realtek
    ——————————————————————————————————–

    I would appreciate some help.

    Thanks.

    1. vineethkartha

      Please do give me some time to look into the issue as I am caught up with other work, I will look into it as soon as I can.

  4. effepi

    My Pc with Ubuntu share my home internet connection with Android, but not if the connection is through a company lan with firewall at predefined gateway. Can you give me help? Thanks.

    1. vineethkartha

      It must have something to do with the firewall settings. If your company policies are Liberal enough ask your system admin 😉

      1. effepi

        I can surf the Internet on the PC through firewall using my credentials, but on the smartphone I don’t see the form to login.

      1. effepi

        The smartphone ping the IP of PC’s wlan interface, but not the ethernet (lan with static IP). Thanks

  5. abhishek khandal

    Hi vineeth
    I followed all your mentioned steps,my HTC explorer connects to the network made,but the Internet does not work,I can’t browse internet or anything which requires internet.
    I did everything mentioned in your post.I am using Ubuntu 12.04 LTS and my HTC explorer is on the stock ROM(not rooted).
    I router is Nokia Siemens residential router 1600
    I even have an inbuilt wireless adapter,but do not know which is it.
    Well,if this does not workout,I actually want to use WiFi to use internet on my mobile,but my router reboots whenever I connect my phone to it,possible b’coz of IP address conflicts,as it used to connect to my ubuntu via wired and even by wireless,both.
    Hoping for a quick reply.

    Thanks,
    Abhishek

    1. vineethkartha

      Hi abhishek,
      if your router has a wireless adapter than why do you want to share net via you PC why dont you connect your phone directly over wifi to your phone?

      1. Abhishek khandal

        I have the Nokia Siemens wifi router,I have a wireless adapter in my CPU cabinet,and I have this wireless option in my ubuntu which use this wireless adapter to connect to wifi by the nokia wifi router.
        The prob. is whenever I connect my phone to the the router to access internet,it reboots,possibly b’coz of IP conflicts,but wired and wireless connection simultaneously connected works on Ubuntu,there is no IP conflict problem on my PC,it is only on my HTC.
        I tried your method to make use of wireless adapter or router to make it as a hotspot to which I can connect my HTC to use internet,is there any other way possible where I do not have to root my phone?

        Thanks a ton

  6. James

    Not working for me. My phone gets stuck at the obtaining ip address.
    This is what is in terminal

    Using interface wlan0 with hwaddr 00:c0:ca:2f:a6:dd and ssid ‘geekpeek’
    wlan0: STA 28:98:7b:64:5e:5c IEEE 802.11: authenticated
    wlan0: STA 28:98:7b:64:5e:5c IEEE 802.11: associated (aid 1)
    wlan0: AP-STA-CONNECTED 28:98:7b:64:5e:5c
    wlan0: STA 28:98:7b:64:5e:5c RADIUS: starting accounting session 5176F1B0-00000000
    wlan0: STA 28:98:7b:64:5e:5c WPA: pairwise key handshake completed (RSN)
    wlan0: AP-STA-DISCONNECTED 28:98:7b:64:5e:5c
    wlan0: STA 28:98:7b:64:5e:5c IEEE 802.11: disassociated
    Could not set station 28:98:7b:64:5e:5c flags for kernel driver (errno=11).
    wlan0: STA 28:98:7b:64:5e:5c IEEE 802.11: authenticated
    wlan0: STA 28:98:7b:64:5e:5c IEEE 802.11: associated (aid 1)
    wlan0: AP-STA-CONNECTED 28:98:7b:64:5e:5c
    wlan0: STA 28:98:7b:64:5e:5c RADIUS: starting accounting session 5176F1B0-000000

  7. Ahmed Saleh

    Hello ,

    Really thank you very much for your tutorial , i was very happy to see the (GEEKPEEK) at my android device connected to my ubuntu. but unfortunately , there is no internet in my android device. could you please help me to share the internet.

    when i run the command to start sharing the wirless this line appears:
    Using interface wlan0 with hwaddr 00:c0:a8:ee:f4:48 and ssid ‘geekpeek’

    and after configuring my phone – it has beem connected succefully and these lines appears:
    wlan0: STA 30:85:a9:65:f0:bf IEEE 802.11: authenticated
    wlan0: STA 30:85:a9:65:f0:bf IEEE 802.11: associated (aid 1)
    AP-STA-CONNECTED 30:85:a9:65:f0:bf
    wlan0: STA 30:85:a9:65:f0:bf RADIUS: starting accounting session 51880568-00000000
    wlan0: STA 30:85:a9:65:f0:bf WPA: pairwise key handshake completed (RSN)

    your help is really appreciated.

    All Respect;

  8. Hello ,
    Please i need your help , I followed all the steps but it does not work : (
    Error message
    On my ubuntu :
    wlan0: STA a0:0b:ba:b3:f6:6f IEEE 802.11: authenticated
    wlan0: STA a0:0b:ba:b3:f6:6f IEEE 802.11: associated (aid 1)
    wlan0: STA a0:0b:ba:b3:f6:6f IEEE 802.11: deauthenticated due to local deauth request

    on my android :
    authentication problem

    what’s the problem ????
    Thank you

    1. vineethkartha

      I am sorry to hear this, But I have made a post that has worked for me and some of my friends. So this was not posted without trying it out. And as a solution to your problem please read through the comments of this post, I have mentioned on how to change the unmanaged status.

      1. sorry for being rude extremly sorry as recently in a week twice i have formated my system and wasted a much time to have all the things customized like shell extension app mozilla and chrome extensions , and lots more ……problem solved but still not able to share my wired connection ….thnking to go for a wifi router the better way its been a month searching for the thing to work out sorry once again…u r doing great job by sharing ur knowldege and promoting open soyrce

      1. yes did everything…But, I dont see any option for netmask on my android phone…other than that did everything from top to bottom 4-5 times now…

      2. vineethkartha

        Which phone are you using and which android version is it? I have done this with samsung Ace plus and Ubuntu 12.04,

    1. vineethkartha

      Can you explain what exactly the error is. Is problem existing only for play store alone? are you able to access internet from other apps or browser?

  9. navid

    nice…it was connected…..

    BUT THERE IS NO INTERNET ….

    PLZ HELP!

    my phone :sumsung y
    ubuntu 12.4
    i followed all of the steps…
    i’m new user in linux…
    does it have firewall that cause this problem(desable phon from access pc’s internet)???

  10. anand

    I tried u’r instructions on my ubuntu 12.04. The result:

    1.) It takes a almost 4-5 min to boot the system. It searches for network configuration (for about 1-2 minute), and fails and shows “booting in 60 more seconds” and then starts up.

    2.) After booting I don’t see my network manager. I always have to run “sudo service network-manager start” to start my network manager.

    3.) In Android after entering u’r recommended settings. I can see my laptop’s connection on the android device but it does not connect.

    Please Help.

  11. Christopher

    Thanks for the post…
    I get connection to the wifi from phone (using laptop), but I open a browser and tried to browse, and shows error : Check Your DNS settings and not accessing network

    I’m using phone: lg optimus l4 ii dual e445
    In this phone there is no option for Netmask, and it asks for “network prefix length” I entered “24”

    What should I do???

  12. al

    hello,
    I am getting error below, not sure what problem is. I am trying this on unbuntu.
    I have used the same usb wifi adapter and it works hostapd works fine on my pogoplug archlinux. Please help.

    Could not set station flags for kernel driver (errno=11)

  13. Akand Sitra

    Hi,

    I recently bought a Nexus 5, and it cannot detect the wifi hotspot that I created on ubuntu 12.04.

    I followed your steps till the first reboot. But, then I couldnt connect to the internet at all! As in, the internet connection was totally gone.

    I got scared, and redid all the changes, and now its working fine.

    Please help me in setting up the adhoc.

  14. Baris

    Hi Vineeth,

    Thank you very much for your instructions. This is the closest I have come to connecting my Android ICS device to the internet via shared connection over my Ubuntu 12.04LTS box.

    Closest… but no there yet…

    I can see the network, and connect to it, but I can’t share the internet connection. No internet on the phone…

    Still, thanks a lot. 🙂

    In case it would help, here is the relevant info:
    **********
    Samsung Galaxy Note II, stock rom, Android 4.1.2, Kernel 3.0.31-1071214

    ***********
    output of the hostapd :
    sudo hostapd /etc/hostapd/hostapd.conf
    Configuration file: /etc/hostapd/hostapd.conf
    Using interface wlan1 with hwaddr and ssid ‘BarisDesktopUbuntu’
    wlan1: STA IEEE 802.11: authenticated
    wlan1: STA IEEE 802.11: associated (aid 1)
    AP-STA-CONNECTED
    wlan1: STA RADIUS: starting accounting session 52A1AC54-00000000
    wlan1: STA WPA: pairwise key handshake completed (RSN)

    (I removed the MAC addresses)

    *********************

    1. Baris

      Hm. I had written MAC ADDRESS in brackets where the mac addresses are, but apparently, the scripts on this site thought it was a piece of code and did not display that. So, here’s the output of the sudo hostapd again:

      *********************
      sudo hostapd /etc/hostapd/hostapd.conf
      Configuration file: /etc/hostapd/hostapd.conf
      Using interface wlan1 with hwaddr PC_MAC_ADD and ssid ‘BarisDesktopUbuntu’
      wlan1: STA PHONE_MAC_ADD IEEE 802.11: authenticated
      wlan1: STA PHONE_MAC_ADD IEEE 802.11: associated (aid 1)
      AP-STA-CONNECTED PHONE_MAC_ADD
      wlan1: STA PHONE_MAC_ADD RADIUS: starting accounting session 52A1AC54-00000000
      wlan1: STA PHONE_MAC_ADD WPA: pairwise key handshake completed (RSN)

  15. Aw, this was an incredibly nice post. Spending some time and actual effort to generate a top notch article… but what can I say… I put things off a lot and never seem
    to get nearly anything done.

  16. jayendra

    hi Vineeth,
    i followed your tutorial and did all steps as mentioned. i want to share my pc internet which i am using through wifi and i want to create a wifi network to access internet in my android phone.
    my android phone is not able to directly connect to router because router is far away but my laptop is able to connect to router. so, currently, i am connected to internet in my laptop and now from laptop i want to connect to my phone. is it possible?
    thanks

    1. vineethkartha

      I believe this may not be possible because your laptop wifi adapter may not be able to act as access point while it is connected to another access point. Someone Please do correct me if I am wrong

  17. berry

    Hi,

    Thanks for the tutorial.
    Since the port forwarding, I’m not able to use VNC or SSH throught the both interfaces. I need a screen for change something on the hotspot.
    Do you know how I can have a remote access to the hotspot?

    Thanks in advance.

  18. Ali Khan

    I am able to connect my mobile to hotspot but I cant use ant wireless connection on my laptop because it is shown unmanaged.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s