Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, 1 May 2020

How to fix Ubuntu update error “waiting for unattended-upgr to exit”

Background

So I logged into my Ubuntu machine after a long time and I decided to update my installed software to the latest versions. But I see following screen and the update is stuck: “waiting for unattended-upgr to exit”



In this post, I will show you how to fix this issue.


Fixing “waiting for unattended-upgr to exit” issue


To begin, make sure all packages are in a clean state and correctly installed For this you can run:
  • sudo dpkg --configure -a
However, this fails for me with the following error



In fact, "sudo apt-get upgrade" also fails for me:

athakur:~$ sudo apt-get upgrade
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?


This could be due to multiple causes. Most probably some other internal update is running and using the lock. You can check this with the following commands:


  • ps -eaf | grep -i apt
  • lsof /var/lib/dpkg/lock-frontend 

Note: If you see a process like "apt.systemd.daily" using the lock, please wait for a few mins. This is auto-scheduler that updates your system. If you do not want this behavior you can go to "Software and Updates" and disable auto-updates:




Anyways, if you do not wish to wait you can always kill the process. Above commands - ps and lsof should give you PIDs corresponding to the process using the locks. You can kill them by running


  • sudo kill -9 PID
Replace PID with actual PID (Process ID) you see in the output of the above commands. Once done you can resume the software updates. You can also do
  • sudo apt-get upgrade
to upgrade your software.



If above does not work as well you can always delete the lock file (Not recommended)


  • sudo rm -rf /var/lib/dpkg/lock-frontend


and resume any update you might have. Please note we should not do this under ideal conditions. Lock files are meant to be present for special purposes. That being said, sometimes Softwares do go into an inconsistent state, and lock files have to be removed manually.

Once you kill the process or remove the lock file manually run following command to let dpkg fix itself:


  • sudo dpkg --configure -a
This is the same command we ran as the very 1ts step.



Related Links


Sunday, 26 May 2019

How to remove computer name from prompt in Ubuntu terminal

Background

If you are using Ubuntu terminal you must have noticed that terminal prompt looks like below -


Notice that prompt looks like -

athakur@athakur-Inspiron-7572:~/Documents/code$

The problem is this is fairly large and with more folders inside the root directory, there is much less space to type actual commands. In this post, I will show how to fix this.

How to remove computer name from prompt in Ubuntu terminal


To fix this you need to edit ~/.bashrc file. In this file there is a variable called PS1 that governs how your command prompt looks like. If you open ~/.bashrc you should see something like below -

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac



Let me explain a little bit about PS1 variable and above itself.

  • debian_chroot: This is applicable if you are running chroot operation (Your root directory is different than the default one). If you do not know chroot then do not worry, this is just and empty variable and can be ignored for now. 
  • XTERM: This is for a terminal emulator. If you are using XTERM PS1 under this would take effect.
  • color_prompt: This would be the default case for most. Your terminal would support color prompt. So PS1 under "$color_prompt" = yes is something you need to edit. 
If you have not heard above, don't worry about it just change the PS1 variable under the color_propmt if statement.

  • \u: expands to the current username
  • \h: expands to the current hostname
  • \w: expands to the current working directory
  • \$: expands to # for root and $ for all other users

Now that you know what each means and once you have figured out which PS1 to change you can simply remove "@\h" from it.



So change

if [ "$color_prompt" = yes ]; then

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '


to

if [ "$color_prompt" = yes ]; then

    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '



Once done save it, and run -

  • source ~/.bashrc
or simply open a new terminal and you should see the change.


Related Links


Customizing tray/taskbar date display in Ubuntu to show the actual date

Background

In my Ubuntu 18.04.2 LTS, I see that the date/time displayed on the desktop on the taskbar above is like - "Sun 12.00 PM".

 

You have to actually click on it to see the current date. In this post, I will show you how to add a date to the format that you see on the top. It would have been much easier if Ubuntu gave us this customization in settings itself, but unfortunately, that is not the case, at least not yet.

Customizing tray/taskbar date display in Ubuntu to show the actual date

There are two ways you can do this. I will show the most user-friendly method first.  But if you are more comfortable with Linux command line terminal and executing commands go to 2. advance section below.



1. User-Friendly - GUI Way

If you do not prefer using the command line, then you can install a GUI based tool. The tool name is "gnome-tweak-tool". You can search for "GNOME tweaks" in the Ubuntu software center. Install it.




Launch it and you should see the following screen. Go to Top Bar for date settings.





You can change the configuration as needed. You can see there are a bunch of other options here as well - like show battery percentage. All yours to play with :)


NOTE: If you do not prefer the Ubuntu software center but apt-get to install software, you can use the following command to install and launch the above application.

sudo apt install gnome-tweak-tool
gnome-tweaks  #  now launch it





2. Advanced - CLI Way

There are two commands that you need to know here -
  1. gsettings set org.gnome.desktop.interface clock-show-date true
    • makes the date appear
  2. gsettings set org.gnome.desktop.interface clock-show-seconds true
    • switches the seconds display on
You can similarly replace "set" by "get" to see the current values. By default, both values are set to false which is why you don't see a date or seconds.



Now let's go ahead and set these values to true and see the change of date/time format in the taskbar above.



And the result is -




I personally don't like seconds showing up. Just date works for me, so I have set accordingly. You can have settings that best suit you. You can do a similar thing with battery percentage as well with following command -

  • gsettings set org.gnome.desktop.interface show-battery-percentage true


Related Links


Saturday, 25 May 2019

How to fix hitting arrow keys adds characters in vi editor issue in Ubuntu

Background

I just purchased a new Laptop and the obvious next thing to do was add Ubuntu to it. There are some common problems that everyone faces when a new Ubuntu OS is installed and one such problem is -  hitting arrow keys adds characters in vi the editor. In this post, I will show you how to fix this issue.




Fixing hitting arrow keys adds characters in vi editor issue

To fix this issue, edit a file called vim.rc in your root folder. If it's not present create one. You can use the following command.

  • vi ~/.vimrc
Now add the following content to it -

  • set nocompatible
If backspace is not working add following content -

  • set backspace=2

And that's it. Save the file and you should be good to go. 



Another cleaner way is just to install vim -
  • sudo apt-get install vim
Now that the problem is resolved, let's try to understand the issue here. vi as an editor has normal and insert mode. When you open a file using vi you are in normal mode. Here you can go to any line, use any arrow keys and the behavior is as expected. Now when you type "i", you essentially go into insert mode. In this mode, vi does not expect you to go left, right, top, bottom using arrow keys. You can always do that by pressing "ESC" and going back to normal mode. Insert mode is just to add text to your file and that's the behavior of vi.

Hope this helps :)




Saturday, 13 April 2019

How to install Oracle Java 11 in Ubuntu

Background

In one of my previous posts I had covered how you can install Java 8 in your Ubuntu machine. In this post I will show how you can install Java 11 which is the latest SE version released (April 2019).

Oracle Java 11 is first LTS (Long term support) of Oracle Java release. 

NOTE: Oracle uses a commercial license now. You can download and use Oracle java for development and testing without any cost but to use it in production you need to pay a fee. 

If you do not want to pay you can always use Open JDK 11. From Java 11 forward, therefore, Oracle JDK builds and OpenJDK builds will be essentially identical. You can read more about this -

How to install Oracle Java 11 in Ubuntu

You can get Oracle Java 11 installer using linuxuprising PPA. To add this PPA execute following commands -

  • sudo add-apt-repository ppa:linuxuprising/java
  • sudo apt-get update



 To install the installer execute the following command -

  • sudo apt-get install oracle-java11-installer
You would need to accept the terms and conditions and continue with the installer. Once installed you can check the version of Java installed with following command -

  • java -version

To make Java 11 default you can install following package / execute following command -

  • sudo apt-get install oracle-java11-set-default
If you do not want this as default simply remove above pacakge  -
  • sudo apt-get remove oracle-java11-set-default


Related Links

Friday, 2 February 2018

How to show hidden files and folder in Ubuntu

Background

Some files are folders are hidden in Ubuntu. These are the ones that start with a "." in the beginning. Eg -
  • ~/.bashrc
  • ~/.vimrc etc
In this post, I will show you how you can make these files visible.

How to hide files and folder in Ubuntu?

The Files file manager gives you the ability to hide and unhide files at your discretion. When a file is hidden, it is not displayed by the file manager, but it is still there in its folder.

To hide a file, rename it with a "." at the beginning of its name. For example, to hide a file named example.txt, you should rename it to .example.txt.

You can hide folders in the same way that you can hide files. Hide a folder by placing a "." at the beginning of the folder’s name.

How to show hidden files and folder in Ubuntu CLI




To see the hidden files in the command line interface (CLI) you can just use -
  • ls -la
To not see the hidden files you can just use -
  • ls -l





 How to show hidden files and folder in Ubuntu Files explorer

To hidden files in the Files explorer, you can go to -
  • View -> Show hidden files
or you can simply press
  • Ctrl + H 

You can use the same shortcut or select the same setting again to toggle between showing and hiding hidden files in your Files explorer.

To make this permanent you can go to -
  • Edit -> Preferences
and turn on the setting to show hidden files.




Related Links



Sunday, 9 July 2017

How to display or hide line numbers in vi or vim text editor

Background

Line number come in very handy when you are working with any Text Editor. However if you take vi or vim editor then line numbers are hidden by default. In this post we will see how we can turn it on.


How to display or hide line numbers in vi or vim text editor

This post assumes you are a vim user and are aware of basic usage. Like for example when you need to save and quit data in vim you-
  • press escape followed by :wq
or if you want to exit without saving
  • :q!
 vi support a lot of commands to use and  options to be set by using colon (:)

To show line numbers in vi simple type below are pressing escape -
  • :set number OR
  • :set nu
 To hide it you can type -
  • :set nu! OR
  • set nonumber





This you need to do every time you launch into vi editor. To make it default edit your file at location ~/.vimrc and append following line at the end -

  • set number
NOTE : You can create this file if it does not exist already.

Now you will always get line numbers when you launch vim editor.




 Now that we have seen how to hide and display line numbers in vim editor lets see how we can jump to a particular line in vim -

How to jump to a particular line in vim

This is also fairly simple. Once you have launched vim you can simply move to any line number using following command -
  • : linenumber
Eg.
  • :6


 You can ever jump directly to your line number immediately as you open vim. To do that use following command while opening vim -
  • vi +linenumber filename
Eg.
  •  vi +6 test.txt
This should open your file and move to the linenumber you have provided.


General Info

vim provides a lot of configurable options to set. To see them all type following command -
  • :set all

To see everything that you have set so far you can type following command -
  • :set
For me it is as follows -



Related Links

Wednesday, 28 June 2017

How to access websites on your Mac that requires Internet Explorer

Background

There are certain websites that can be accessed from Internet Explorer only. This happens because of the websites compatibility with IE. But this will not work on your Mac laptop or Linux machine since you cannot run IE on it. At least not in traditional way - You can always install a software like Wine and then run your windows application in that simulated environment. But there is a much simpler way.

How to access websites on your Mac that requires Internet Explorer

I will take Safari browser in our Mac as an example. 
  • Open Safari browser and open preferences from menu bar at the top.
  • Once opened go to "Advanced Tab"
  • In "Advanced Tab" select the "Show Develop menu in menu bar" check box.

  • Once done you should be able to see "Develop" menu in menubar on top. 
  • Under "Develop" menu you can select "User Agent" and then select the user agent you want. For eg - "Internet Explorer 7"

  • Once you select that your IE compatible page should load fine.

 On other operating systems and browser  - Linux/Chrome/Firefox

Above approach was specific to Safari but the solution remains same - You need to change the user agent. So you have plugins to do so -


Similar you can find a similar plugin in chrome store. 

Manual way

    FIREFOX 4.0 :  In Firefox type in the URL Address: about:config. A webpage will appear saying a warning about the use of the Config. Click on the button about you being careful. In the search bar in the Config type agent and look for the variable general.useragent.override. Double click on it and overwrite the value it has with one of the following (For the default leave the value EMPTY):

    IE6 - Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    IE7 - Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
    IE8 - Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)


    CHROME :  Chrome has an about page to CHECK if you have changed your User Agent about: and other options like about:labs, about:memory, about:hang, about:plugins and many others that depending on your version they could be available or not. But for the question at hand this option is not yet in any of the about pages i have found. To have it manually in chrome you need to start chrome with the option user-agent. For example google-chrome --user-agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" which will open Chrome like it were IE6. The IE User Agents are from the Firefox option above.

    OPERA : In Opera type in the URL Address: about:config. A list will appear and a search address in the upper part of the list. Type in the search address user agent. The option for User Agen will appear below the search address. Click on it and depending on the Browser you want you have several options that change depending on version. For example:

    1 - Opera (this is the default user agent string used by Opera)
    2 - Mozilla (With the Opera String in it)
    3 - Internet Explorer (With the Opera String in it)
    4 - Mozilla (Without the Opera String in it. 100% Mozilla)
    5 - Internet Explorer (Without the Opera String in it. 100% IE)


    But this values could change so you would need to test each one to know what User Agent value it has.

Related Links

Saturday, 17 June 2017

Make a bootable flash drive from an ISO image on Ubuntu

Background

There are many times you download an ISO file from internet and want to install it on your machine. For eg. Windows or Ubuntu image. What you really wish is flash it iso image into a bootable drive like USB drive or a CD drive and install it from there on your machine.

 Make a bootable flash drive from an ISO image on Ubuntu

