Category: How-To

  • How to easily find the full path of a command in Ubuntu

     

    If you’re writing scripts or making cron jobs you will need to know the full path of the commands you’re using; rather than just being able to use “ls” you would have to use “/bin/ls” instead. You could use the find command here but there’s a quicker and more elegant way: which. Use it like so:

     

    which ls

     

    will return:

     

    /bin/ls

     

    Not everything will be in /bin, e.g.:

     

    which timeout

     

    will likely return:

     

    /usr/bin/timeout

     

    Simple but it will make your life quite a bit easier when writing scripts, particularly with new commands or command which you don’t use often.

  • Using rsync with a non-standard SSH port

     

    There are many reasons you may be using an SSH port other than 22; perhaps you changed it as a security measure, or perhaps you have multiple machines behind your firewall which you are port forwarding to and thus have to use other ports in addition to 22. Rsync is an extremely powerful file synchronization tool which by default uses SSH to connect your source and destination, thus if you have changed your SSH port you will need to tell rsync. This can be easily done with the e switch like so (using 2222 as the new SSH port as an example):

     

    rsync -e “ssh -p 2222” /path/to/source [email protected]:/path/to/destination

     

    As a practical example using the other options -avzP (our typical selection) your command might look like:

     

    rsync -avzP -e “ssh -p 2222” /home/user/myfile [email protected]:/home/user/

     

     

  • ESXi: Entering and exiting maintenance mode via command line

     

    Following on from yesterday’s post, here is how to enter or leave maintenance mode on an ESXi host via SSH:

     

    vim-cmd hostsvc/maintenance_mode_enter

     

    to go into maintenance mode – and to leave it:

     

    vim-cmd hostsvc/maintenance_mode_exit

     

    If you’re interested in other useful commands, you can see more hostsvc options by running:

     

    vim-cmd hostsvc

     

    This is a useful command to know as it is one of the critical steps in applying some patches to ESXi remotely.

  • ESXi: Determining maintanance mode status from the command line

     

    If you need to know if a host is in maintenance mode via the command line, SSH into your server and run the following:

     

    vim-cmd hostsvc/hostsummary | grep -i maintenance

     

    This will return the following line (in this example the host is NOT in maintenance mode):

     

     inMaintenanceMode = false,

    To see the entire host summary printout without filtering everything apart from maintenance, run:

     

    vim-cmd hostsvc/hostsummary

     

    …but you’ll soon see why grep is useful here!

  • How to find which version of ESXi you’re running from the command line?

     

    If you’re remotely logging in to a server to apply the latest patch but can’t remember whether you’re running 4, 4.1, 5.0 or 5.1 – and it can certainly happen when you’re managing quite a few of them remotely – there is a handy command to see which version and build number you’re actually using. After you’ve SSH’d in, run:

     

    vmware -v

     

    This will display output along the lines of the following:

     

    VMware ESXi 5.0.0 build-469512

     

    You can also use:

     

    vmware -l

     

    which doesn’t display the build number:

     

    VMware ESXi 5.0.0 GA

     

    Straightforward but very handy if you haven’t got the proper notes with you.

  • ESXi: Accessing datastores via web browser

     

    This is one that a lot of people don’t seem to be aware of – did you know you could access your ESXi server’s datastores via a browser? It’s a convenient way of grabbing copies of ISOs or patches stored on your server for burning or use elsewhere. It’s set up automatically with ESXi – simply enter in the IP address of your local ESXi server and you should see a page akin to the following:

     

    Click on the link on the right-hand side to view the datastores and you will be prompted for a login:

     

    Enter your login – usually the root login you created when you installed ESXi. From there you should be taken to a page where you can see a listing of all of your available datastores:

     

    From there you can browse the contents of the datastores and download files as you please! It can also be handy as a quick way of viewing log files.

  • Ubuntu: Clear terminal screen

     

    Sometimes you may wish to clear the terminal window, whether it be to hide what you’ve just done, clear some irrelevant/distracting output or any other reason. The best command to do this is simple:

     

    reset

     

    This completely clears the output shown in your terminal window but doesn’t log you out. If you want to keep your output in the buffer (i.e. so you can scroll back up to it) but still clear the terminal you can see you can use the following key combination:

     

    ctrl+L

     

    This pushes the output up above your prompt and puts the prompt at the top of your window.

     

     

  • How to change a user’s password in Mediawiki

    If you have a wiki you may need to change a user’s password from time to time; you can do this from the back end quite easily. First, access mysql:

     

    mysql -u root -p

     

    Log in using your root password. Next, list your databases:

     

    show databases;

     

    On our test system this shows all of our databases like so:

     

    mysql> show databases;
    +——————–+
    | Database
    +——————–+
    | information_schema
    | mysql
    | performance_schema
    | press
    | test
    | wiki
    +——————–+
    6 rows in set (0.10 sec)

    Select your wiki’s database:

     

    USE wiki;

     

    Replace “wiki” in the above with your own database’s name.

     

    UPDATE user SET user_password = MD5(CONCAT(user_id, ‘-‘, MD5(‘newpasswordgoeshere’))) WHERE user_name = ‘usernameofuser’;

     

    If this is successful you should get the following:

     

    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 0

     

    If something has gone wrong (e.g. a non-existent username) you will get the following instead:

     

    Query OK, 0 rows affected (0.03 sec)
    Rows matched: 0  Changed: 0  Warnings: 0

     

    All done! To leave mysql just type “exit”.

     

  • Ubuntu: How to create a file or folder using today’s date

    This is a useful little trick to use in your scripts – particularly for things like periodic backups.

     

    For a file:

     

    touch $(date +%F)

     

    creates the file 2012-11-18

     

    For a folder, let’s add the time after the date:

     

    mkdir $(date +%F-%H:%M)

     

    creates the folder 2012-11-18-09:00

     

    We can use this in a command, like so:

     

    tar -cf $(date +%F).tar /path/to/files/

     

    This creates the tarball (archive) file 2012-11-18.tar from the files in the /path/to/files/.

     

    To see the other options, type man date or visit the Ubuntu webpage on date here.

  • How To: Export all mysql databases for backup

    This is a handy command for anyone using multiple mysql databases – it produces a single file which you can easily back up to elsewhere.

     

    mysqldump -u root -p –all-databases > databasesBackup.sql

     

    Note the two hyphens before “all”. This command creates the file databasesBackup.sql which contains the contents of all of your databases. This file can be easily rsync’d or scp’d elsewhere to create an offsite backup of your site’s databases.