Skip to content

Instantly share code, notes, and snippets.

@darioseidl
Last active October 8, 2020 20:55
Show Gist options
  • Save darioseidl/4cc93f85c9581a43b208f8dfa5f209b0 to your computer and use it in GitHub Desktop.
Save darioseidl/4cc93f85c9581a43b208f8dfa5f209b0 to your computer and use it in GitHub Desktop.
Some useful commands for Debian administration

Doing something important on the server:

Restoring a mysqldump:

mysql < mysqldump.sql

When restoring a dump of all tables from a different machine (e.g. using a dump from the production server on the VM), this will override the debian-sys-maint user (as well as other users and passwords).

Detele the sections Table structure for table `user` and Dumping data for table `user` from the mysqldump file (or see http://serverfault.com/questions/9948/what-is-the-debian-sys-maint-mysql-user-and-more for how to fix). But it's probably easier/better to just use dumps of single tables instead.

Useful commands:

Check which distribution is installed (on a vserver):

cat /etc/*-release
lsb_release -a

Find [XXX] tags:

grep -r "\[XXX\]" /etc/ /usr/local/bin/

Find config files that diff from their .deb:

debsums -se

Find non-config files that diff from their .deb:

debsums -s

Find which package a config file is from:

dpkg -S <config-file>

Replace a missing config file from it's package:

apt-get install --reinstall -o Dpkg::Options::="--force-confmiss" <package>

Check apache2 config:

apache2ctl configtest

Show loaded apache2 modules:

apache2ctl -M

Check nginx config:

nginx -t

Tomcat logs:

less +F /var/log/tomcat7/catalina.out
less +F /var/log/tomcat7/localhost<TAB>

Apache2 logs:

less +F /var/log/apache2/access.log
less +F /var/log/apache2/error.log 
less +F /var/log/apache2/mod_jk.log

Update and run rkhunter:

rkhunter --update && rkhunter --check --skip-keypress
less +F /var/log/rkhunter.log

Change a mysql user password:

echo "SET PASSWORD FOR 'user'@'localhost' = PASSWORD('password');" | mysql

Delete a line from bash history (e.g. a password):

history -d <line-number> && history -w

Edit the mysql history

nano ~/.mysql_history

Clean package cache (run before making a backup to make it smaller):

aptitude clean

Show changelog for a package:

apt-get changelog <package-name>

Set package on hold (dpkg, apt-get, and apticron use a different method than aptitude; better do both):

apt-mark hold <package-name>
aptitude hold <package-name>

Tomcat server info:

java -cp /usr/share/tomcat7/lib/catalina.jar org.apache.catalina.util.ServerInfo

Find high priority packages that are not installed, or automatically installed:

aptitude search '(~prequired|~pimportant|~pstandard)(!~i|~M)'

Find manually installed, low priority packages:

aptitude search '(~poptional|~pextra)~i!~M'

Find manually installed, low priority, libary packages (except apache modules):

aptitude search '^lib!libapache(~poptional|~pextra)~i!~M'

Write list of low priority packages from remote host to file:

ssh "$host" 'aptitude search "(~poptional|~pextra)~i!~M" -F "%p"' > "$host-packages"

List packages with user-tags:

aptitude search '?user-tag()' -F '%p %T'

List third-party packages (not from the Debian repo):

aptitude search '~S ~i !~ODebian !~o'

Show last logged in users

last -w

Also show shutdown entries

last -w -x

Change when apticron runs (edit the time it ran last time; it runs every 24 hours; the minute is specified in /etc/cron.d/apticron)

touch -d "today 06:00" /var/lib/apticron/{cron,last}_run

Playback a collectl log with all columns

collectl --all -p /var/log/collectl/*.raw.gz

Playback a collectl log with CPU, memory, disk and network columns

collectl -s cmdn -p /var/log/collectl/*.raw.gz

Recursive chmod for files/directories:

find <parentdir> -type d -exec chmod 755 {} \;
find <parentdir> -type f -exec chmod 644 {} \;

Find files older than a given days:

find . -mtime +7 

Find files older than a specific date:

find . ! -newermt 20170101

Upgrade packages

sudo aptitude update
sudo aptitude upgrade

Purge configuration files

sudo aptitude purge '~c'

Remove obsolete package cache

sudo aptitude autoclean

Remove all package cache

sudo aptitude clean

Update apt-file database

sudo apt-file update

Update locate database

sudo updatedb

Restore missing configuration files from package

sudo aptitude reinstall -o Dpkg::Options::=--force-confmiss $packagename

or from .deb file

dpkg -i --force-confmiss ${package}.deb

Show manually installed packages

aptitude search '~i!~M!'

Show manually installed packages of low priority

aptitude search '~i!~M(~poptional|~pextra)'

Show installed packages with broken recommends

aptitude search '~S~i~Brecommends'

Show intalled packages from foreign architecture

aptitude search '~i!~M!~rforeign'

Show third party packages

aptitude search '~i!~M!~ODebian'

Show installed deb-multimedia packages

aptitude search ~mmarillat@deb-multimedia.org

Show low priority transitional and dummy packages

aptitude search '~i!~M(~poptional|~pextra)(~dtransitional|~ddummy)'

Show automatically installed packages that are not a dependency or recommendation of any package (only a suggestion)

aptitude search '~i~M!~Rdepends:~i!~Rrecommends:~i'

Show intalled packages from a given release e.g. unstable

apt-show-versions | grep unstable

Show orphaned libs

deborphan

Show orphaned low priority packages

deborphan --all-packages --no-show-section --priority optional | sort

Show alternatives

sudo update-alternatives --get-selections 

Update alternatives

sudo update-alternatives --all

Show java alternatives

sudo update-java-alternatives -l

Update java alternatives

sudo update-java-alternatives -s java-6-sun

Delete command history

history -c

Find broken symlinks in /usr

sudo find -L /usr -type l -ls

with color

sudo find -L /usr -type l -exec ls -lF --color=yes '{}' +

List missing and unexplained files and broken symlinks with cruft

sudo cruft --ignore "/dev /proc /sys /tmp /var /opt /usr/local /usr/lib/pymodules /boot/grub /boot/efi /home" -r "$HOME/cruft_report-$(date "+%Y-%m-%d_%H-%M-%S")"

List modified package files with debsums

Non-configuration files

sudo debsums -cg

Configuration files

sudo debsums -ceg

All files

sudo debsums -cag

Verify package files with dpkg

sudo dpkg --verify

Monitor hard-drive health with smartmontools

Quick over-all health of all disks

for dev in $(sudo smartctl --scan | cut -d" " -f1); do echo "${dev}:"; sudo smartctl -H $dev | \grep "result"; done

Detailed information of a device, e.g. /dev/sda

sudo smartctl -i /dev/sda

Scan for viruses with clamav

clamscan -ri --log=clamscan.log /

Monitor scanning process

watch -n 1 "ps u -C clamscan"

Scan for viruses with avg

Download and install from http://free.avg.com/us-en/download.prd-alf

sudo avgctl --start
sudo avgupdate
sudo avgscan -awmP --report avgscan.log / 

Search for a pattern in /etc

sudo grep -r -A 1 --exclude-dir=.git PATTERN /etc

Search for pattern in hidden files and directories in ~

sudo grep -r -A 1 --include=.* PATTERN ~

Locate old config files

sudo find /etc -regextype posix-egrep -regex '.*\.((dpkg)|(ucf))-((bak)|(dist)|(old)|(remove))'

Delete

sudo find /etc -regextype posix-egrep -regex '.*\.((dpkg)|(ucf))-((bak)|(dist)|(old)|(remove))' -delete

Recursive chmod for files or directories

for files:

find $path -type f -exec chmod $expression {} \;

for directories:

find $path -type d -exec chmod $expression {} \;

e.g.:

find . -type f -exec chmod u=rw,go=r {} \;
find . -type d -exec chmod u=rwx,go=rx {} \;

verbose:

find . -type f -print -exec chmod u=rw,go=r {} \;
find . -type d -print -exec chmod u=rwx,go=rx {} \;

Select the best repositories using netselect

install and run 'netselect-apt' copy the created sources.list file or parts from it to /etc/apt/sources.list

List manually installed packages with aptitude

to show all installed packages

aptitude search ~i

to show only manually installed packages

aptitude search '!~M~i'

to further exclude packages of of high priority (standard, important, required) and essential packages, use

aptitude search '!~pstandard!~pimportant!~prequired!~E!~M~i'
  • ~i installed packages
  • ~M automatically installed packages
  • ~E essential packages
  • ~p packages with given priority
  • ! not

1 2

Show package versions, upgradeable packages and upgrade specific packages

show package versions

apt-show-versions

e.g. to show all packages not from lenny use

apt-show-versions | grep -v lenny

or to show packages from lenny-backports

apt-show-versions | grep backports

or to show manually upgardeable packages

apt-show-versions | grep manually

run

aptitude install `apt-show-versions -u -b | grep $pattern`

to upgrade only packages that match $pattern

Find third-party packages

aptitude search '~i!~M!~o!~ODebian'

Show available versions and apt pinning priorities

show available versions and apt pinning priorities for package $packagename

apt-cache policy $packagename

Install a specific package version with aptitude

aptitude install $packagename=$version

e.g.

aptitude install virtualbox-ose=3.1.4-dfsg-1~bpo50+1

Change console keyboard layout

dpkg-reconfigure keyboard-configuration
setupcon # from package console-setup

Troubleshooting

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8
dpkg-reconfigure locales

Check if there are open SYN_RECV (possible syn flood attack):

netstat -tuna | grep :80 | grep SYN_RECV

Fix for the following warnings when running apt: upgrade debconf to >= 1.5.57

Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/^(.*?)(\\)?\${ <-- HERE ([^{}]+)}(.*)$/ at /usr/share/perl5/Debconf/Question.pm line 72.
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE ([^}]+)}/ at /usr/share/perl5/Debconf/Config.pm line 30.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment