Uploaded by Derek Lewinson

How to change hostname in Kali Linux

advertisement
How to change hostname in Kali Linux?
December 12, 2013 How to, Kali Linux, Linux 12 Comments
Kali Linux is becoming popular and more and more users are using it to try out different things.
When installing kali, you get to choose a hostname, but in case you accepted the default
hostname (kali) and later want to change it, here’s a How to guide to change hostname in Kali
Linux.
Now just changing hostname to something else might not be enough. How about we change
hostname every time you boot your computer to a random one? That could be fun. It also helps
to avoid suspicion from System Admins in your network to see “kali” in their network. Like
BackTrack, SysAdmins doesn’t like Kali much (I mean why would they? Kali is designed to
poke and prod around the network to find vulnerability). Even if you’re using Kali as your
primary OS, it just raises eyebrows and you might get a visit from an over-conscious SysAdmin.
So we will discuss all possible ways, change hostname to something else permanently and
change hostname randomly in each boot.
Change hostname permanently and make it sticky – with reboot
Change hostname permanently and make it sticky – without rebooting
Change hostname randomly in each boot time.
Change hostname permanently – with reboot
As always, I’ve divided the options into multiple dot points.
Step 1: edit hostname file
Open hostname file from /etc directory and modify the name in there.
leafpad /etc/hostname
Let’s say we change the name from kali to aiur
Save the file.
Step 2: edit hosts file
Open hosts file from /etc directory and modify the name in there.
leafpad /etc/hosts
Change kali to aiur.
Save the file.
Step 3: reboot
Now reboot to reflect your changes
reboot
And you should see the new hostname coming up in terminal (i.e. root@aiur)
Change hostname permanently – without reboot
Don’t want to reboot? Here’s how
Follow step 1 and 2 from above
i.e.
Update /etc/hostname
Update /etc/hosts, so local address (es) resolves with the new system name.
Reload configuration files
Type in following 3 commands one at a time.
service hostname.sh start
service networking force-reload
service network-manager force-reload
Now force-reload networking service.
This will temporarily disconnect your system from the network (ssh usually resists short disconnection)
This might definitively disconnect your system from the network because networking might not restore
connections; please reboot, which is not lazy, but ensures that your setup is really correct
So we need to reload network-manager service as well.
This should reconnect networking again.
Depending on what other services you’re running, i.e. avahi, metasploit, postgresql, cups,
openSSH server, ssmtp etc. you might have to restart them all.
Now you must close your existing terminals to have the new hostname coming up at the top. See
following screenshot with highlighting.
First screenshot is after re-loading all the required services. Note that it’s still showing
root@kali in the top. uname -a or hostname shows correct info though.
If I close this terminal and open a new one, root@kali becomes root@aiur which is what we
want.
Change hostname randomly in each boot time
Following procedure will allow you to change your hostname randomly in each boot. That
hostname will stick until you reboot again.
Create a bash script
Create a script which will automate the procedure
In this terminal create a file.
touch newhostname
leafpad newhostname
Now, add the following lines to your newly created file:
#!/bin/bash
cp -n /etc/hosts{,.old}
idomainname=$(domainname -i)
fdomainname=$(domainname -f)
newhn=$(cat /dev/urandom | tr -dc 'A-Z' | head -c8)
echo $newhn > /etc/hostname
mv /etc/hosts /etc/hosts.old
echo "127.0.0.1 localhost" > /etc/hosts
echo "$idomainname $fdomainname $newhn" >> /etc/hosts
echo "# The following lines are desirable for IPv6 capable hosts" >> /etc/hosts
echo "::1 localhost ip6-localhost ip6-loopback" >> /etc/hosts
echo "ff02::1 ip6-allnodes" >> /etc/hosts
echo "ff02::2 ip6-allrouters" >> /etc/hosts
service hostname.sh stop
sleep 1
service hostname.sh start
service networking stop
sleep 1
service networking start
service network-manager stop
sleep 1
service network-manager start
xhost +$newhn
exit
Save it and exit leafpad.
Note: I’ve used only CAPS here for new hostname, 'A-Z'. You can also choose a mixure of
uppercase and lowercase ('A-Za-z') or numbers etc.
Also I’ve chosen 8 characters long hostname head -c8, you can change it to any length you like.
Move script to /usr/bin/ folder
We need to move this file to /usr/bin.
mv newhostname /usr/bin/newhostname
Make it executable
Use the following command to make newhostname file executable.
chmod +x /usr/bin/newhostname
Make it run at Startup:
Now that we have the script in right place and it’s executable, we need to add it your Startup
applications. This will allow your system to run it every time you reboot your machine and
generate a new hostname for you.
Follow the steps below:
Click on “Applications” –> “System Tools” –> “Preferences” –> “Startup Applications”
Click “Add”
Fill in:
Name: Random Host Name
Command: /usr/bin/newhostname
Comment: Start Kali with a random hostname each boot
Click Save
Close Windows
Reboot
Finally reboot your machine to load the script at start-up.
reboot
Enjoy your new hostnames. Here’s mine
Download