You have following options -
  1. Etcher- is a free and open-source image burner with support for Windows, OS X and GNU/Linux.
  2. Easy2Boot- Flexible and configurable USB drive multiboot solution which also supports UEFI booting.
  3. LiveUSB Install- is a free software for GNU/Linux and Windows. With LiveUSB Install you can effortlessly install various Linux distros.
  4. Multisystem- is an awesome tool created by LiveUSB.info that works similar to our Windows based MultiBootISOs USB creator.
  5. WinUSB - is a simple tool that enable you to create your own usb stick windows installer from an iso image or a real DVD.
  6. Unetbootin - UNetbootin allows you to create bootable Live USB drives for Ubuntu and other Linux distributions without burning a CD.
 Let's see some of the options -

Unetbootin

To install this run following command in your terminal -
  • sudo apt-get install unetbootin


Once installed you can  open the app. Now you can either select one of the given linux distros (those will get downloaded automatically) or  provide it an iso file from local machine.



WinUsb

To install winusb run following commands on your linux terminal -
  • sudo add-apt-repository ppa:colingille/freshlight
  • sudo apt-get update
  • sudo apt-get install winusb
Once installed you can open up the app select iso file and target usb and start creating bootable usb drive for windows.



NOTE : If you are looking for rufus then it is for Windows only. You cannot use it on your Linux machine.

Saturday, 17 December 2016

Introduction to crontab - scheduling tasks in Linux

Background

Cron is a daemon that runs in background and helps executing commands or set of commands at a predefined time. You can use to to schedule your commands at regular intervals. For example a batch job to pull all your unread mails.

Crontab


This will be per user. So each user can have their cron jobs and are stored in separate files called crontab files. To handle these the command used is crontab. The crontab file is often stored in

  • /var/spool/cron/crontabs/<user> (Unix/Slackware/*BSD),
  • /var/spool/cron/<user> (RedHat) or 
  • /var/cron/tabs/<user> (SuSE),
but might be kept elsewhere depending on what Un*x flavor you're running. Though these files are in var they are not supposed to be edited directly. As mentioned earlier you should use crontab commands.
  • crontab [ -u user ] file
  • crontab [ -u user ] { -l | -r | -e }
Options are as follows -
  • The -l option causes the current crontab to be displayed  on  standard output. 
  • The -r option causes the current crontab to be removed.
  • The  -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.  After you exit  from the editor, the modified crontab will be installed auto‐matically. If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.
For more details see
  • man crontab 


 As mentioned in the usage of edit crontab option you need to first set your editor -
  • aniket@aniket-Compaq-610:~$ export EDITOR=vi

 Creating a new cron job

Edit your crontab file with command -
  •  aniket@aniket-Compaq-610:~$ crontab -e
Now your crontab file should be opened in vi. Enter following command, press enter and save your file.

  • */1 * * * * date >> /tmp/log.txt 2>&1
This essentially puts current date every 2 mins in /tmp/log.txt file (combines standard output and error streams to stdout)

You should see following output -
crontab: installing new crontab

Post saving you can check the contents of your crontab file with command -
  • crontab -l



You can now check /tmp/log.txt file for output -



 
Finally you can delete your crontab file using command -
  • crontab -r


Format of crontab file command line

Syntax is as follows -
  • minute hour day-of-month month day-of-week command 
Possible values -
  • The asterisk (*) operator specifies all possible values for a field. e.g. every hour.
  • The comma (,) operator specifies a list of values. For eg every 2nd and 10th hour of the day.
  • The dash (-) operator specifies a range of values, Eg: "1-4", which is equivalent to "1,2,3,4".
  • The slash (/) operator, can be used to skip a given number of values. Eg "*/4" in the hour part of syntax would be equivalent to "0,4,8,12,16,20". "*" specifies 'every hour' but the "/4" means that skip 4 and try.
 Now lets revisit the command we used -
  • */1 * * * * date >> /tmp/log.txt 2>&1 
It means execute command every minute, every hour, every day of month, every year, each day of a week.

There are also special string that you can use instead of above syntax -

 

NOTES

  • As mentioned earlier each user has his/her crontab file. If you want to do operations that require root permissions you need to use root crontab file. To use that you can execute 
    • sudo crontab -e
  • In the /etc directory you will probably find some sub directories called  
    • 'cron.hourly', 
    • 'cron.daily', '
    • cron.weekly' and 
    • 'cron.monthly'.
      If you place a script into one of those directories it will be run either hourly, daily, weekly or monthly, depending on the name of the directory. 
  • Cron will email to the user all output of the commands it runs, to silence this, redirect the output to a log file or to /dev/null.
    • Eg. */1 * * * * date >> /dev/null 2>&1
  • For Cron permissions these two files play an important role:
    • /etc/cron.allow - If this file exists, it must contain your username for you to use cron jobs.
    • /etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, you must not be listed in the /etc/cron.deny file.

Related Links

Tuesday, 13 December 2016

Reset raspberry Pi forgotten password

Background

Well the background is bit embarrassing really :) To secure my raspberry Pi I set a very strong password and guess who forgot after a while :). Well I made my way back into the Pi but it was interesting. So will share this with you as well. Another constraint that I had is that I did not have my external keyboard with me so had to hack my way in. Will should you the same. However I will also list down a neater way in case you do have a keyboard.

NOTE : If you have not changed the password then it might still be raspberry. Try it. 



Without the keybaord

First and foremost you must know where passwords are stored. For Linux OS it is stored in /etc/shadow file. Ofcourse it's a one way hash and there is no way to reverse engineer it. But the file and its contents are important to us.

  • You should have another Linux distro with you. In this create a new user using command
    • useradd testuser
  • Now go to /etc/shadow file and you should see an entry corresponding to testuser. Something like -
    • testuser:!:16406:0:99999:7:::
  • Now remove the ! (or two) from between the first two colons, so it is testuser::16406.... This makes this a passwordless account.
  • Now change the current user to testuser using su command and change the password using passwd command -
    • su testuser
    • passwd
  • One you set the password you can go back to the /etc/shadow file and the line corresponding to testuser should now have a has password hash - something that most probably begins with $6
  • Copy this string and note it down.
  • Now take out your SD card from your Pi. Plug it into another Linux distro. Now on this open up the /etc/shadow file of pi and replace the hash that you might be having with the one you just noted above (You can do this for pi user).
  • And thats it you can use the same password you have for testuser to log into pi now. Try an ssh. You should be good.





Alternate route - the one with USB keyboard 

This is kind of non hacky route assuming you have a USB keyboard.
  • Remove SD card from your Pi. Plug it into some other Linux distro. 
  • In the boot partition you should find a file named cmdline.txt. 



  • Edit it to append 'init=/bin/sh' at the end. Before and after screenshots below -



  •  Save the file. Put the SD card back in Pi and boot up. Now Pi will boot up in single user mode.
  • When you get a cursor type in
    • passwd pi
  • and change the password of user pi. You can then do a normal startup using command -
    •  sync exec /sbin/init 
  • And that's it. Do not forget to remove the appended text that we put in cmdline.txt file. Remove it once you have successfully changed the password and your subsequent boots should be normal again.


Related Links

Saturday, 10 December 2016

How to enable developer mode on a Chromebook

Before you Start

Caution: Modifications you make to the system are not supported by Google, may cause hardware, software or security issues and may void warranty.

Couple of things to note -
  1. You will have to turn of verified boot which mean you or anyone can install a custom (non google verified) image.
  2. This will give you access to root console/shell.
  3. All your local data will be erased. Your device will be essentially reset. All your cloud data will still be present.
So to sum up your device will be less secure. Do this on your own risk.

NOTE : This article in intended only for developers and advanced users.

If you are concerned about anything mentioned above this would be a good point to turn back.

How to enable developer mode on a Chromebook

  1. Press and hold 'Esc' and refresh key (the button with a circular arrow - which is in place of your F3). Holding these buttons press your power key.

  2. You should now see a recovery mode screen with an exclamation icon. Press 'Ctrl' + 'D'. You will see screen displaying press Enter to turn of OS verification.



  3. Press Enter. You will see a transition screen displaying message of transition to developer mode.

  4. After some time your device should reboot and start in developer mode. At the end you should see your normal setup screen with language, keyboard layout etc.


How would you test that you are in developer mode?

  • First of all you should be able to access shell (console). Press Ctrl + Alt + T. Then in opened console type shell. You should be getting access to your normal linux command line.

  •  You can also check your chrome setting. You should be able to see all new advanced setting available. You can also see Android internal setting here. You can very well go ahead tab on Build number in About devices and turn on Android developer mode to access developer options. You can use this to turn on USB debugging and adb.





That's it you are now in developer mode. You have root access and can do more cool stuff. But remember  - With great power comes great responsibility :)

NOTE :  If Ctrl + D is not working you  can press Refresh+Power to boot in developer mode with ctrl+D or hit space and wipe clean.

Related Links


t> UA-39527780-1 back to top