Category: Linux

Removing old kernels – Debian based systems



Linux systems that use the apt package management system do not remove old kernels when you upgrade to a newer one and you will end up with your boot directory increasing in size with every kernel upgrade. The only reason you would want keep your old kernels is if you upgrade the kernel and for some reason your system doesn’t come up. Once you boot on a new kernel checked your system and everything it is okay there is no reason to keep the old kernels it just eats up disk space.

Each distro names there kernels a little differently, so you will need to tweak the scripts below depending on your distro. I use two different distro’s Debian and Mint and will show the scripts I use. You need to remove both the kernel image and the header files packages.

Also – I shouldn’t need to say this, but “sudo apt update && sudo apt dist-upgrade” you want your system to be up to date before you continue.

Example 1 – Debian 10 (buster)

Step 1 –  List what kernel you are currently running.


$ uname -r
4.19.0-6-amd64

Step 2 – You will want to check the current installed kernel image. If you did a system upgrade and did not reboot the system you could have a running kernel different from the latest installed kernel package. Using the “apt show linux-image-amd64” compare the “Depends:” option with the running kernel version from the “uname -r” command, I’m on the same kernel “4.19.0-6-amd64”, so good to go. If the two are different you will need to reboot and check it again before you continue.

Note: each distro uses different naming conventions, so use the command in “Step 3” if you are unsure of the meta package name. The meta package will be the last part of the “uname -r” command (for  my Debian system 4.19.0-6-amd64) plus “linux-image”. So the package I want to show in this example is “linux-image-amd64”  


$ apt show linux-image-amd64
Package: linux-image-amd64
Version: 4.19+105+deb10u1
Priority: optional
Section: kernel
Source: linux-latest (105+deb10u1)
Maintainer: Debian Kernel Team <debian-kernel@lists.debian.org>
Installed-Size: 17.4 kB
Provides: linux-latest-modules-4.19.0-6-amd64
Depends: linux-image-4.19.0-6-amd64
Download-Size: 8,076 B
APT-Manual-Installed: yes
APT-Sources: http://deb.debian.org/debian buster/main amd64 Packages
Description: Linux for 64-bit PCs (meta-package)
 This package depends on the latest Linux kernel and modules for use on PCs
 with AMD64, Intel 64 or VIA Nano processors.

Step 3 –  List the installed kernels and header packages on your system;


$ dpkg --list | egrep -i  'linux-image|linux-headers'
ii  linux-headers-4.19.0-5-amd64                      4.19.37-5+deb10u2                    amd64        Header files for Linux 4.19.0-5-amd64 
ii  linux-headers-4.19.0-5-common                     4.19.37-5+deb10u2                    all          Common header files for Linux 4.19.0-5 
ii  linux-headers-4.19.0-6-amd64                      4.19.67-2+deb10u1                    amd64        Header files for Linux 4.19.0-6-amd64 
ii  linux-headers-4.19.0-6-common                     4.19.67-2+deb10u1                    all          Common header files for Linux 4.19.0-6 
ii  linux-headers-4.9.0-4-amd64                       4.9.65-3+deb9u1                      amd64        Header files for Linux 4.9.0-4-amd64 
ii  linux-headers-4.9.0-4-common                      4.9.65-3+deb9u1                      all          Common header files for Linux 4.9.0-4 
ii  linux-headers-amd64                               4.19+105+deb10u1                     amd64        Header files for Linux amd64 configuration (meta-package)
rc  linux-image-4.19.0-1-amd64                        4.19.12-1                            amd64        Linux 4.19 for 64-bit PCs (signed)
rc  linux-image-4.19.0-2-amd64                        4.19.16-1                            amd64        Linux 4.19 for 64-bit PCs (signed)
rc  linux-image-4.19.0-4-amd64                        4.19.28-2                            amd64        Linux 4.19 for 64-bit PCs (signed)
ii  linux-image-4.19.0-5-amd64                        4.19.37-5+deb10u2                    amd64        Linux 4.19 for 64-bit PCs (signed)
ii  linux-image-4.19.0-6-amd64                        4.19.67-2+deb10u1                    amd64        Linux 4.19 for 64-bit PCs (signed)
rc  linux-image-4.9.0-1-amd64                         4.9.6-3                              amd64        Linux 4.9 for 64-bit PCs (signed)
rc  linux-image-4.9.0-2-amd64                         4.9.18-1                             amd64        Linux 4.9 for 64-bit PCs (signed)
ii  linux-image-4.9.0-3-amd64                         4.9.30-2+deb9u5                      amd64        Linux 4.9 for 64-bit PCs
ii  linux-image-4.9.0-4-amd64                         4.9.65-3+deb9u1                      amd64        Linux 4.9 for 64-bit PCs
rc  linux-image-4.9.0-5-amd64                         4.9.65-3+deb9u2                      amd64        Linux 4.9 for 64-bit PCs
rc  linux-image-4.9.0-6-amd64                         4.9.88-1+deb9u1                      amd64        Linux 4.9 for 64-bit PCs
rc  linux-image-4.9.0-7-amd64                         4.9.110-3+deb9u2                     amd64        Linux 4.9 for 64-bit PCs
rc  linux-image-4.9.0-8-amd64                         4.9.130-2                            amd64        Linux 4.9 for 64-bit PCs
ii  linux-image-amd64                                 4.19+105+deb10u1                     amd64        Linux for 64-bit PCs (meta-package)

First column is the package flag

  • rc: It means package is in remove/deinstall state and only config file exists.
  • ii: It means package is in install state and it is 100% installed on the system.

Second column is what we are interested in, that is the list of all the header packages and kernel packages we have installed on the system. My list is pretty short, but you will be surprised on how many packages are on your system if you have been running it for any length of time.

There are five packages I want to leave on my Debian 10 system:

  • linux-image-amd64 (the kernel meta package – the package name you use to install with the “apt install” command)
  • linux-image-x.x.x-x-amd64 (the actual kernel image that the meta package installs in this case “linux-image-4.19.0-6-amd64”)
  • linux-headers-amd64 (the header meta package which installs two header packages – the package name you use to install with the “apt install” command)
  • linux-header-x.x.x-x-amd64 (the actual header files for the amd64 kernel that the meta package installs in this case “linux-headers-4.19.0-6-amd64”)
  • linux-header-x.x.x-x-common (the actual common header files that the meta package installs in this case “linux-headers-4.19.0-6-common”)

Step 4 –  run the following commands and scripts to remove all of your old kernels.

Set variable “v” to the kernel version we are running on and list it :


$ v="$(uname -r | awk -F '-amd64' '{ print $1}')"       
$ echo "$v"
4.19.0-6

Set variable “i” to the packages we want to keep and list them;


$ i="linux-headers-amd64|linux-image-amd64|linux-headers-${v}-amd64|linux-headers-${v}-common|linux-image-$(uname -r)"
$ echo "$i"
linux-headers-amd64|linux-image-amd64|linux-headers-4.19.0-6-amd64|linux-headers-4.19.0-6-common|linux-image-4.19.0-6-amd64

Test to see what we are going to remove with our script;


root@jupiter:~# dpkg --list | egrep -i  'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i"
linux-headers-4.19.0-5-amd64     
linux-headers-4.19.0-5-common    
linux-headers-4.9.0-4-amd64      
linux-headers-4.9.0-4-common     
linux-image-4.19.0-1-amd64       
linux-image-4.19.0-2-amd64       
linux-image-4.19.0-4-amd64       
linux-image-4.19.0-5-amd64       
linux-image-4.9.0-1-amd64        
linux-image-4.9.0-2-amd64        
linux-image-4.9.0-3-amd64        
linux-image-4.9.0-4-amd64        
linux-image-4.9.0-5-amd64        
linux-image-4.9.0-6-amd64        
linux-image-4.9.0-7-amd64        
linux-image-4.9.0-8-amd64        

Remove the packages;


root@jupiter:~# apt-get --purge remove $(dpkg --list | egrep -i  'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i")
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  linux-kbuild-4.9
Use 'apt autoremove' to remove it.
The following packages will be REMOVED:
  linux-headers-4.19.0-5-amd64* linux-headers-4.19.0-5-common* linux-headers-4.9.0-4-amd64* linux-headers-4.9.0-4-common* linux-image-4.19.0-1-amd64* 
linux-image-4.19.0-2-amd64* linux-image-4.19.0-4-amd64* linux-image-4.19.0-5-amd64* linux-image-4.9.0-1-amd64* linux-image-4.9.0-2-amd64* 
linux-image-4.9.0-3-amd64* linux-image-4.9.0-4-amd64*  linux-image-4.9.0-5-amd64* linux-image-4.9.0-6-amd64* linux-image-4.9.0-7-amd64* 
linux-image-4.9.0-8-amd64* 
0 upgraded, 0 newly installed, 16 to remove and 0 not upgraded. 
After this operation, 646 MB disk space will be freed. 
Do you want to continue? [Y/n] 
 

Looking at the output from the script notice that my installed kernel is omitted 4.19.0-6-amd64. By doing this I freed up 646M of disk space. Hit enter to continue and it will remove all of the old kernels and header files.


dpkg --list | egrep -i  'linux-image|linux-headers'
ii  linux-headers-4.19.0-6-amd64                      4.19.67-2+deb10u1                    amd64        Header files for Linux 4.19.0-6-amd64
ii  linux-headers-4.19.0-6-common                     4.19.67-2+deb10u1                    all          Common header files for Linux 4.19.0-6
ii  linux-headers-amd64                               4.19+105+deb10u1                     amd64        Header files for Linux amd64 configuration (meta-package)
ii  linux-image-4.19.0-6-amd64                        4.19.67-2+deb10u1                    amd64        Linux 4.19 for 64-bit PCs (signed)
ii  linux-image-amd64                                 4.19+105+deb10u1                     amd64        Linux for 64-bit PCs (meta-package)

Much better only have the files for the current kernel……..

To be safe lets make sure we didn’t remove something that we shouldn’t have. This will just reinstall the most recent kernel and headers packages


sudo apt --reinstall install linux-image-amd64 linux-headers-amd64 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 2 reinstalled, 0 to remove and 0 not upgraded.
Need to get 0 B/14.8 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 335095 files and directories currently installed.)
Preparing to unpack .../linux-headers-amd64_4.19+105+deb10u1_amd64.deb ...
Unpacking linux-headers-amd64 (4.19+105+deb10u1) over (4.19+105+deb10u1) ...
Preparing to unpack .../linux-image-amd64_4.19+105+deb10u1_amd64.deb ...
Unpacking linux-image-amd64 (4.19+105+deb10u1) over (4.19+105+deb10u1) ...
Setting up linux-headers-amd64 (4.19+105+deb10u1) ...
Setting up linux-image-amd64 (4.19+105+deb10u1) ...

One last step is to upgrade Grub so it knows we removed all the old kernels.


$ sudo update-grub
Generating grub configuration file ...
Found background image: /usr/share/images/desktop-base/desktop-grub.png
Found linux image: /boot/vmlinuz-4.19.0-6-amd64
Found initrd image: /boot/initrd.img-4.19.0-6-amd64
done

Example 2 – Mint 19.2 Cinnamon


$ uname -r
4.15.0-65-generic

List what packages are installed…..may more on this system.


