Quickly access your configuration files on your server

It’s been a while since I last wrote a post on this blog, so I’ve decided to share a simple way to quickly access the configuration files of the numerous services you may be running on your server. It is indeed quite painful to frequently edit arbitrary deep configuration files (such as /etc/php5/apache2/php.ini) that are spread out in your file system and which you don’t remember the names. The trick I am using is a directory named cfg at the root of my server, in which I create symbolic links pointing to configuration files or directories containing them, with names that are easier to remember.

christophetd@vps:~$ ls -l /cfg
  ftp -> /etc/vsftpd.conf
  nginx -> /etc/nginx/sites-available/
  ssh -> /etc/ssh/sshd_config

Now, if I want to edit the configuration file of vsftpd (a great FTP server), I simply have to type

sudo vim /cfg/ftp . This will resolve the symbolic link and any change you make in the editor will be applied to the original file /etc/vsftpd.conf . I have found this method to be quite useful for readability and efficiency, but also for your sanity and peace of mind.

Setup

First, create your /cfg directory (you will need to create it as the super user, since the root folder belongs to him) by typing

sudo mkdir /cfg . Then, you can start placing symbolic links in the directory. The command to do this is:

sudo ln -s /path/to/the/config/file /cfg/alias

In the case of vsftpd, this would give:

sudo ln -s /etc/vsftpd.conf /cfg/ftp

Note that the first argument of the command line can also be a directory.

sudo ln -s /etc/nginx/sites-available /cfg/nginx

Then you can simply open

/cfg/nginx/mywebsite.com to edit /etc/nginx/sites-available/mywebsite.com

Even shorter If you are familiar with computer science people, you must they know how lazy they are. We can reduce even more the command that we now need to open a configuration file (

sudo vim /cfg/service ) by writing a very simple bash script. What does this do?
1. Checks if the $EDITOR environment variable is set. If it is, the configuration file will open in the corresponding editor. If not, it will open in vim.
2. Uses the selected editor to open the file /cfg/name, where name is the parameter you pass to the script when running it.

Note – you can set the $EDITOR environment variable by running export EDITOR=`which youreditor` , but the value will not be kept when your server reboots. To achieve this, you will need to append this command to your .bashrc file. Example if I wanted to use nano instead of vim:

echo "export EDITOR=`which nano`" > ~/.bashrc

The characters englobing the command

which nano  are backticks, not simple quotes.

With this script we will be able to simply type

cfg ftp to change the configuration of our ftp service! Just copy/paste the code above and paste it in the newly created file /usr/local/bin/cfg (you will need to create it as root). Then, make it executable by running  sudo chmod +x /usr/local/bin/cfg , and that’s it. You can also type these two commands to download the script to the proper location and make it executable:

sudo curl -L j.mp/cfg-script | sudo tee /usr/local/bin/cfg 
sudo chmod +x /usr/local/bin/cfg

Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *