Debian Command-line Reference
Contents
- Help
- System Information
- Package management
- System Configuration
- Files and directories
- Users and Groups
- Networking
- Hardware
- Disks and Partitions
- Linux RAID
- Logging
- Archival
- Copying Files Between Machines
- Memory
- Processes
- Modules
- Time and Date
- Network Packet Capture
- Finding Info About Hosts
- Swap Space
- PCMCIA / Cardbus
- To do
Help
info <program-name>
man <program-name>
Documentation shipped with each package, other than man and info pages, such as README.Debian, changelog.Debian.gz and NEWS is kept at /usr/share/doc/<package name>.
System Information
Display running kernel version: uname -a
Display Version of Debin this is or is based on:
cat /etc/debian_version
Package management
Useful switches for aptitude:
-s- simulate actions, but do not actually perform them-V- show which versions of packages are to be installed-Z- show the change in installed size of each package-D- show the dependencies of automatically changed packages-v- display extra information. (may be supplied multiple times)
Update the list of available packages from the apt sources: aptitude update
Install specific packages: aptitude install <package> <package> <package>
Upgrade installed packages to their most recent version. Installed packages will not be removed unless they are unused; packages which are not currently installed will not be installed: aptitude upgrade (use with -svVZD for a simulated run with lots of detail; use with -vVZD to run for-real with lots of detail)
Upgrade installed packages to their most recent version, removing or installing packages as necessary:
aptitude dist-upgrade
Try hard to fix the dependencies of broken packages, even if it means ignoring the actions requested on the command-line: aptitude -f install
Search for a package by name or part of name: apt-cache search <package>
Display detailed information about a package: apt-cache show <package> or aptitude show <package> or dpkg -p <package>
Display information about a package's version, dependencies and reverse dependencies: apt-cache showpkg <package>
Display more detailed information about a package's versions available and where they'll be installed from: apt-cache policy <package>
list all files included with a particular package: dpkg -L <package>
Find which package provided a particular file: dpkg -S <file> or use or use http://packages.debian.org or on #debian !find <file> <Debian version name>
Display information about a package including its configuration files:
dpkg -s <package>
Add Apt download sources: apt-setup
(this command not available in Sarge but not Testing/Etch)
Add a CD/DVD to your sources list: apt-cdrom add -d /...
List all installed packages and their versions: dpkg -l
Reconfigure a package: dpkg-reconfigure <package>
Choose a specific priority for questions:
dpkg-reconfigure -plow|medium|high|critical <package>
List all installed packages with version number and a summary of their purpose:
dpkg -l
See if a particular package is installed and what version it is:
dpkg -l | grep <package>
Find the location of a package: whereis <package>
List package signing keys: apt-key list
System Configuration
tasksel
debconf
Debian base system configuration. (not included in Testing/Etch): base-config
Files and directories
List files and directories, with information on permissions and (human readable) file sizes:
ls -lh
List files and directories, with information on permissions and (human readable) file sizes and without hiding those beginning with a '.':
ls -alh
List files and directories, without hiding those beginning with a '.', listing entries by columns and appending an indicator to entries to signify their type: ls -aCF
Copy a directory, recursively, to a new location, preserving timestamps:
cp -a <source directory> <destination directory>
If the new directory doesn't exist it will automatically be created, if it does exist then files will be overwritten unless you additionally instruct it not to. Copying the whole directory ensures hidden files / dotfiles (.*) are copied, which would be omitted if you used *.
Use -i to be prompted before overwriting existing files, use -n to not overwrite existing files, use -u to only overwrite existing files when the source file is newer than the destination file.
For verbosity add -v but note that it slows the operation down.
(-a is the same as -dpR where -d is 'same as --no-dereference --preserve=links' where --no-dereference is 'never follow symbolic links in SOURCE' and --preserve[=ATTR_LIST] is 'preserve the specified attributes (default: mode,ownership,timestamps)'); -p is 'same as --preserve=mode,ownership,timestamps'; -r is 'copy directories recursively')
Alternatively you could achieve this in two lines using:
copy everything beginning with a '.', apart from '..':
cp -a <source directory>/.[^.]* <destination directory>/
copy everything else, everything that doesn't begin with a '.':
cp -a <source directory>/* <destination directory>/
Copy files and directories (all of them not beginning with a '.', and all of them beginning with a '.' followed by a number or letter) from the current location, recursively, preserving timestamps:
cp -a * .[0-z]* <destination directory>/
Copy files from source directory to target directory, replacing files in target if older than those in source, deleting source files as it progresses (requires the rsync package be installed); like if you were able to 'mv -u <source directory>/* <target directory>/ -r' but you can't in BASH:
rsync -avz --update --remove-source-files <source directory>/ <target directory>
Find any files that contain specific text (case insensitive) in a specific directory and below:
grep -rin "<text to search for>" <directory to search in> (leave out the i switch for case sensitive; use * in place of directory to indicate current directory)
Display the appended contents of a file whilst it grows:
tail -f <file>
With more than one file, precede each with a header giving the file name. Useful for looking at log
files.
Display the last 10 lines of a number of files at once:
tail <one file> <another file>
Display the size taken up by files and directories:
du --max-depth=1 -h | sort -rn.
Use --si in place of -h for a similarly human-readable output but using powers of 1000 not 1024 (i.e. mebibytes rather than megabytes).
This can take a long time to run and can consume large amounts of CPU and memory resources. If you increase 'max-depth' it will display results from deeper in the directory tree, showing you deeper directories with large sizes, but it shows too many directories with insignificant sizes. I find it easiest to use 'max-depth=1' and if you find a large directory that you want to investigate further you drill down into it by moving into it and using du again. The sort somewhat sorts the list by size but is far from perfect as Kilobyte, megabyte and gigabytes are separated.
Find a file, case insensitively, looking recursively down from the current position:
find -iname <filename> or find -iname *<filename>* or find -iname <filename>* or find -iname *<filename>
Find a file, case sensitively, looking recursively down from the current position:
find -name <filename> or find -name <filename*>
Find a file: locate - (use updatedb after you've made changes to the files on the system, such as when installing a package)
Change mode: chmod
Change ownership: chown
Change a file's creation date:
touch -d 19-sept-2007 <file>
Change a file's creation time:
touch -m -d 08:17:10 <file>
Users and Groups
Add a user: adduser <user>
Delete a user: deluser <user>
Add a group: addgroup <group>
Delete a group: delgroup <group>
Add a user to a group: adduser <user> <group>
Delete a user from a group: deluser <user> <group>
Change a user's UID: usermod -u <new UID> <username>
Networking
Hostname of the machine is defined in /etc/hosts and /etc/hostname.
IP address configuration is defined in /etc/network/interfaces unless NetworkManager is managing it.
DNS address is defined in /etc/resolv.conf. But note if resolvconf is managing it you should define the addresses via resolvconf.
Configure a network interface: ifconfig - i.e. ifconfig eth0 netmask 255.255.255.0 broadcast 10.0.0.255 route? gateway? 10.0.0.1
Disable a network interface: ifdown eth0
Enable a network interface: ifup eth0
Restart all networking: /etc/init.d/networking restart
Look for a DHCP server and renew the IP address: dhclient
Find the machine's IP address:
- #
ifconfig - $
/sbin/ifconfig
Find the machine's IP address: ip addr
Show open ports and whether they listen on just localhost or for remote connections: netstat -l -t -p
Display the routing table: netstat -r
Probe a machine for open ports: nmap -v -A <IP address or hostname>
Get details of how ethernet network card is operating including speed, auto-negotiation, duplex, etcetera (requires the ethtool package)
ethtool <ethernet interface i.e. eth0>
Hardware
Comprehensive list of hardware: lshw (requiers the 'lshw' package be installed)
Compact list of hardware: lshw -short (requiers the 'lshw' package be installed)
Compact list of hardware, giving bus that devices are on: lshw -businfo (requiers the 'lshw' package be installed)
List all PCI (and PCMCIA) devices: lspci
List all USB devices: lsusb
List some PCMCIA device information: lspcmcia
Turn on DMA (use with caution): hdparm
Scan hardware (needs installing): discover
Get a listing of the devices found on the USB do: cat /proc/bus/usb/devices
echo "scsi add-single-device 1 0 5 0" > /proc/scsi/scsi
^ ^
| |
SSCI-bus --+ +----- SCSI-id
Linux provides a simple mechanism to probe a SCSI device on demand. The kernel will probe and recognize the device. This needs to be done as root. (The first zero stands for the first SCSI-channel on your adapter and the last one for the LUN).
Another method (if you're using modules and if the scanner is the only device on the bus) is to unload the modules (SCSI-card and SG) and reload them.
Take a look at scsidev, a utility that comes with Debian distributions. http://www.garloff.de/kurt/linux/scsidev/ SuSe nowadays comes with a script rescan-scsi-bus.sh. And finaly, Oliver Rauch has a tool available. See http://www.rauch-domain.de
scsiadd (not installed by default)
- Scan SCSI bus:
scsiadd -s - Add device at this address:
scsiadd -a <host> <channel> <id> <LUN> - Remove device at this address:
scsiadd -a <host> <channel> <id> <LUN>
Disks and Partitions
Setting Up Partitions
List partitions on a disk: fdisk -l
Partition a disk / Define or redefine the partition's filesystem type that will be used on it:
# cfdisk /dev/<disk device reference i.e. sdb>
Choose 'Type' → choose the filesystem type such as '83 Linux', '0B W95 FAT32', '0C W95 FAT32 LBA' (I need to research the difference between these two), '07 HPFS/NTFS' (AKA NTFS) → choose Write
Format a partition. When you create partition(s), decide what filesystem you want to use.
The most likely you'll want for GNU/Linux is ext3, but you may for example be formatting flash media which usually use FAT.
Format partitions using the correct tool for the particular file system. (-cc does a read-write bad block test before creating the file system (which can take a very long time); -c does just a read test (which is quicker); leave out the -c for a quicker but non-testing format.
- ext3:
mkfs.ext3 -v -c /dev/<disk partition reference i.e. sdb1>
(AKA mke2fs) - MSDOS FAT12 and FAT16:
mkfs.msdos -v -c /dev/<disk partition reference i.e. sdb1>
(AKA mkdosfs and mkfs.vfat)
Note: requires the dosfstools package to be installed. - MSDOS FAT32:
mkfs.msdos -v -c -F 32 /dev/<disk partition reference i.e. sdb1>
(AKA mkdosfs and mkfs.vfat; Note that a FAT32 filesystem cannot be too large)
Note: requires the dosfstools package to be installed. - NTFS (version 1.2, AKA NTFS4):
mkfs.ntfs -v /dev/<disk partition reference i.e. sdb1>
(AKA mkntfs)
Note: requires the ntfsprogs package to be installed.
Note: mkfs.ntfs doesn't use the -c flag the same way other varients do
Label a partition
- ext3, either:
e2label /dev/<disk partition reference i.e. hda1> <label>
(NOTE: this claims it will wipe all files on the partition!)mkfs.ext3 -L <label> /dev/<disk partition reference i.e. hda1>
(NOTE: presumably this will claim to wipe all files on the partition!)- MSDOS FAT12, FAT16 or FAT32:
mkfs.msdos -n <label> /dev/<disk partition reference i.e. hda1>
(NOTE: this will wipe all files on the partition!) - NTFS:
mkfs.ntfs -L <label> /dev/<disk partition reference i.e. hda1> <label>
(NOTE: this might wipe all files on the partition! I haven't tried, but the FAT32 command does)
Set permissions on a removable disk (and any of its files and directories) so that anyone can write to the disk. Without this regular users can't write to the disk. If you want to use more restrictve permissions then user/group numbers have to be the same on all systems the disk will be plugged into:
chmod -R ugo+rwx /media/<partition label>
Check a partition's ext2/ext3 formatted filesystem for defects, and if required repair it, (do NOT run this on a mounted filesystem, though it will warn you if the file system is mounted):
e2fsck -p /dev/<disk partition reference i.e. sdb1>
Partition Information
List the partition tables of all attached disks (describes partition names and filesystem types)
# fdisk -l
List the contents of the filesystem superblock, which gives plenty of partition information such as the inode size:
tune2fs -l /dev/<disk partition reference i.e. sda1>
Copying Partitions
Copy one partition to another
Copy a floppy disk image to a floppy disk:
dd if=filename.img of=/dev/fd0 bs=1024 conv=sync
Linux RAID
Example RAID partition: /dev/md0
Get brief details of a RAID partition:
mdadm --query <RAID partition>
Get full details of a RAID partition:
mdadm --detail <RAID partition>
Logging
Set the level at which logging of messages is done to the console:
dmesg -nlevel1
(-1 prevents all messages, except panic messages)
Archival
tar/gzip
- archive a location to a file:
tar -vcpzf <filename>.tar.gz <location>-v: verbosely list files processed-z: filter the archive through gzip-p: preserve permissions-f: use stdin/stdout-c: create a new archive
- extract an archive file to the current directory:
tar -xzf <filename>.tar.gz-z: filter the archive through gzip-p: preserve permissions-f: use stdin/stdout
- extract an archive file to a specific directory:
tar -xzfC <filename>.tar.gz <directory to extract to> - list contents of a .tar.gz / .tgz file:
tar -tz <file> - list contents of a .gz file:
gzip -l <file>
Copying Files Between Machines
Securely copy a single file from 'server' (logged into as root) to this computer:
scp root@server:/etc/passwd .
Securely recursively copy a directory from 'server' (logged into as root) to this computer:
scp -r root@server:/etc/* .
Securely copy a single file from 'server' (logged into as root) to this computer, preserving
modification times, access times, and modes from the original file:
scp -pr root@server:/etc/* .
Memory
Display amount of free and used memory in the system, including swap, in megabytes:
free -m
Virtual memory statistics - displays a table of various event counters and memory statistics:
vmstat -s
Virtual memory statistics - display a list of active/inactive memory:
vmstat -a
Processes / Running Programs
htop (requires the 'htop' package)
top
pstree
/etc/init.d/<package> restart | start | stop
ps - report a snapshot of the current processes
- List all processes open by anyone:
ps aux - In all processes open by anyone, find a particular program:
ps aux | grep <program> - Get total number of processes open by anyone:
ps aux | wc -l - Get total number of a particular program open by anyone:
ps aux | grep <program> | wc -l ps axjf
List all files open and by which program and user: lsof
Count the number of open files:
lsof | wc -l
Get process IDs of all copies of a running program: pgrep <program>
Get an extended memory map of a process or processes:
pmap -d <process ID>
? http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
kill - kill a running process
- Kill a process by its ID:
kill <process ID> pkill <program>killall <program>
Modules (known as 'drivers' in Windows)
Get information about a running module: modinfo <module>
Specifically, get the version of a running module: cat /sys/module/<module>/version or modinfo <module> |grep -i version
Install a loadable module in the running kernel: insmod
Show the status of modules in the kernel: lsmod
modprobe
Show lots of information about a kernel module: modinfo <module>
modutils
Configure modules: modconf
Time and Date
Manually set system time and date
Set the time, example:
date -s 11:25
Set the date, example:
date -s 10/20/2007
Set the time and date, example:
date -s "10/20/2003 11:25:00"
Network Packet Capture
Display a synopsis of packets transferred through an ethernet interface:
# tcpdump -i <ethernet interface i.e. eth1>
You can pipe through grep if you're looking for a particular IP address
Finding Info About Hosts
Get DNS A (IP address) record for a domain:
dig <domain name to search for>
Get DNS A (IP address) and NS (nameserver) records for a domain:
dig <domain name to search for> any
Get DNS MX (mail exchange) record for a domain:
dig <domain name to search for> mx
Reverse DNS lookup for a domain - map IP address to a name:
dig -x <IP address to lookup>
Get registrant, registrar, registration and renewal dates
whois <domain name to search for>
Get detailed host information (name, address, IP address block)
whois <IP address to lookup>
Detect operating system, server software and versions:
nmap -A <IP address to scan>
Note: with dig you can use '@<name server domain name>' when troubleshooting DNS issues to query a specific DNS server, from root name servers downwards.
Note: with dig you can use '+trace' to show the full delegation path.
Swap Space
Display amount of free and used memory in the system, including swap, in megabytes: free -m
Display swap usage summary: swapon -s
Specify a label (for use by fstab, swapon and swapoff): mkswap -L <label> <device>
To check that your swap space is being automatically mounted without having to reboot, turn off all swap spaces using swapoff -a and then mount all swap spaces listed in /etc/fstab using swapon -a.
PCMCIA / Cardbus
cardctl status
cardctl info
To do
mount -t smbfs -o username=winadmin,password=winadmin //server/winadmin /media/server
Run a command repeatedly, displaying its output (the first screen-full):
watch <command>
This allows you to watch the program output change over time. By default, the program is run every 2 seconds.