dpkg --list | egrep -i  'linux-image|linux-headers'
ii  linux-headers-4.15.0-36                      4.15.0-36.39                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-36-generic              4.15.0-36.39                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-39                      4.15.0-39.42                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-39-generic              4.15.0-39.42                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-43                      4.15.0-43.46                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-43-generic              4.15.0-43.46                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-46                      4.15.0-46.49                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-46-generic              4.15.0-46.49                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-55                      4.15.0-55.60                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-55-generic              4.15.0-55.60                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-64                      4.15.0-64.73                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-64-generic              4.15.0-64.73                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-65                      4.15.0-65.74                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-65-generic              4.15.0-65.74                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-101                      4.4.0-101.124                                       all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-101-generic              4.4.0-101.124                                       amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-104                      4.4.0-104.127                                       all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-104-generic              4.4.0-104.127                                       amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-109                      4.4.0-109.132                                       all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-109-generic              4.4.0-109.132                                       amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-116                      4.4.0-116.140                                       all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-116-generic              4.4.0-116.140                                       amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-124                      4.4.0-124.148                                       all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-124-generic              4.4.0-124.148                                       amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-21                       4.4.0-21.37                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-21-generic               4.4.0-21.37                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-57                       4.4.0-57.78                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-57-generic               4.4.0-57.78                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-59                       4.4.0-59.80                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-59-generic               4.4.0-59.80                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-62                       4.4.0-62.83                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-62-generic               4.4.0-62.83                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-64                       4.4.0-64.85                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-64-generic               4.4.0-64.85                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-66                       4.4.0-66.87                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-66-generic               4.4.0-66.87                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-72                       4.4.0-72.93                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-72-generic               4.4.0-72.93                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-75                       4.4.0-75.96                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-75-generic               4.4.0-75.96                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-77                       4.4.0-77.98                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-77-generic               4.4.0-77.98                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-78                       4.4.0-78.99                                         all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-78-generic               4.4.0-78.99                                         amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-79                       4.4.0-79.100                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-79-generic               4.4.0-79.100                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-81                       4.4.0-81.104                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-81-generic               4.4.0-81.104                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-83                       4.4.0-83.106                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-83-generic               4.4.0-83.106                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-87                       4.4.0-87.110                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-87-generic               4.4.0-87.110                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-91                       4.4.0-91.114                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-91-generic               4.4.0-91.114                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-92                       4.4.0-92.115                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-92-generic               4.4.0-92.115                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-93                       4.4.0-93.116                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-93-generic               4.4.0-93.116                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-96                       4.4.0-96.119                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-96-generic               4.4.0-96.119                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-97                       4.4.0-97.120                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-97-generic               4.4.0-97.120                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-4.4.0-98                       4.4.0-98.121                                        all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-98-generic               4.4.0-98.121                                        amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-generic                        4.15.0.65.67                                        amd64        Generic Linux kernel headers
ii  linux-image-4.15.0-65-generic                4.15.0-65.74                                        amd64        Signed kernel image generic
ii  linux-image-generic                          4.15.0.65.67                                        amd64        Generic Linux kernel image
ii  linux-image-4.15.0-65-generic                4.15.0-65.74                                        amd64        Signed kernel image generic
rc  linux-image-extra-4.10.0-38-generic          4.10.0-38.42~16.04.1                                amd64        Linux kernel extra modules for version 4.10.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-101-generic          4.4.0-101.124                                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-104-generic          4.4.0-104.127                                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-109-generic          4.4.0-109.132                                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-116-generic          4.4.0-116.140                                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-124-generic          4.4.0-124.148                                       amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-21-generic           4.4.0-21.37                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-57-generic           4.4.0-57.78                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-59-generic           4.4.0-59.80                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-62-generic           4.4.0-62.83                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-64-generic           4.4.0-64.85                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-66-generic           4.4.0-66.87                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-72-generic           4.4.0-72.93                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-75-generic           4.4.0-75.96                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-77-generic           4.4.0-77.98                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-78-generic           4.4.0-78.99                                         amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-79-generic           4.4.0-79.100                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-81-generic           4.4.0-81.104                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-83-generic           4.4.0-83.106                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-87-generic           4.4.0-87.110                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-91-generic           4.4.0-91.114                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-92-generic           4.4.0-92.115                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-93-generic           4.4.0-93.116                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-96-generic           4.4.0-96.119                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-97-generic           4.4.0-97.120                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
rc  linux-image-extra-4.4.0-98-generic           4.4.0-98.121                                        amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-generic                          4.15.0.65.67                                        amd64        Generic Linux kernel image
ii  linux-image-unsigned-4.15.0-64-generic       4.15.0-64.73                                        amd64        Linux kernel image for version 4.15.0 on 64 bit x86 SMP

This is the only change from the Debian example above, here are five packages I want to leave on my Mint 19.2 system:

  • linux-image-generic(the kernel meta package – the package name you use to install with the “apt install” command)
  • linux-image-x.x.x-x-generic (the actual kernel image that the meta package installs in this case “linux-image-4.15.0-65-generic”)
  • linux-headers-generic (the header meta package which installs two header packages – the package name you use to install with the “apt install” command)
  • linux-header-x.x.x-x-generic (the actual header files for the amd64 kernel that the meta package installs in this case “linux-headers-4.15.0-65-generic”)
  • linux-header-x.x.x-x (the actual common header files that the meta package installs in this case “linux-headers-4.15.0-65”)

Set variable “v” to the kernel version we are running on and list it. Then set variable “i” to the five packages I what we want to keep.


$ v="$(uname -r | awk -F '-generic' '{ print $1}')"
$ echo "$v"
4.15.0-65
$ i="linux-headers-generic|linux-image-generic|linux-headers-${v}-generic|linux-headers-${v}|linux-image-$(uname -r)"
$ echo "$i"
linux-headers-generic|linux-image-generic|linux-headers-4.15.0-65-generic|linux-headers-4.15.0-65|linux-image-4.15.0-65-generic

