Useful commands to administer a Linux based system.
sudo command
Execute a command as another user.
It will ask for your user password and will execute the command if you have permissions enough in /etc/sudoers file.
By default sudo executes a command as root:
$ sudo cat /etc/sudoers
# Show content of /etc/sudoers file. Execute a command as user vincent:
$ sudo -u vincent ls /tmp
# List /tmp directory as user vincent.Run a login shell for another user (root by default):
$ sudo -i
Edit sudo permissions for users in /etc/sudoers file:
$ visudo
ps command
ps tool lists unix processes
List all processes in the system:
$ ps -e
top
It shows processes, CPU consumption, PID, etc.
htop
Improved version of top tool.
iotop
Monitor input and outputs (disk reads and writes) in a top like manner.
Show only processes that perform I/O and accumulate values:
$ sudo iotop -o -a
free
Show amount of free and used memory in the system:
In human readable format:
$ free -h
uname
Print system information:
$ uname -a
Linux mySystem 4.4.0-1-amd64 #1 SMP Debian 4.4.8-2 (2016-05-16) x86_64 GNU/Linux
lsusb
List USB devices
$ lsusb
Bus 002 Device 022: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
udevadm
udev management tool.
Show usb related (--subsystem-match=usb) kernel uevents (--kernel), events sent out by udev rules (--udev) and display properties too (--property):
$ sudo udevadm monitor --subsystem-match=usb --kernel --property --udev
STORAGE AND FILESYSTEM RELATED COMMANDS
lsblk
List block devices, partition sizes, mount points, etc
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 100G 0 part
├─sda3 8:3 0 50G 0 part [SWAP]
└─sda4 8:4 0 681.5G 0 part /home
sr0 11:0 1 1024M 0 rom
Show info about filesystems
$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 system cb3d3c17-0865-448e-b7c1-04a50f2a7e80 /
├─sda2 ext4 secondary c0fb3dca-eb48-49e8-ace3-4326fd168439
├─sda3 swap ee7d92ec-a542-4c42-8b2e-f9e14b8b9980 [SWAP]
└─sda4 ext4 home 9f93bcb8-8723-43e1-8d08-8e40d3f5340a /home
fdisk
fdisk manipulates disk partition tables.
List current partitions:
$ fdisk -l
Disk /dev/sdb: 15 GiB, 16131293184 bytes, 31506432 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x04030201
Device Boot Start End Sectors Size Id Type
/dev/sdb1 63 31503464 31503402 15G b W95 FAT32
Partition /dev/sdb device
$ fdisk /dev/sdb
mkfs.ext4
Create an ext4 filesystem in /dev/sdb1 partition:
$ sudo mkfs.ext4 /dev/sdb1
Create an ext4 filesystem in /dev/sdb1 partitional and label the volume 'myLabel':
$ sudo mkfs.ext4 -L 'myLabel' /dev/sdb1
Same but disabling journaling to minimize writes to disk:
$ sudo mkfs.ext4 -O "^has_journal" -L 'myLabel' /dev/sdb1
mount, umount
Mount and umount a filesystem in the big tree that pends from / root
Show all currently mounted filesystems:
$ mount
Mount sda2 device as an ext4 filesystem in /home directory:
$ sudo mount -t ext4 /dev/sda2 /home
Remount part of filesystem hierarchy (/home) in another place (/tmp/foo):
$ sudo mount --bind /home /tmp/foo
Remount part of filesystem hierarchy and also its submounts in another place:
$ sudo mount --rbind /dev chroot/dev
Mount again but in read only way:
$ sudo mount -o remount,ro /dev/sda1 /
Unmount a directory or a device:
$ sudo umount /dev/sda2
$ sudo umount /home
Unumount a directory recursively: (it will fail if any submount fails)
$ sudo umount -R /home
lsof
Without arguments lsof lists all open files in the system belonging to active processes.
Passing a file path to lsof it returns info about processes which have opened that file.
$ lsof /dev/tty1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 1482 vicente 0u CHR 4,1 0t0 1042 /dev/tty1
bash 1482 vicente 1u CHR 4,1 0t0 1042 /dev/tty1
NETWORK RELATED COMMANDS
netstat
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
Show network interfaces:
$ netstat -i
Show routing table:
$ netstat -r
$ netstat -rn
# -n option make netstat show numerical addresses, ports, etc instead of determining host, port, etc.Show listening connections:
$ netstat -l
Show name and PID of processes running each socket:
$ netstat -p
ip command
ip - show / manipulate routing, devices, policy routing and tunnels
Show devices and their IPv4 and IPv6 associated addresses:
$ ip address
Show routing table:
$ ip route
List all network devices:
$ ip link
Enable or disable enp0s3 device:
$ ip link set enp0s3 up
$ ip link set enp0s3 down
dhclient
Client for DHDP
Get an IP address for enp0s3 device from DHCP server:
$ dhclient enp0s3
Release current IP address for enp0s3 device:
$ dhclient -r enp0s3
wget
wget is a non-interactive network downloader. It can download web sites recursively.
Download a file:
$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/testing/linux-4.9-rc8.tar.xz
Download a ftp site:
$ wget -c -r -l 0 --timeout 60 ftp://my_user:my_pass@www.foo.net/dir_app
Options stand for:
-c : continue downloading partially downloaded files
-r : download directories recursively
-l 0 : recurse levels indefinitely
--timeout 60 : timeout after 60 seconds.
Download a http site:
$ wget -k -r -l 3 -D www.kernel.org https://www.kernel.org/pub/linux/
-D : follow links only to this address.
-k : convert links
curl vs wget:
https://daniel.haxx.se/docs/curl-vs-wget.html
REFERENCE
Chapter 17. System and Administrative Commands (Advanced Bash Scripting Guide)