Setting up an ad-hoc network on boot
Solution 1
This blogspot entry has a guide for configuring a wireless network in ad-hoc mode. On the first machine, run these commands (fill in your own network information):
ifconfig wlan0 down
iwconfig wlan0 channel 4
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 essid 'fermilevel'
iwconfig wlan0 key 1234567890
ifconfig wlan0 192.168.1.1
On the second machine:
ifconfig wlan0 down
iwconfig wlan0 channel 4
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 essid 'fermilevel'
iwconfig wlan0 key 1234567890
ifconfig wlan0 192.168.1.2
The essid
and key
must be the same on both machines.
Solution 2
Creating ad-hoc Wi-Fi network used for sharing wired Internet connection using shell commands should be simple. Although I don't use Fedora setting up wireless networks should work similarly on all desktop Linux systems.
First, let's make sure that all components required to setup wireless network are available and install them if they are not:
$ command -v hostapd dnsmasq iptables ip
/usr/sbin/hostapd
/usr/sbin/dnsmasq
/usr/sbin/iptables
/sbin/ip
We'll put all configs in a dedicated ~/wireless-network-setup
directory so that we can find them easily:
mkdir ~/wireless-network-setup && cd ~/wireless-network-setup
As stated on https://w1.fi/hostapd/:
hostapd is a user space daemon for access point and authentication servers. It implements IEEE 802.11 access point management, IEEE 802.1X/WPA/WPA2/EAP Authenticators, RADIUS client, EAP server, and RADIUS authentication server. The current version supports Linux (Host AP, madwifi, mac80211-based drivers) and FreeBSD (net80211).
Put this in hostapd.conf
:
interface=wlan0
ssid=FREE-Wi-Fi
channel=1
auth_algs=1
wpa=3
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
This will create Wi-Fi network with FREE-Wi-Fi
SSID, WPA2 security
and password
passphrase.
dnsmasq
is DHCP server. It will assign IP addresses to Wi-Fi network
clients. Put this in dnsmasq.conf
:
interface=wlan0
dhcp-range=10.0.0.3,10.0.0.20,12h
DHCP server cannot provide IP address to itself so we have to set
wlan0
interface IP address manually:
sudo ip addr add 10.0.0.1/16 dev wlan0
sudo ip link set wlan0 up
iptables
is a firewall tool. We have to tell it to redirect traffic
to eth0
:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -P FORWARD ACCEPT
If your Internet-facing interface is not named eth0
modify its name
accordingly.
That's the whole configuration. Start hostapd
and dnsmasq
:
sudo hostapd -B hostapd.conf
sudo dnsmasq -C dnsmasq.conf
Now you should be able to connect to Free-Wi-Fi
network from other
devices and access the internet.
Notice that in Fedora both dnsmasq and hostapd probably come with customized init startup scripts that you can use instead of starting them manually like that.
Also notice that if you're using some specific hardware, for example
rtl8188eu
you will have to use forked hostapd
:
https://github.com/lwfinger/rtl8188eu.
Related videos on Youtube
Edward Alm
Updated on September 18, 2022Comments
-
Edward Alm over 1 year
I'm pretty new to Linux; this project is me trying to learn more. I've done login scripts before but not boot scripts, so that's one of my questions. My other question is how to get an ad-hoc network working with just terminal commands, so that I can make a boot script to do it. I'm running Fedora 18 off of a flash drive for now, but when I'm done configuring I'll be copying everything to an old laptop's hard drive. (The goal of this project is to not have to buy a better wireless router)
The most luck I've had setting up an ad-hoc was by using Gnome's "Use as Hotspot..." button in my wireless network settings, and then changing the name and WEP code (Can I use other encryption methods?) using iwconfig in the terminal, but I want it all done with terminal commands so that I can just run a script to set it all up. I think I can figure out how to use iptables to set up my port forwarding and junk myself, but first I need to get this computer able to share its internet connection wirelessly.
-
BatchyX almost 11 yearsWhat card are you using ? Have you checked if it supports AP mode ? (post the output of
iw phy
to figure out ?). If AP mode is not supported, maybe IBSS-RSN (which is basically WPA2 in ad-hoc mode) is supported. -
Edward Alm almost 11 yearsI ran "iw phy" on my newer laptop. I see a line under "Supported RX frame types:" that says "AP: 0x00 0x20 0x40 0xa0 0xb0 0xc0 0xd0" and immediately under is one that says "AP/VLAN: " and the same hex numbers. The laptop I'll eventually move this all onto is a Dell Latitude D630, but I don't have it with me at the moment. Its hinges are broken so I don't touch it if I don't have to.
-
BatchyX almost 11 yearsIf there is no
AP
underSupported interface types
, your card/driver does not support AP mode. What containsSupported RX frame types
is a bit irrelevant. Check if there is a mention ofIBSS-RSN
anyway, in that case, you may still do WPA2 in ad-hoc mode...
-
-
BatchyX almost 11 years
ifconfig
andiwconfig
are both deprecated, especially the latter which is completely obsolete. Useip
andiw
instead. Usingiw
has the direct benefit that it gives you the option to use 802.11n in theiw ibss join
command. -
Edward Alm almost 11 yearsThat blog post is trying to connect two Linux computers together; I'm just trying to set up one computer that shares its internet connection wirelessly with Windows PCs and my phone and junk. I can get the network to appear on my Windows PC by running one set of those commands on my laptop, but if I try to connect to it I get "Unidentified Network/No Internet Access".
-
Raza almost 11 yearsad-hoc is a peer-to-peer connection. you can setup each device to connect to the box that is sharing the net. I am not sure if ad-hoc will provide simultaneously connection to all of your devices.
-
Raza almost 11 yearsPlease make sure to setup bridge or routing in order to avoid "Unidentified Network/No Internet Access" error.
-
Yang Yu almost 5 years
ifconfig wlan0 up
is not need? -
Yang Yu almost 5 yearsWith
iwconfig wlan0 mode ad-hoc
, I cannot useifconfig wlan0 up
.