Print out what packages we will be removing…………..


$ dpkg --list | egrep -i  'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i"
linux-headers-4.15.0-36
linux-headers-4.15.0-36-generic
linux-headers-4.15.0-39
linux-headers-4.15.0-39-generic
linux-headers-4.15.0-43
linux-headers-4.15.0-43-generic
linux-headers-4.15.0-46
linux-headers-4.15.0-46-generic
linux-headers-4.15.0-55
linux-headers-4.15.0-55-generic
linux-headers-4.15.0-64
linux-headers-4.15.0-64-generic
linux-headers-4.4.0-101
linux-headers-4.4.0-101-generic
linux-headers-4.4.0-104
linux-headers-4.4.0-104-generic
linux-headers-4.4.0-109
linux-headers-4.4.0-109-generic
linux-headers-4.4.0-116
linux-headers-4.4.0-116-generic
linux-headers-4.4.0-124
linux-headers-4.4.0-124-generic
linux-headers-4.4.0-21
linux-headers-4.4.0-21-generic
linux-headers-4.4.0-57
linux-headers-4.4.0-57-generic
linux-headers-4.4.0-59
linux-headers-4.4.0-59-generic
linux-headers-4.4.0-62
linux-headers-4.4.0-62-generic
linux-headers-4.4.0-64
linux-headers-4.4.0-64-generic
linux-headers-4.4.0-66
linux-headers-4.4.0-66-generic
linux-headers-4.4.0-72
linux-headers-4.4.0-72-generic
linux-headers-4.4.0-75
linux-headers-4.4.0-75-generic
linux-headers-4.4.0-77
linux-headers-4.4.0-77-generic
linux-headers-4.4.0-78
linux-headers-4.4.0-78-generic
linux-headers-4.4.0-79
linux-headers-4.4.0-79-generic
linux-headers-4.4.0-81
linux-headers-4.4.0-81-generic
linux-headers-4.4.0-83
linux-headers-4.4.0-83-generic
linux-headers-4.4.0-87
linux-headers-4.4.0-87-generic
linux-headers-4.4.0-91
linux-headers-4.4.0-91-generic
linux-headers-4.4.0-92
linux-headers-4.4.0-92-generic
linux-headers-4.4.0-93
linux-headers-4.4.0-93-generic
linux-headers-4.4.0-96
linux-headers-4.4.0-96-generic
linux-headers-4.4.0-97
linux-headers-4.4.0-97-generic
linux-headers-4.4.0-98
linux-headers-4.4.0-98-generic
linux-image-4.15.0-36-generic
linux-image-4.15.0-39-generic
linux-image-4.15.0-43-generic
linux-image-4.15.0-46-generic
linux-image-4.15.0-55-generic
linux-image-4.15.0-64-generic
linux-modules-extra-4.15.0-36-generic
linux-modules-extra-4.15.0-39-generic
linux-modules-extra-4.15.0-43-generic
linux-modules-extra-4.15.0-46-generic
linux-modules-extra-4.15.0-55-generic
linux-modules-extra-4.15.0-64-generic*

Now we remove the packages.


sudo apt-get --purge remove $(dpkg --list | egrep -i  'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i")
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-4.15.0-36* linux-headers-4.15.0-36-generic* linux-headers-4.15.0-39* linux-headers-4.15.0-39-generic* linux-headers-4.15.0-43* linux-headers-4.15.0-43-generic* linux-headers-4.15.0-46* linux-headers-4.15.0-46-generic*
  linux-headers-4.15.0-55* linux-headers-4.15.0-55-generic* linux-headers-4.15.0-64* linux-headers-4.15.0-64-generic* linux-headers-4.4.0-101* linux-headers-4.4.0-101-generic* linux-headers-4.4.0-104* linux-headers-4.4.0-104-generic*
  linux-headers-4.4.0-109* linux-headers-4.4.0-109-generic* linux-headers-4.4.0-116* linux-headers-4.4.0-116-generic* linux-headers-4.4.0-124* linux-headers-4.4.0-124-generic* linux-headers-4.4.0-21* linux-headers-4.4.0-21-generic*
  linux-headers-4.4.0-57* linux-headers-4.4.0-57-generic* linux-headers-4.4.0-59* linux-headers-4.4.0-59-generic* linux-headers-4.4.0-62* linux-headers-4.4.0-62-generic* linux-headers-4.4.0-64* linux-headers-4.4.0-64-generic*
  linux-headers-4.4.0-66* linux-headers-4.4.0-66-generic* linux-headers-4.4.0-72* linux-headers-4.4.0-72-generic* linux-headers-4.4.0-75* linux-headers-4.4.0-75-generic* linux-headers-4.4.0-77* linux-headers-4.4.0-77-generic*
  linux-headers-4.4.0-78* linux-headers-4.4.0-78-generic* linux-headers-4.4.0-79* linux-headers-4.4.0-79-generic* linux-headers-4.4.0-81* linux-headers-4.4.0-81-generic* linux-headers-4.4.0-83* linux-headers-4.4.0-83-generic*
  linux-headers-4.4.0-87* linux-headers-4.4.0-87-generic* linux-headers-4.4.0-91* linux-headers-4.4.0-91-generic* linux-headers-4.4.0-92* linux-headers-4.4.0-92-generic* linux-headers-4.4.0-93* linux-headers-4.4.0-93-generic*
  linux-headers-4.4.0-96* linux-headers-4.4.0-96-generic* linux-headers-4.4.0-97* linux-headers-4.4.0-97-generic* linux-headers-4.4.0-98* linux-headers-4.4.0-98-generic*
  linux-image-4.15.0-36-generic* linux-image-4.15.0-39-generic* linux-image-4.15.0-43-generic* linux-image-4.15.0-46-generic*
  linux-image-4.15.0-55-generic* linux-image-4.15.0-64-generic* linux-modules-extra-4.15.0-36-generic*
  linux-modules-extra-4.15.0-39-generic* linux-modules-extra-4.15.0-43-generic* linux-modules-extra-4.15.0-46-generic*
  linux-modules-extra-4.15.0-55-generic* linux-modules-extra-4.15.0-64-generic*

0 upgraded, 0 newly installed, 62 to remove and 17 not upgraded.
After this operation, 3,488 MB disk space will be freed.
Do you want to continue? [Y/n] 

That freed up over 3G of space!!!!

Now lets reinstall the kernel and header packages to make sure we didn’t break anything.


$ sudo apt-get --reinstall install linux-image-generic linux-headers-generic
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 2 reinstalled, 0 to remove and 17 not upgraded.
Need to get 0 B/4,736 B of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 301089 files and directories currently installed.)
Preparing to unpack .../linux-headers-generic_4.15.0.65.67_amd64.deb ...
Unpacking linux-headers-generic (4.15.0.65.67) over (4.15.0.65.67) ...
Preparing to unpack .../linux-image-generic_4.15.0.65.67_amd64.deb ...
Unpacking linux-image-generic (4.15.0.65.67) over (4.15.0.65.67) ...
Setting up linux-image-generic (4.15.0.65.67) ...
Setting up linux-headers-generic (4.15.0.65.67) .

Show what kernel and header packages are installed


dpkg --list | egrep -i  'linux-image|linux-headers'
ii  linux-headers-4.15.0-65                      4.15.0-65.74                                        all          Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-65-generic              4.15.0-65.74                                        amd64        Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-generic                        4.15.0.65.67                                        amd64        Generic Linux kernel headers
ii  linux-image-4.15.0-65-generic                4.15.0-65.74                                        amd64        Signed kernel image generic
ii  linux-image-generic                          4.15.0.65.67                                        amd64        Generic Linux kernel image

Now we update Grub to have only the current kernel

 
$ sudo update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/50_linuxmint.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-65-generic
Found initrd image: /boot/initrd.img-4.15.0-65-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done

Bill


Coping Photos from Camera to Linux with Gphoto2

 

Install gphoto2

sudo apt-get install gphoto2

Example from my camera:

===============================================

To make sure I can see camera

billf@mars $ gphoto2 –auto-detect

Model Port

———————————————————-

USB PTP Class Camera usb:004,098

 

billf@mars ~/Downloads $ gphoto2 –list-ports
Devices found: 2
Path Description
————————————————————–
ptpip:PTP/IP Connection
usb:003,012  Universal Serial Bus

Model Port
———————————————————-
USB PTP Class Camera  usb:003,012
billf@mars ~/Downloads $ gphoto2 –summary
Camera summary:
Manufacturer: Nikon Corporation
Model: L30
Version: COOLPIX L30 V1.0
Vendor Extension ID: 0xa (1.0)
Vendor Extension Description: microsoft.com: 1.0;

Capture Formats: JPEG
Display Formats: Undefined Type, Association/Directory, Defined Type, JPEG, DPOF, MS AVI, Apple Quicktime, MS Wave

Device Capabilities:
File Download, File Deletion, File Upload
Generic Image Capture, No Open Capture, No vendor specific capture
Nikon Wifi support

Storage Devices Summary:
store_00010001:
StorageDescription: L30
VolumeLabel: None
Storage Type: Removable RAM (memory card)
Filesystemtype: Digital Camera Layout (DCIM)
Access Capability: Read-Write
Maximum Capability: 15539896320 (14820 MB)
Free Space (Bytes): 13047693312 (12443 MB)
Free Space (Images): 2354

Device Property Summary:
Property 0xd407:(read only) (type=0x6) 1
Property 0xd406:(readwrite) (type=0xffff) ”
Property 0xd002:(readwrite) (type=0x6) Enumeration [1,2,3,4,5,6,7] value: 6
Date & Time(0x5011):(readwrite) (type=0xffff) ‘20150910T154012’
Flash Mode(0x500c):(readwrite) (type=0x4) Enumeration [1,2,3,4] value: Automatic flash (1)
Focus Mode(0x500a):(readwrite) (type=0x4) Enumeration [2,3] value: Automatic (2)
Focal Length(0x5008):(read only) (type=0x6) Enumeration [3500,4600,5300,6100,7300,8600,10500] value: 35 mm (3500)
Battery Level(0x5001):(read only) (type=0x2) Enumeration [2,5,25,50,65,80,100] value: 80% (80)
Property 0xd303:(read only) (type=0x2) 1

billf@mars ~/Downloads $ gphoto2 –list-files
There is no file in folder ‘/’.
There is no file in folder ‘/store_00010001’.
There is no file in folder ‘/store_00010001/DCIM’.
There are 200 files in folder ‘/store_00010001/DCIM/100NIKON’.
#1  DSCN0520.JPG rd  8757 KB 5152×3864 image/jpeg
#2 DSCN0521.JPG rd 8631 KB 5152×3864 image/jpeg
#3 DSCN0522.JPG rd 8616 KB 5152×3864 image/jpeg
#4 DSCN0523.JPG rd 8060 KB 5152×3864 image/jpeg
#5 DSCN0524.JPG rd 8644 KB 5152×3864 image/jpeg
#6 DSCN0525.JPG rd 8755 KB 5152×3864 image/jpeg
#7 DSCN0526.JPG rd 7622 KB 5152×3864 image/jpeg
#8 DSCN0527.JPG rd 7185 KB 5152×3864 image/jpeg
#9 DSCN0528.JPG rd 8342 KB 5152×3864 image/jpeg
#10 DSCN0529.JPG rd 8139 KB 5152×3864 image/jpeg
#11 DSCN0530.JPG rd 8769 KB 5152×3864 image/jpeg
#12 DSCN0531.JPG rd 8888 KB 5152×3864 image/jpeg
#13 DSCN0532.JPG rd 8759 KB 5152×3864 image/jpeg
#14 DSCN0533.JPG rd 8744 KB 5152×3864 image/jpeg
#15 DSCN0534.JPG rd 8678 KB 5152×3864 image/jpeg
…. will show all pictures and videos

 

gphoto2 –get-all-files

Make sure you are in the directory you want to be in to download the files

 

Camara PTP with gphoto2

 


Using Eclipse to remote debug Python on a Raspberry Pi

Overview

Using Eclipse on a workstation to debug Python scripts running on a Raspberry Pi is a very powerful python debugging tool. In this howto post the remote debugging workstation will be Linux, my favorite OS, but this should also work the same if you are using a Windows OS remote workstation. You will need to install Eclipse IDE and Pydev plugin on the workstation. On the Raspberry Pi you can install the Eclipse IDE and Pydev plugin, but running Eclipse on the Pi is painfully show, so I don’t recommend installing Eclipse IDE on the Pi we will only be installing only the Pydev plugin on the Pi to makes this work.

You might be asking yourself why not install with the package manger….first reason is current apt version is Eclipse Mars 3.8 and we will be installing the latest stable version (at the time on this writing) Eclipse Neon 4.6 or higher. Second reason is Eclipse is very picky about what versions on both Eclipse itself and the plugin’s and I have found it to be much more crash proof if you install the way I’m describing in this how-to. For example you will not be able to use the install wizard or drag and drop to install PyDev (plugin that allows Python debugging) because it needs the 4.x Neon.

Network Connection

Both devices need to communicate over an IP connection this can be a local network LAN or even over the Internet WAN. For this post I’m assuming the Pi and the workstation are on the same LAN network. Over the Internet you need to make sure any firewalls between the two device will pass port 5678. You will also need to setup port forwarding to the remote workstation. The Pi makes a connection to the workstation when the Python script Pydev debug function pydevd.settrace() is called, more on that later on.

Eclipse Remote Debug

Click to enlarge

 

Check the Java JRE Release number

Eclipse IDE is a java application and the latest as of this post is Eclipse Platform 4.6 Neon. It requires Java 8 (or later) JRE or JDK to run. The installer also uses java, so the first thing you need to do is check and see if you have Java Runtime Environment JRE installed and correct release.

$ java -version
java version “1.8.0_112”
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

On my Linux workstation it shows java version “1.8.0_112” which is Java 8 you look at the second digit of the version to determine the release number, so 1.7 would be Java 7 release. If you don’t have Java release 8 or higher or you get “command not found” you need to figure out why and fix before continuing.

Installing the Eclipse IDE and Pydev plugin on the Remote Workstation

How that you have verified you have Java 8 JRE or higher installed lets download the Eclipse installer. Open your browser and point it to http://www.eclipse.org/downloads/eclipse-packages/ and download the installer. Once you select you system type it will download a tar.gz file “eclipse-inst-linux64.tar.gz” after it downloads unpack it.

$ tar xvfz eclipse-inst-linux64.tar.gz

Change into the “eclipse-installer” sub directory you just unpacked the install to and run the installer.

$ cd eclipse-installer
$ ./eclipse-inst

 

screenshot_eclipse_installer

Click to Enlarge

The Installer will load and you can pick any of the IDE’s that best fits your needs, I chose “Eclipse IDE for PHP Developers”, since I also program in php and includes GIT plugin. After you select one it will then ask you where you want to install to, the default is home directory. Remember or write down where it is install you will need that later to run eclipse.

Click to Enlarge

Click to Enlarge

After it installs chose launch and eclipse should load. To run eclipse later you will find it in the /home/your_user_name/eclipse/php-neon/eclipse for my install /home/billf/eclipse/php-neon/eclipse. To run eclipse from the command line:

$ cd /home/billf/eclipse/php-neon/eclipse
$ ./eclipse

 

Click to Enlarge

Click to Enlarge

 

Installing the Pydev plugin

You need the Pydev plugin to debug and create Python code in Eclipse. There are several ways to install the plugin, but the easiest and fastest is to go to the marketplace.eclipse.com site and use the drag and drop feature. To do that you need to have eclipse up and go to http://marketplace.eclipse.org/.  Second option to install a plugins is from within Eclipse Help -> Eclipse Maketplace. Once at the Eclise Marketplace search for Pydev.

 

Click to Enlarge

Click to Enlarge

You can use the Drag and Drop install plugin by dragging the Install button button to your running Eclipse workspace. You may need to minimize the welcome window so the workspace is available should look like this:

Click to Enlarge

Click to Enlarge

Once you drag and drop the install button this window will appear:

Click to Enlarge

Click to Enlarge

Make sure all the boxes are check then hit confirm. It will start the install and stop popping up this window

Click to Enlarge

Click to Enlarge

 

You need to hit “Select All” and then “OK” if you do not hit “Select All” it will terminate the install with no message and you have to reinstall the plugin. It will continue to install the plugin and ask you to restart once it finishes. Select restart and Eclipse will restart and you will have the Pydev plugin installed.

There is one other plugin I recommend using that allows to to ssh into the Pi and browse the file and use Eclipse IDE features to develop and edit existing Python scripts. The install is the same as installing Pydev. The plugin is called “Remote System Explorer” search for in the market place and install.

Click to Enlarge

Click to Enlarge

 

Once you have completed the Eclpise install and plugin installs you will have a IDE that can debug and develop Python code.

Configuring the Raspberry Pi to allow remote debugging

