I first encountered this issue a long time ago and forgot to document it – so now I had to resolve it again.
I’m running Notepad ++ on Windows and using WinSCP to transfer bash scripts to Ubuntu Server. My script is saved as an Unix script file (*.sh, *.bsh) in Notepad++ and WinSCP is set to transfer files in binary mode.
When I run the script on Ubuntu Server I get the following error:
: No such file or directory
I could see the problem in Notepad++ and in nano on Ubuntu Server when I wanted to save the file – the file was formatted for Dos\Windows.
The fix is to change the formatting to UNIX. In Notepad ++ click Edit, EOL Conversion and UNIX and save the file.
On Ubuntu the hostname is stored in both the /etc/hosts and /etc/hostname files. There are several ways that we can change the hostname in these files.
1. Manually Edit the hostname
We can manually edit these files using a basic text editor like nano:
sudo nano /etc/hosts
sudo nano /etc/hostname
In /etc/hostname simply overwrite the existing hostname with a new one. In /etc/hosts you will find the hostname on the line beginning 127.0.0.1 – overwrite only the hostname with the new one, and then reboot.
2. Use sed to change the hostname
Another way to achieve the same goal is to use the sed command to replace the existing hostname with a new one.
For example, my Ubuntu Server has the default hostname of ‘ubuntu’.
Use the hostname command to check what your hostname is.
With sed we can look for our hostname (in /etc/hosts and /etc/hostname) and then replace it with the desired new-hostname:
sudo sed -i 's/ubuntu/new-hostname/g' /etc/hosts
sudo sed -i 's/ubuntu/new-hostname/g' /etc/hostname
3. Write a Bash Script
It’s always handy to have a script to do things – so here is a quick bash script that I put together that uses sed to change the hostname and then reboot:
#Assign existing hostname to $hostn
#Display existing hostname
echo "Existing hostname is $hostn"
#Ask for new hostname $newhost
echo "Enter new hostname: "
#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname
#display new hostname
echo "Your new hostname is $newhost"
#Press a key to reboot
read -s -n 1 -p "Press any key to reboot"
Installing software or making system changes from the command line can be a great time saver. Editing the repositories list though has always been something that I have done manually – either opening /etc/apt/sources.list with nano or making the necessary changes via the GUI.
Today, for example, I wanted to install Skype in Ubuntu 11.4 which meant enabling the “partner” repository. In /etc/apt/sources.list this would mean manually removing the # from the following line:
# deb http://archive.canonical.com/ubuntu natty partner
With a bit of searching though I found a solution that can be adapted to enable any of the existing entries in sources.list.