Like I mentioned in the overview you can install Eclipse on the Pi, but it is painfully slow, so I do not recommend. If you want to install and try for yourself just “sudo apt-get install eclipse”,  or follow the the same steps you did to install Eclipse and the Pydev plugin from above for the remote workstation and you can do both the remote debugging I describe here and standalone. You can skip the rest of the Pi setup below and go right to testing if you installed Eclipse and PyDev plugin on the Pi.

A better option is to only install the PyDev core files on the Pi. I found out you can preform remote debugging on the Raspberry Pi by just installing the pysrc files from the PyDev plugin for Eclipse. What this does is allow to you preform remote debugging without installing Eclipse on the Raspberry Pi, so this complete setup can be done via ssh on a headless Pi.  The other plus for only installing what you need is the full Eclipse install is over 200M where just the PyDev pysrc is only about 20M.

Installing PyDev minimum on the Pi

The location for the PyDev source plugin files is http://www.pydev.org/updates/ when you go there you will see it is just a redirect to the latest stable source.

Click to Enlarge

Click to Enlarge

Click on the link and it will take you to the latest stable version then select plugins.

Click to Enlarge

Click to Enlarge

In the plugin source sub-directory find org.python.pydev.core_6.5.0.201809011628.jar  depending on the release the numbers will change.

Click to Enlarge

Click to Enlarge

Don’t download by clicking on the link we will use wget to download directly on the Pi. The source pysrc we are installing here and the version of the plugin on the workstation do not need to match, so once you install this on the Pi and a new version of PyDev is updated on the remote workstation you don’t need to go back and update the version we are installing on the Pi. Having said that, updating this on the Pi from time to time is a good idea since there may be fixes that would improve performance or security.

How to install on the Pi

To install the necessary files, ssh into the Pi and create a directory in your home directory that we will install the pysrc into. I doesn’t matter where you create it, but you need note where you are installing pysrc it is used in the “sys.path.append()” function in your Python script you will be debugging. Create the directory and change into that directory:

$ mkdir $HOME/eclipse-PyDev
$ cd $HOME/eclipse-PyDev/

 

Next step is to download the source code. To do this right click on the link and select “Copy link location” then type “wget” and paste the source URL.
Note:you need to remove the colon they put in front of org in the link name or else you will get a 404: not found from wget.

$ ~/eclipse-PyDev $ wget https://dl.bintray.com/fabioz/pydev/6.5.0/plugins/org.python.pydev.core_6.5.0.201809011628.jar

Next step we need to unpack using unzip.

$ ~/eclipse-PyDev $ unzip org.python.pydev.core_6.5.0.201809011628.jar

If you followed the above step you have a remote workstation that can debug Python scripts using Eclipse IDE.

Optional: You will get a warning on the Pi when you enable remote debugging and you run the script, you need to do the following to fix. Install cython and build command to get rid of this warning message. Doesn’t seem to affect the debugging, so you can skip, but is easy to fix.

warning: Debugger speedups using cython not found. Run ‘”/usr/bin/python” “/home/billf/eclipse-PyDev/pysrc/setup_cython.py” build_ext –inplace’ to build.

Install both cython and cython3 so we can support both 2.x and 3.x Python scrips.

sudo apt-get install cython cython3

Next build the ext’s for setup_cython.py for both Python 2.x and Python 3.x

/usr/bin/python /home/billf/eclipse-PyDev/pysrc/setup_cython.py build_ext –inplace
/usr/bin/python3 /home/billf/eclipse-PyDev/pysrc/setup_cython.py build_ext –inplace

The above steps will get rid of the warning on the Pi when you run a script in remote debug mode.

End Optional:

Testing Eclipse remote debug Python on a Raspberry Pi

 

Here is a example of a simple test script with a RemoteDebug() function you can add and call in any Python scripts you write to do remote debugging works for both Python 2.x and Python 3.x scripts



import sys
import os

# pydev remote debugger function 
def RemoteDebug():

		try:
			sys.path.append("/home/billf/eclipse-PyDev/pysrc/")

			import pydevd
			global pydevd

			# if you want debug to break as soon as the pydevd.sttrace 
			# function below is called set "suspend=True and the debug will start on the next line of 
			# python code after the function call.
			# if you want to go directly to a break point change "supend=False" and 
			# insert "pydevd.settrace()"function where you want the break point.
			# pydevd.settrace('Remote IP address or hostname',port=5678, suspend=True)
			pydevd.settrace('mars', port=5678, suspend=True)
			# stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console
			# pydevd.settrace('192.168.0.32', stdoutToServer=True, stderrToServer=True)

	#===========================================================================
	#   settrace() debug arguments .
	# 
	#   host: the user may specify another host,if the debug server is not in the 
	#	  same machine (default is the localhost)
	# 
	#   stdoutToServer: when this is true, the stdout is passed to the debug server
	# 
	#   stderrToServer: when this is true, the stderr is passed to the debug server
	#	            so that they are printed in its console and not in this process console.
	# 
	#   port: specifies which port to use for communicating with the server
	#	  default port 5678
	# 
	#   suspend: whether a breakpoint should be emulated as soon as this function is called.
	# 
	#   trace_only_current_thread: determines if only the current thread will be traced or 
	#			       all current and future threads will also have the tracing enabled.
	# 
	#   overwrite_prev_trace: if True we'll reset the frame.f_trace of frames which are already 
	#	                  being traced
	# 
	#   patch_multiprocessing: if True we'll patch the functions which create new processes 
	#	                   so that launched processes are debugged.
	#===========================================================================

			 # hack force 'xterm' terminal for pydevd debugging. Else will get terminal error with curses module			 
			os.environ['TERM'] = 'xterm'
			print ("Remote Debug works")

		except ImportError:
			sys.stderr.write("Error: " + 
			   "You must add pysrc path to the above sys.path.append statement or to your PYTHONPATH.\n")
			sys.exit(1)

def main():

		global remote_debug

		remote_debug = True

		# Check to see if we enabled remote debugging
		if remote_debug == True:
			RemoteDebug()

		print ("Goodbye")  

if __name__ == "__main__":
		main()

Using Eclipse is out of the scope of this how-to there are many youtube video’s, but will get you started if you never used it before…..

Couple of things you need to understand are prospectives and debugging basics. Lets first talk about prospectives in Eclipse. A prospective is a view of the workspace and depending on what you are doing the windows in that prospective change. For example when in the debug prospective windows that pertain to debugging will display window for Variables, Debug server, the Python script you will be stepping through and the icons below the menu will change too. You can move the windows around change the size of them delete them, minimize, maximize them. Try playing around with them…the good news is it is very easy to reset them back to the default. In the top menu:

To reset the current prospective back to its default:
Windows -> Prospective – > Reset Prospective

 

To go to a prospective you can use the icons in the upper right next to a box that says “Quick Access” no icons will display there until you go to a prospective at least once. If you know the name of the prospective you can type in in the “Quick Access” box for example “debug” will pop-up as a Prospective. We will need to go to the Prospective “debug” to test our remote debugging, so type debug in the “Quick Access” box and select Prospective – Debug.

Click to Enlarge

Click to Enlarge

To try your first remote debug you need to have the example script from above installed on the Pi. For this test installing it in the home directory is fine. From the workstation you are running Eclipse on open up a shell and ssh into the Pi. Copy and paste the example into a file called “remote_debugger_test.py”. edit line 21 and put in the IP address of the workstation running Eclipse, not the Pi’s IP address.

Edit line 21 of the remote_debugger_test.py change to the IP of the pc running Eclipse debug server, not the PI’s IP:
pydevd.settrace(‘192.168.0.32‘, port=5678, suspend=True)

In Eclipse go to the upper menu and start the Debug Server:

To start the remote Debug server in Eclipse:
PyDev -> Start Debug Server

You will see in the console window “Debug Server at port: 5678.

Now go to the ssh shell session and run the the remote_debugger_test.py example.

python remote_debugger_test.py

You should see the Eclipse debug server connect it should look like this.

Click to Enlarge

Click to Enlarge

Use the icons below the menu to step through the lines of code.

Once you step through the code the debug will terminate. Back on the ssh session for the Pi you will see output from the print statement display.

 

$ python remote_debugger_test.py
Remote Debug works
Goodbye

 

Happy Debugging!!!
Bill….

 


tcpdump-examples

See the list of interfaces on which tcpdump can listen:

tcpdump -D

Listen on interface eth0:

tcpdump -i eth0

Listen on any available interface (cannot be done in promiscuous mode. Requires Linux kernel 2.2 or greater):

tcpdump -i any

Be verbose while capturing packets:

tcpdump -v

Be more verbose while capturing packets:

tcpdump -vv

Be very verbose while capturing packets:

tcpdump -vvv

Be verbose and print the data of each packet in both hex and ASCII, excluding the link level header:

tcpdump -v -X

Be verbose and print the data of each packet in both hex and ASCII, also including the link level header:

tcpdump -v -XX

Be less verbose (than the default) while capturing packets:

tcpdump -q

Limit the capture to 100 packets:

tcpdump -c 100

Record the packet capture to a file called capture.cap:

tcpdump -w capture.cap

Record the packet capture to a file called capture.cap but display on-screen how many packets have been captured in real-time:

tcpdump -v -w capture.cap

Display the packets of a file called capture.cap:

tcpdump -r capture.cap

Display the packets using maximum detail of a file called capture.cap:

tcpdump -vvv -r capture.cap

Display IP addresses and port numbers instead of domain and service names when capturing packets (note: on some systems you need to specify -nn to display port numbers):

tcpdump -n

Capture any packets where the destination host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n dst host 192.168.1.1

Capture any packets where the source host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n src host 192.168.1.1

Capture any packets where the source or destination host is 192.168.1.1. Display IP addresses and port numbers:

tcpdump -n host 192.168.1.1

Capture any packets where the destination network is 192.168.1.0/24. Display IP addresses and port numbers:

tcpdump -n dst net 192.168.1.0/24

Capture any packets where the source network is 192.168.1.0/24. Display IP addresses and port numbers:

tcpdump -n src net 192.168.1.0/24

Capture any packets where the source or destination network is 192.168.1.0/24. Display IP addresses and port numbers:

tcpdump -n net 192.168.1.0/24

Capture any packets where the destination port is 23. Display IP addresses and port numbers:

tcpdump -n dst port 23

Capture any packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:

tcpdump -n dst portrange 1-1023

Capture only TCP packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:

tcpdump -n tcp dst portrange 1-1023

Capture only UDP packets where the destination port is is between 1 and 1023 inclusive. Display IP addresses and port numbers:

tcpdump -n udp dst portrange 1-1023

Capture any packets with destination IP 192.168.1.1 and destination port 23. Display IP addresses and port numbers:

tcpdump -n “dst host 192.168.1.1 and dst port 23”

Capture any packets with destination IP 192.168.1.1 and destination port 80 or 443. Display IP addresses and port numbers:

tcpdump -n “dst host 192.168.1.1 and (dst port 80 or dst port 443)”

Capture any ICMP packets:

tcpdump -v icmp

Capture any ARP packets:

tcpdump -v arp

Capture either ICMP or ARP packets:

tcpdump -v “icmp or arp”

Capture any packets that are broadcast or multicast:

tcpdump -n “broadcast or multicast”

Capture 500 bytes of data for each packet rather than the default of 68 bytes:

tcpdump -s 500

Capture all bytes of data within the packet:

tcpdump -s 0