Command | |
ansible-playbook file.yaml | Run an Ansible playbook called file.yaml |
Authentication options | |
–user, -u <username> | Log in as username |
–private-key, –key-file <key> | Log in using SSH key (usually in ~/.ssh) |
–ssh-extra-args | Pass extra command options to SSH |
–vault-id <id> | Use vault identity ID |
–vault-password-file <key> | Use vault password file key |
–ask-vault-pass | Prompt for a vault password |
–become | Escalate privileges |
–ask-become-pass | Prompt for a password for become |
–become-method | Escalate privilege using a specific method |
ansible-doc –-type foo –list | List choices for become, connection, and other Ansible options |
Control options | |
–syntax-check | Verify syntax of a playbook, but do not run it |
–list-hosts | Show hosts listed in a playbook |
–list-tasks | Show tasks defined in a playbook |
–start-at-task <task_name> | Run playbook starting at task name |
–check | Run the playbook but don’t make changes |
–diff | Show diffs for what changes are made |
–module-path | Prepend colon-separated path to default path |
–connection <method> | Connect over method |
Playbook and YAML | |
Parameter: value | A YAML mapping entry is a key and a value |
– foo | A YAML sequence entry is an itemized list |
– bar | |
– baz | |
Distro: | A mapping entry can contain a sequence |
– Fedora | |
– RHEL | |
– Debian | |
– Slackware | |
OS: | Sequence items can contain mappings |
– Linux: Fedora | |
– BSD: NetBSD | |
Playbook structure | |
— | YAML files start with three dashes |
– name: “My play” | Use the name mapping to name your play |
hosts: all | Indent, and define which hosts the play runs on. List target hosts in etc/ansible/hosts |
tasks: | Open a tasks mapping, which will contain a sequence |
– name: | “My task” Give the task a name with the name mapping |
some_module: | Import a module as a new mapping containing a sequence of parameters. Find required and optional parameters in the module’s documentation. |
path: ‘/example/’ | Parameters are usually mappings using the command option as the key and an argument as the value. |
– name: “My other task” | A play may contain more than one task |
other_module: | A task usually imports a module |
foo: ‘bar’ |
Month: March 2022
grep cheat sheet
Basics | |
grep [pattern] FILE | |
grep ‘^[A,E].*o’ f.txt | Find a string starting with A or E and ending in o |
grep -f pat.txt f.txt | Scan f.txt, using contents of pat.txt as regex |
grep -i Gnu f.txt | Find “gnu” in f.txt, ignoring capitalization |
grep -v gnu f.txt | Find all lines not containing “gnu” (invert match) |
grep -w ‘a.*o’ f.txt | Find whole word matches only, ignoring substrings |
grep -x ‘a.*o’ f.txt | Find whole line matches only, as in ^(a.*o)$ |
Output | |
-c | Print only the number of lines containing a match |
–colo[u]r | Display matches in color |
-l | Print the names of files with matches |
-L | Print the names of files searched that contained no matches |
-o | Print only the matched part of a line |
-s | Suppress errors (such as non-existent or unreadable files) |
-A n | Print n number of lines after a matching line |
-B n | Print n number of lines before a matching line |
-C n | Print n number of lines before and after a matching line |
Output prefixes | |
-b | Print the byte offset of the match within the input file |
-H | Print the filename containing a match |
-h | Do not print the filename containing a match |
-n | Print the line number of each match |
-T | Print an initial Tab before matches so that output is neatly aligned |
File and directory selection | |
-a | Process a binary file as if it were text |
-D <skip|read> | Skip or read a FIFO, device, or socket |
-d <skip|read|recurse> | Skip, read, or recurse through a directory |
–exclude ‘*.sh’ | Do not search any file with the .sh suffix |
–exclude-from FILE | Skip any file listed in FILE |
–exclude-dir *foo | Skip any directory ending in foo |
-r | When a directory is encountered, search files in it |
-R | Search directories and follow symlinks |
Variants | |
-G | Use basic regex (this is the default) |
-E | Extended regex |
-F | Interpret the search pattern as a fixed string, not regex |
-P | Use Perl regex (PCRE) |
Regular expression | |
. | Any single character |
? | Match preceding item zero or one time |
* | Match preceding item zero or more times |
+ | Match preceding item one or more times |
{2} | Match preceding item two times |
{3,} | Match preceding item three or more times |
{,4} | Match preceding item at most four times |
{1,5} | Match preceding item at least once, but no more than five times |
[A,B] | Match A or B |
[3-9] | Match all digits 3 to 9 |
^ | Start of a line |
$ | End of a line |
\s | Space |
[:alnum:] | Alphanumeric character |
[:alpha:] | Alphabetic character |
[:digit:] | Digits 0 through 9 |
[:punct:] | Punctuation |
[:space:] | Space |
lvm cheat sheet
Logical Volume Manager (LVM)
This is a quick and dirty cheat sheet on LVM using Linux, I have highlighted many of the common attributes for each command however this is not an extensive list, make sure you look up the command.
With the pvs, vgs and lvs commands, the number of verboses added the more verbose information for example pvs -vvvvv
Directory and Files | |
Directories and Files | ## Directories /etc/lvm – default lvm directory location /etc/lvm/backup – where the automatic backups go /etc/lvm/cache – persistent filter cache /etc/lvm/archive – where automatic archives go after a volume group change /var/lock/lvm – lock files to prevent metadata corruption # Files /etc/lvm/lvm.conf – main lvm configuration file $HOME/.lvm – lvm history |
Tools | |
diagnostic | lvmdump lvmdump -d dmsetup [info|ls|status] Note: by default the lvmdump command creates a tar ball |
Physical Volumes | |
display | pvdisplay -v pvs -v pvs -a pvs –segments (see the disk segments used) pvs attributes are: 1. (a)llocatable 2. e(x)ported |
scanning | pvscan -v Note: scans for disks for non-LVM and LVM disks |
adding | pvcreate /dev/sdb1 ## Create physical volume with specific UUID, used to recover volume groups (see miscellaneous section) pvcreate –uuid /dev/sdb1 Common Attributes that you may want to use: -M2 create a LVM2 physical volume |
removing | pvremove /dev/sdb1 |
checking | pvck -v /dev/sdb1 Note: check the consistency of the LVM metadata |
change physical attributes | ## do not allow allocation of extents on this drive, however the partition must be in a vg otherwise you get an error pvchange -x n /dev/sdb1 Common Attributes that you may want to use:–addtag add a tag -x allowed to allocate extents -u change the uuid |
moving | pvmove -v /dev/sdb2 /dev/sdb3 Note: moves any used extents from this volume to another volume, in readiness to remove that volume. However you cannot use this on mirrored volumes, you must convert back to non-mirror using “lvconvert -m 0” |
Volume Groups | |
display | vgdisplay -v vgs -v vgs -a -o +devices vgs flags: #PV – number of physical devices #LV – number of configured volumes vgs attributes are: 1. permissions (r)|(w) 2. resi(z)eable 3. e(x)ported 4. (p)artial 5. allocation policy – (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited 6. (c)luster |
scanning | vgscan -v |
creating | vgcreate VolData00 /dev/sdb1 /dev/sdb2 /dev/sdb3 vgcreate VolData00 /dev/sdb[123] ## Use 32MB extent size vgcreate VolData00 -s 32 /dev/sdb1 Common Attributes that you may want to use: -l maximum logical volumes -p maximum physical volumes -s physical extent size (default is 4MB) -A autobackup |
extending | vgextend VolData00 /dev/sdb3 |
reducing | vgreduce VolData00 /dev/sdb3 vgreduce –removemissing –force VolData00 |
removing | vgremove VolData00 Common Attributes that you may want to use: -f force the removal of any logical volumes |
checking | vgck VolData00 Note: check the consistency of the LVM metadata |
change volume attributes | vgchange -a n VolData00 Common Attributes that you may want to use: -a control availability of volumes within the group -l maximum logical volumes -p maximum physical volumes -s physical extent size (default is 4MB) -x resizable yes or no (see VG status in vxdisplay) |
renaming | vgrename VolData00 Data_Vol_01 note: the volume group must not have any active logical volumes |
converting metadata type | vgconvert -M2 VolData00 Note: vgconvert allows you to convert from one type of metadata format to another for example from LVM1 to LVM2, LVM2 offers bigger capacity, clustering and mirroring |
merging | # the old volumes group will be merged into the new volume group vgmerge New_Vol_Group Old_Vol_Group Note: you must unmount any fielsystems and deactivate the vg that is being merged “vgchange -a n “, then you can activiate it again afterwards “vgchange -a y “, then perform a vgscan, dont forget to backup the configuration |
spliting | vgsplit Old_Vol_Group New_Vol_Group [physical volumes] [-n logical volume name] |
importing | vgimport VolData00 Common Attributes that you may want to use: -a import all exported volume groups |
exporting | ## to see if a volume has already been export use “vgs” and look at the third attribute should be a x vgexport VolData00 Common Attributes that you may want to use: -a export all inactive volume groups |
backing up | ## Backup to default location (/etc/lvm/backup) vgcfgbackup VolData00 # Backup to specific location vgcfgbackup -f /var/backup/VolData00_bkup VolData00# Backup to specific location all volume groups (notice the %s) vgcfgbackup -f /var/backup/vg_backups_%s Note: the backup is written in plain text and are by default located in /etc/lvm/backup |
restoring | vgcfgrestore -f /var/backup/VolData00_bkup VolData00 Common Attributes that you may want to use: -l list backups of file -f backup file -M metadataype 1 or 2 |
cloning | vgimportclone /dev/sdb1 Note: used to import and rename duplicated volume group |
special files | vgmknodes VolData00 Note: recreates volume group directory and logical volume special files in /dev |
Logical Volumes | |
display | lvdisplay -v lvdisplay –maps display mirror volumes lvs -v lvs -a -o +devices ## lvs commands for mirror volumes lvs -a -o +devices lvs -a -o +seg_pe_ranges –segments## Stripe size lvs -v –segments lvs -a -o +stripes,stripesize## use complex command lvs -a -o +devices,stripes,stripesize,seg_pe_ranges –segments lvs attributes are: 1. volume type: (m)irrored, (M)irrored without initail sync, (o)rigin, (p)vmove, (s)napshot, invalid (S)napshot, (v)irtual, mirror (i)mage mirror (I)mage out-of-sync, under (c)onversion 2. permissions: (w)rite, (r)ead-only 3. allocation policy – (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited 4. fixed (m)inor 5. state: (a)ctive, (s)uspended, (I)nvalid snapshot, invalid (S)uspended snapshot, mapped (d)evice present with-out tables, mapped device present with (i)nactive table 6. device (o)pen (mounted in other words) |
scanning | lvscan -v lvmdiskscan |
creating | ## plain old volume lvcreate -L 10M VolData00## plain old volume but use extents, use 10 4MB extents (if extent size is 4MB) lvcreate -l 10 VolData00 ## plain old volume but with a specific name web01 lvcreate -L 10M -n web01 VolData00 ## plain old volume but on a specific disk lvcreate -L 10M VolData00 /dev/sdb1 ## a striped volume called lvol1 (note the captial i for the stripe size), can use -l (extents) instead of -L lvcreate -i 3 -L 24M -n lvol1 vg01## Mirrored volume lvcreate -L 10M -m1 -n data01 vg01 ## Mirrored volume without a mirror log file lvcreate -L 10M -m1 –mirrorlog core -n data01 vg01 Common Attributes that you may want to use: -L size of the volume [kKmMgGtT] -l number of extents -C contiguous [y|n] -i stripes -I stripe size -m mirrors –mirrorlog -n volume name |
extending | lvextend -L 20M /dev/VolData00/vol01Common Attributes that you may want to use:-L size of the volume [kKmMgGtT] -l number of extents -C contiguous [y|n] -i stripes -I stripe size Note: you can extend a ext2/ext3 filesystem using the “resize2fs” or “fsadm” command fsadm resize /dev/VolData01/data01 resize2fs -p /dev/mapper/VolData01-data01 [size] The -p option displays bars of progress while extendingthe filesystem |
reducing/resizing | lvreduce -L 5M /dev/VolData00/vol01 lvresize -L 5M /dev/VolData00/vol01 Note: rounding will occur when extending and reducing volumes to the next extent (4MB by default), you can use resize2fs or fsadm to shrink the filesystemfsadm resize /dev/VolData01/data01 [size] resize2fs -p /dev/mapper/VolData01-data01 [size] |
removing | lvremove /dev/VolData00/vol01 |
adding a mirror to a non-mirrored volume | lvconvert -m1 –mirrorlog core /dev/VolData00/vol01 /dev/sdb2Note: you can also use the above command to remove a unwanted log |
removing a mirror from a mirrored volume | lvconvert -m0 /dev/VolData00/vol01 /dev/sdb2Note: the disk in the command is the one you want to remove |
Mirror a volume that has stripes | lvconvert –stripes 3 -m1 –mirrorlog core /dev/VolData00/data01 /dev/sdd1 /dev/sde1 /devsdf1 |
change volume attributes | lvchange -a n /dev/VolData00/vol01 Common Attributes that you may want to use:-a availability -C contiguous [y|n] |
renaming | lvrename /dev/VolData00/vol_old /dev/VolData00/vol_new |
snapshotting | lvcreate –size 100M –snapshot -name snap /dev/vg01/data01 |
Miscellaneous | |
Simulating a disk failure | dd if=/dev/zero of=/dev/sdb2 count=10 |
reparing a failed mirror no LVM corruption | ## check volume, persume /dev/sdb2 has failed lvs -a -o +devices # remove the failed disk from the volume (if not already done so) , this will convert volume into a non-mirrored volume vgreduce –removemissing –force VolData00 ## replace the disk physically, remember to partion it with type 8e fdisk /dev/sdb …….. ## add new disk to LVM pvcreate /dev/sdb2 ## add the disk back into volume group vgextend VolData00 /dev/sdb2 ## mirror up the volume lvconvert -m1 –mirrorlog core /dev/VolData00/vol02 /dev/sdb2 |
corrupt LVM metadata without replacing drive | # attempt to bring the volume group online vgchange -a y VolData00 # Restore the LVM configation vgcfgrestore VolData00 # attempt to bring the volume grou online vgchange -a y VolData00 # file system check e2fsck /dev/VolData00/data01 |
corrupt LVM metadata but replacing the faulty disk | # attempt to bring the volume group online but you get UUID conflict errors make note of the UUID number vgchange -a y VolData00 vgchange -a n VolData00 ## sometimes it my only be a logical volume problem lvchange -a y /dev/VolData00/web02 lvchange -a n /dev/Voldata00/web02 ## replace the disk physically, remember to partion it with type 8e fdisk /dev/sdb …….. # after replacing the faulty drive the disk must have the previuos UUID number or you can get it from /etc/lvm directory pvcreate –uuid <previous UUID number taken from above command> /dev/sdb2 # Restore the LVM configation vgcfgrestore VolData00 # attempt to bring the volume group online or logical volume vgchange -a y VolData00 lvchange -a y /dev/VolData00/web02 # file system check e2fsck /dev/VolData00/data01Note: if you have backed the volume group configuration you can obtain the UUID number in the backup file by default located in /etc/lvm/backup or running “pvs -v” |
ip command examples
The linux ip command is similar to ifconfig, but more powerful and is intended to be a replacement for it. With ip you have the advantage of performing several network administration tasks with only one command.
In this tutorial, we are going to discuss 15 most common uses for ‘ip’ command, so let’s get going.
1. Identify available network interface with ip command
If you run ip link show command it will list all available network interfaces on your server.
$ ip link show
Example output:
$ ip link show 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff
2. How to Check an IP Address
To get the depth information of your network interfaces like IP Address, MAC Address information, use the following command as shown below.
$ ip addr show
The output will display the currently assign IP configuration for all network interfaces.
$ ip addr show 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff inet 192.168.100.8/24 brd 192.168.100.255 scope global enp0s3 valid_lft forever preferred_lft forever inet 192.168.100.20/24 scope global secondary enp0s3:1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe64:ab7a/64 scope link valid_lft forever preferred_lft forever
3. How to Enable Network Interface
The “up” flag with interface name (enp0s3) enables a network interface. For example, the following command will activates the enp0s3 network interface.
$ ip link enp0s3 up Or $ sudo ip link set enp0s3 up
4. How to Disable Network Interface
The “down” flag with interface name disables a network interface. For example, the following command will De-activates the enp0s3 network interface.
$ ip link set enp0s3 down or $ sudo ip link set enp0s3 down
5.How to Assign a IP Address to Specific Interface
To assign IP address to interface, we will use:
$ sudo ip addr add 192.168.100.4/255.255.255.0 dev enp0s3
We can also set broadcast address to interface with ‘ip’ command. By default no broadcast address is set, so to set a broadcast address command is:
$ $ sudo ip addr add broadcast 192.168.100.255 dev enp0s3
We can also set standard broadcast address along with IP address by using the following command,
$ sudo ip addr add 192.168.100.4/24 brd + dev enp0s3
As shown in the above example, we can also use ‘brd’ in place on ‘broadcast’ to set broadcast ip address.
6. How to Remove an IP Address
The following command will remove an assigned IP address from the given interface:
$ sudo ip addr del 192.168.100.4/24 dev enp0s3
7. Adding an Alias for an interface
To add an alias i.e. assign more than one IP to an interface, execute below command:
$ sudo ip addr add 192.168.100.20/24 dev enp0s3 label enp0s3:1
Example outputs:
$ ip addr show enp0s3 2: enp0s3: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff inet 192.168.100.8/24 brd 192.168.100.255 scope global enp0s3 valid_lft forever preferred_lft forever inet 192.168.100.20/24 scope global secondary enp0s3:1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe64:ab7a/64 scope link valid_lft forever preferred_lft forever
8. Check routing table
Checking routing information shows us the route a packet will take to reach the destination. To check the network routing information, execute the following command:
$ ip route show
Example output:
$ ip route show default via 192.168.100.1 dev enp0s3 proto static 192.168.100.0/24 dev enp0s3 proto kernel scope link src 192.168.100.8
Suppose now that you have an IP address which you need to know the route packets will take. You can use route option as follows:
$ ip route get 192.168.100.4
9. Adding a static route
To change the default route, the ip command can be used as follows:
$ sudo ip route add default via 192.168.100.150/24
So now all network packets will travel via 192.168.100.150 as opposed to old default route. For changing the default route for a single interface & to make change route further, execute
$ sudo ip route add 10.0.0.2 via 192.168.100.150/24 dev enp0s3
10. Removing a static route
To remove assigned static route, simply type the following command.
$ sudo ip route del 10.0.0.2
11. How do i add default gateway
Default gateway can be specified globally or for in interface-specific config file. Advantage of default gateway is If we have more than one NIC is present in the system. You can add default gateway on the fly as shown below command.
sudo ip route add default via 192.168.100.1
12. Show network statistics
The ip command can also be used to show the statistics of the various network interfaces. To do this you can use the ip command with the option -s and then specify the network device.
$ ip -s link
When you need to get information about a particular network interface, add the option ls followed by the name of the network interface. The option -s when used more than once gives you more information about that particular interface. This can be very useful especially when trouble shooting errors in network connectivity.
ip -s -s link ls enp0s3
13.Checking ARP entries
Address Resolution Protocol (ARP) is used to translate an IP address to its corresponding physical address, commonly known as MAC address. With ip command you can view the MAC address of the devices connected in your LAN by using the option neigh or neighbour.
$ ip neigh
14. Monitor netlink messages
It is also possible to view netlink messages with ip command. The monitor option allows you to see the state of your network devices. For instance a computer on your LAN could be categorized as REACHABLE or STALE depending on its status. The command can be used as follows:
$ ip monitor all
Example output:
$ ip monitor all [NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 REACHABLE [NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 STALE [NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 PROBE [NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 REACHABLE
15. View help
If you want to find a option which is not listed in above examples, then you can look for manual.
$ man ip
Conclusion
The command ip is a must have tool for network administrators and all Linux users alike. It is time to move from ifconfig, especially when you are writing scripts.
show df results without snaps
add an alias:
alias df='df -x"squashfs"'
remove “@eaDir” folders from synology nas
ssh to nas, elevate to root
cd /volume1/video/
find . -name “@eaDir” -type d ## to see how many there are
find . -name “@eaDir” -type d -exec /bin/rm -rf {} + ## remove the folders
kvm libvirt copy to/from host
enable copy/paste to/from host:
sudo apt install spice-vdagent
downloading via command line with wget
/etc/wgetrc Default location of the global startup file. .wgetrc User startup file. #How to Download a Website Using wget
wget -r www.dlsite.com
#This downloads the pages recursively up to a maximum of 5 levels deep.
#Five levels deep might not be enough to get everything from the site. You can use the -l switch to set the number of levels you wish to go to as follows:
wget -r -l10 www.dlsite.com
#If you want infinite recursion you can use the following:
wget -r -l inf www.dlsite.com
# How to Download Certain File Types wget -A "*.mp3" -r
#The reverse of this is to ignore certain files. Perhaps you don't want to download executables. In this case, you would use the following syntax:
wget -R "*.exe" -r
#Other Parameters
-b, --background Go to background immediately after startup. If no output file is specified via the -o, output is redirected to wget-log.
-o logfile, --output-file=logfile Log all messages to logfile. The messages are normally reported to standard error.
-a logfile, --append-output=logfile Append to logfile. This option is the same as -o, only it appends to logfile instead of overwriting the old log file. If logfile does not exist, a new file is created.
-q, --quiet Turn off wget's output.
-v, --verbose Turn on verbose output, with all the available data. The default output is verbose.
-nv, --non-verbose Non-verbose output. Turn off verbose without being completely quiet (use -q for that), which means that error messages and basic information still get printed.
-i file, --input-file=file Read URLs from a local or external file. If "-" is specified as file, URLs are read from the standard input. (Use "./-" to read from a file literally named "-".)
-F, --force-html When input is read from a file, force it to be treated as an HTML file. This enables you to retrieve relative links from existing HTML files on your local disk, by adding to HTML, or using the --base command-line option.
-t number, --tries=number Set number of retries to number. Specify 0 or inf for infinite retrying. The default is to retry 20 times, with the exception of fatal errors like "connection refused'' or "not found'' (404), which are not retried.
-O file, --output-document=file The documents will not be written to the appropriate files, but all will be concatenated together and written to file.
-c, --continue Continue getting a partially-downloaded file. This option is useful when you want to finish up a download started by a previous instance of wget, or by another program. For instance: wget -c ftp://dlsite/filename
--progress=type Select the progress indicator you want to use. Legal indicators are "dot" and "bar".
-N, --timestamping Turn on time stamping. Output file will have timestamp matching remote copy; if file already exists locally, and remote file is not newer, no download will occur.
--no-use-server-timestamps Don't set the local file's timestamp by the one on the server.
-S, --server-response Print the headers sent by HTTP servers and responses sent by FTP servers.
--spider When invoked with this option, wget will behave as a web spider, which means that it will not download the pages, just check that they are there. For example, you can use wget to check your bookmarks: wget --spider --force-html -i bookmarks.html
-T seconds, --timeout=seconds Set the network timeout to seconds seconds. This option is equivalent to specifying --dns-timeout, --connect-timeout, and --read-timeout, all at the same time.
--dns-timeout=seconds Set the DNS lookup timeout to seconds seconds. DNS lookups that don't complete within the specified time will fail. By default, there is no timeout on DNS lookups, other than that implemented by system libraries.
--connect-timeout=seconds Set the connect timeout to seconds seconds. TCP connections that take longer to establish will be aborted. By default, there is no connect timeout, other than that implemented by system libraries.
--read-timeout=seconds Set the read (and write) timeout to seconds seconds. Reads that take longer will fail. The default value for read timeout is 900 seconds.
--limit-rate=amount Limit the download speed to amount bytes per second. The amount may be expressed in bytes, kilobytes (with the k suffix), or megabytes (with the m suffix). For example, --limit-rate=20k will limit the retrieval rate to 20 KB/s. This option is useful when, for whatever reason, you don't want wget to consume the entire available bandwidth.
-w seconds, --wait=seconds Wait the specified number of seconds between the retrievals. Use of this option is recommended, as it lightens the server load by making the requests less frequent. Instead of in seconds, the time can be specified in minutes using the m suffix, in hours using h suffix, or in days using d suffix.
--waitretry=seconds If you don't want wget to wait between every retrieval, but only between retries of failed downloads, you can use this option. wget will use linear backoff, waiting 1 second after the first failure on a given file, then waiting 2 seconds after the second failure on that file, up to the maximum number of seconds you specify. Therefore, a value of 10 will actually make wget wait up to (1 + 2 + ... + 10) = 55 seconds per file. By default, wget will assume a value of 10 seconds.
--random-wait Some websites may perform log analysis to identify retrieval programs such as wget by looking for statistically significant similarities in the time between requests. This option causes the time between requests to vary between 0 and 2*wait seconds, where wait was specified using the --wait option, to mask wget's presence from such analysis.
--no-dns-cache Turn off caching of DNS lookups. Normally, wget remembers the addresses it looked up from DNS so it doesn't have to repeatedly contact the DNS server for the same (typically small) set of addresses it retrieves. This cache exists in memory only; a new wget run will contact DNS again.
--retry-connrefused Consider "connection refused" a transient error and try again. Normally wget gives up on a URL when it is unable to connect to the site because failure to connect is taken as a sign that the server is not running at all and that retries would not help. This option is for mirroring unreliable sites whose servers tend to disappear for short periods of time.
--user=user, --password=password Specify the username user and password for both FTP and HTTP file retrieval. These parameters can be overridden using the --ftp-user and --ftp-password options for FTP connections and the --http-user and --http-password options for HTTP connections.
--ask-password Prompt for a password for each connection established. Cannot be specified when --password is being used, because they are mutually exclusive.
--unlink Force wget to unlink file instead of clobbering existing file. This option is useful for downloading to the directory with hardlinks.
-nd, --no-directories Do not create a hierarchy of directories when retrieving recursively. With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the file names will get extensions .n).
-x, --force-directories The opposite of -nd; create a hierarchy of directories, even if one would not have been created otherwise. For example, wget -x http://fly.srk.fer.hr/robots.txt will save the downloaded file to fly.srk.fer.hr/robots.txt.
-nH, --no-host-directories Disable generation of host-prefixed directories. By default, invoking wget with -r http://dlsite/ will create a structure of directories beginning with dlsite/. This option disables such behaviour.
--protocol-directories Use the protocol name as a directory component of local file names. For example, with this option, wget -r http://host will save to http/host/... rather than just to host/....
--cut-dirs=number Ignore number directory components. This option is useful for getting a fine-grained control over the directory where recursive retrieval will be saved.
--http-user=user, --http-passwd=password Specify the username user and password on an HTTP server. According to the challenge, wget will encode them using either the "basic" (insecure) or the "digest" authentication scheme.
--ignore-length Unfortunately, some HTTP servers (CGI programs, to be more precise) send out bogus "Content-Length" headers, which makes wget start to bray like a stuck pig, as it thinks not all the document was retrieved. You can spot this syndrome if wget retries getting the same document again and again, each time claiming that the (otherwise normal) connection has closed on the very same byte. With this option, wget ignores the "Content-Length" header, as if it never existed.
--private-key=file Read the private key from file. This option allows you to provide the private key in a file separate from the certificate.
--private-key-type=type Specify the type of the private key. Accepted values are PEM (the default) and DER.
-r, --recursive Turn on recursive retrieving. -l depth, --level=depth Specify recursion maximum depth level depth. The default maximum depth is 5.
-K, --backup-converted When converting a file, backup the original version with an .orig suffix. Affects the behavior of -N.
-m, --mirror Turn on options suitable for mirroring. This option turns on recursion and time-stamping, sets infinite recursion depth and keeps FTP directory listings. It is currently equivalent to -r -N -l inf -nr.
-p, --page-requisites This option causes wget to download all the files that are necessary to properly display a given HTML page. Including such things as inlined images, sounds, and referenced stylesheets. Ordinarily, when downloading a single HTML page, any requisite documents that may be needed to display it properly are not downloaded. Using -r together with -l can help, but since wget does not ordinarily distinguish between external and inlined documents, one is generally left with "leaf documents'' that are missing their requisites. -A acclist, --accept acclist; -R rejlist, --reject rejlist Specify comma-separated lists of file name suffixes or patterns to accept or reject. Note that if any of the wildcard characters, *, ?, [ or ], appear in an element of acclist or rejlist, it will be treated as a pattern, rather than a suffix. -D domain-list, --domains=domain-list Set domains to be followed. domain-list is a comma-separated list of domains. Note that it does not turn on -H. --exclude-domains domain-list Specify the domains that are not to be followed. --follow-ftp Follow FTP links from HTML documents. Without this option, wget will ignore all the FTP links.
create a linux swap file
How to add Swap File
Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G.
Create a file that will be used for swap:
sudo fallocate -l 1G /swapfile
If fallocate is not installed or if you get an error message saying fallocate failed: Operation not supported then you can use the following command to create the swap file:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Only the root user should be able to write and read the swap file. To set the correct permissions type:
sudo chmod 600 /swapfile
Use the mkswap utility to set up the file as Linux swap area:
sudo mkswap /swapfile
Enable the swap with the following command:
sudo swapon /swapfile
To make the change permanent open the /etc/fstab file and append the following line: /etc/fstab
/swapfile swap swap defaults 0 0
Copy
To verify that the swap is active, use either the swapon or the free command as shown below:
sudo swapon –show
NAME TYPE SIZE USED PRIO /swapfile file 1024M 507.4M -1
sudo free -h
total used free shared buff/cache available Mem: 488M 158M 83M 2.3M 246M 217M Swap: 1.0G 506M 517M
How to adjust the swappiness value
Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.
The default swappiness value is 60. You can check the current swappiness value by typing the following command:
cat /proc/sys/vm/swappiness
Output:60
While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.
For example, to set the swappiness value to 10, you would run:
sudo sysctl vm.swappiness=10
To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:
/etc/sysctl.conf
vm.swappiness=10
The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.
How to remove Swap File
If for any reason you want to deactivate and remove the swap file, follow these steps:
First, deactivate the swap by typing:
sudo swapoff -v /swapfile
Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
Finally, delete the actual swapfile file using the rm command:
sudo rm /swapfile
rename images with resolution
install exiftool using:
sudo apt install libimage-exiftool-perl
run with:
exiftool ‘-filename<%f_$imagesize.%e’ (images folder)
conkyrc for raspberry pi
background yes
use_xft yes
xftfont 123:size=8
xftalpha 0.1
update_interval 0.5
total_run_times 0
own_window yes
own_window_type normal
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes
minimum_size 250 5
maximum_width 400
draw_shades no
draw_outline no
draw_borders no
draw_graph_borders no
default_color gray
default_shade_color red
default_outline_color green
alignment top_right
gap_x 10
gap_y 10
no_buffers yes
uppercase no
cpu_avg_samples 2
net_avg_samples 1
override_utf8_locale no
use_spacer right
TEXT
${font Arial:size=20}${color Tan1}Raspberry Pi OS
${color orange}Hostname: ${color}${nodename}
${voffset -90}
${color DimGray}
${font}
${font Arial:bold:size=10}${color Tan1}SYSTEM ${color DarkSlateGray} ${hr 2}
$font${color DimGray}$sysname $kernel $alignr $machine
Frequency $alignr${freq_g cpu0}Ghz
Uptime $alignr${uptime}
File System $alignr${fs_type}
${font Arial:bold:size=10}${color Tan1}CPU ${color DarkSlateGray}${hr 2}
$font${color DimGray}Temp: $alignr ${exec /opt/vc/bin/vcgencmd measure_temp | cut -c6-9} C
$font${color DimGray}CPU1 ${cpu cpu1}% ${cpubar cpu1}
CPU2 ${cpu cpu2}% ${cpubar cpu2}
CPU3 ${cpu cpu3}% ${cpubar cpu3}
CPU4 ${cpu cpu4}% ${cpubar cpu4}
${cpugraph DimGray DarkSlateGray} $color
${font Arial:bold:size=10}${color Tan1}MEMORY ${color DarkSlateGray}${hr 2}
$font${color DimGray}MEM $alignc $mem / $memmax $alignr $memperc%
$membar
$font${color DimGray}SWAP $alignc $swap / $swapmax $alignr $swapperc%
$swapbar
${font Arial:bold:size=10}${color Tan1}HDD ${color DarkSlateGray}${hr 2}
$font${color DimGray}/home $alignc ${fs_used /home} / ${fs_size /home} $alignr ${fs_free_perc /home}%
${fs_bar /home}
${font Arial:bold:size=10}${color Tan1}TOP PROCESSES ${color DarkSlateGray}${hr 2}
${color DimGray}$font${top_mem name 2}${alignr}${top mem 2} %
$font${top_mem name 3}${alignr}${top mem 3} %
$font${top_mem name 4}${alignr}${top mem 4} %
$font${top_mem name 5}${alignr}${top mem 5} %
${font Arial:bold:size=10}${color Tan2}NETWORK ${color DarkSlateGray}${hr 2}
$font${color DimGray}IP on eth0 $alignr ${addr eth0}
Down $alignr ${downspeed eth0} kb/s
Up $alignr ${upspeed eth0} kb/s
Downloaded: $alignr ${totaldown eth0}
Uploaded: $alignr ${totalup eth0}
Ubuntu 20.04 Setting Background Colour
Newer versions of Ubuntu Desktop have removed the capability to set the background to a solid colour. You may run the following command in a terminal window to accomplish this task:
To clear the current image setting:
Newer versions of Ubuntu Desktop have removed the capability to set the background to a solid colour. You may run the following command in a terminal window to accomplish this task:
To clear the current image setting:
> gsettings set org.gnome.desktop.background picture-uri ""
To set the primary colour (the first colour in a gradient or the solid colour):
> gsettings set org.gnome.desktop.background primary-color '#000000'
To set the secondary colour (the second colour in a gradient, ignored for solid):
> gsettings set org.gnome.desktop.background secondary-color '#000000'
To set the background type (‘solid’ for one color, ‘horizontal’ or ‘vertical’ for gradient):
> gsettings set org.gnome.desktop.background color-shading-type 'solid'
Lenovo Thinkpad – Enable Touchpad Right-click on Ubuntu
To be able to enable Touchpad right-click on Ubuntu 18.04 running in Lenovo Thinkpad, you’ll need to install gnome-tweak-tool
which is a tool used to Tweak and customize Gnome Desktop Environment.
$ sudo apt install gnome-tweak-tool
Once launched, navigate to “Keyboard & Mouse”
Under “Mouse Click Emulation”, Select “Area”
Close Tweaks tool and you should have Right-click on your Lenovo Thinkpad laptop working.
systemctl / systemd commands
Starting and Stopping Services
To start a systemd
service, executing instructions in the service’s unit file, use the start
command:
- sudo systemctl start application.service
Since
systemd
knows to look for *.service
files for service management commands, so the command could just as easily be typed like this:
- sudo systemctl start application
To stop a currently running service, you can use the stop
command instead:
- sudo systemctl stop application.service
Restarting and Reloading
To restart a running service, you can use the restart
command:
- sudo systemctl restart application.service
If the application in question is able to reload its configuration files (without restarting), you can issue the reload
command to initiate that process:
- sudo systemctl reload application.service
If you are unsure whether the service has the functionality to reload its configuration, you can issue the reload-or-restart
command. This will reload the configuration in-place if available. Otherwise, it will restart the service so the new configuration is picked up:
- sudo systemctl reload-or-restart application.service
Enabling and Disabling Services
To start a service at boot, use the enable
command:
- sudo systemctl enable application.service
This will create a symbolic link from the system’s copy of the service file, usually in /lib/systemd/system
or /etc/systemd/system
) into the location on disk where systemd
looks for autostart files (usually /etc/systemd/system/some_target.target.wants
.
To disable the service from starting automatically, you can type:
- sudo systemctl disable application.service
This will remove the symbolic link.
Enabling and Starting Services together
To enable and start a service in one command, use
- sudo systemctl enable application.service –now
Checking the Status of Services
To check the status of a service on your system, you can use the status
command:
- systemctl status application.service
There are also methods for checking for specific states. For instance, to check to see if a unit is currently active (running), you can use the is-active
command:
- systemctl is-active application.service
This will return the current unit state, which is usually active
or inactive
. The exit code will be “0” if it is active, making the result simpler to parse in shell scripts.
To see if the unit is enabled, you can use the is-enabled
command:
- systemctl is-enabled application.service
This will output whether the service is enabled
or disabled
and will again set the exit code to “0” or “1” depending on the answer to the command question.
A third check is whether the unit is in a failed state. This indicates that there was a problem starting the unit in question:
- systemctl is-failed application.service
This will return active
if it is running properly or failed
if an error occurred. If the unit was intentionally stopped, it may return unknown
or inactive
. An exit status of “0” indicates that a failure occurred and an exit status of “1” indicates any other status.
System State Overview
The commands so far have been useful for managing single services, but they are not very helpful for exploring the current state of the system. There are a number of systemctl
commands that provide this information.
Listing Current Units
To see a list of all of the active units that systemd
knows about, we can use the list-units
command:
- systemctl list-units
This will show you a list of all of the units that systemd
currently has active on the system. The output will look something like this:
Output
UNIT LOAD ACTIVE SUB DESCRIPTION
atd.service loaded active running ATD daemon
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
dbus.service loaded active running D-Bus System Message Bus
dcron.service loaded active running Periodic Command Scheduler
dkms.service loaded active exited Dynamic Kernel Modules System
getty@tty1.service loaded active running Getty on tty1
. . .
The output has the following columns:
- UNIT: The
systemd
unit name - LOAD: Whether the unit’s configuration has been parsed by
systemd
. The configuration of loaded units is kept in memory. - ACTIVE: A summary state about whether the unit is active. This is usually a fairly basic way to tell if the unit has started successfully or not.
- SUB: This is a lower-level state that indicates more detailed information about the unit. This often varies by unit type, state, and the actual method in which the unit runs.
- DESCRIPTION: A short textual description of what the unit is/does.
Since the list-units
command shows only active units by default, all of the entries above will show loaded
in the LOAD column and active
in the ACTIVE column. This display is actually the default behavior of systemctl
when called without additional commands, so you will see the same thing if you call systemctl
with no arguments:
- systemctl
We can tell systemctl
to output different information by adding additional flags. For instance, to see all of the units that systemd
has loaded (or attempted to load), regardless of whether they are currently active, you can use the --all
flag, like this:
- systemctl list-units –all
This will show any unit that systemd
loaded or attempted to load, regardless of its current state on the system. Some units become inactive after running, and some units that systemd
attempted to load may have not been found on disk.
You can use other flags to filter these results. For example, we can use the --state=
flag to indicate the LOAD, ACTIVE, or SUB states that we wish to see. You will have to keep the --all
flag so that systemctl
allows non-active units to be displayed:
- systemctl list-units –all –state=inactive
Another common filter is the --type=
filter. We can tell systemctl
to only display units of the type we are interested in. For example, to see only active service units, we can use:
- systemctl list-units –type=service
Listing All Unit Files
The list-units
command only displays units that systemd
has attempted to parse and load into memory. Since systemd
will only read units that it thinks it needs, this will not necessarily include all of the available units on the system. To see every available unit file within the systemd
paths, including those that systemd
has not attempted to load, you can use the list-unit-files
command instead:
- systemctl list-unit-files
Units are representations of resources that systemd
knows about. Since systemd
has not necessarily read all of the unit definitions in this view, it only presents information about the files themselves. The output has two columns: the unit file and the state.
Output
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount static
var-lib-nfs-rpc_pipefs.mount static
org.cups.cupsd.path enabled
. . .
The state will usually be enabled
, disabled
, static
, or masked
. In this context, static means that the unit file does not contain an install
section, which is used to enable a unit. As such, these units cannot be enabled. Usually, this means that the unit performs a one-off action or is used only as a dependency of another unit and should not be run by itself.
We will cover what masked
means momentarily.
Unit Management
So far, we have been working with services and displaying information about the unit and unit files that systemd
knows about. However, we can find out more specific information about units using some additional commands.
Displaying a Unit File
To display the unit file that systemd
has loaded into its system, you can use the cat
command (this was added in systemd
version 209). For instance, to see the unit file of the atd
scheduling daemon, we could type:
- systemctl cat atd.service
Output
[Unit]
Description=ATD daemon
[Service]
Type=forking
ExecStart=/usr/bin/atd
[Install]
WantedBy=multi-user.target
The output is the unit file as known to the currently running systemd
process. This can be important if you have modified unit files recently or if you are overriding certain options in a unit file fragment (we will cover this later).
Displaying Dependencies
To see a unit’s dependency tree, you can use the list-dependencies
command:
- systemctl list-dependencies sshd.service
This will display a hierarchy mapping the dependencies that must be dealt with in order to start the unit in question. Dependencies, in this context, include those units that are either required by or wanted by the units above it.
Output
sshd.service
├─system.slice
└─basic.target
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
. . .
The recursive dependencies are only displayed for .target
units, which indicate system states. To recursively list all dependencies, include the --all
flag.
To show reverse dependencies (units that depend on the specified unit), you can add the --reverse
flag to the command. Other flags that are useful are the --before
and --after
flags, which can be used to show units that depend on the specified unit starting before and after themselves, respectively.
Checking Unit Properties
To see the low-level properties of a unit, you can use the show
command. This will display a list of properties that are set for the specified unit using a key=value
format:
- systemctl show sshd.service
Output
Id=sshd.service
Names=sshd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=syslog.target network.target auditd.service systemd-journald.socket basic.target system.slice
Description=OpenSSH server daemon
. . .
If you want to display a single property, you can pass the -p
flag with the property name. For instance, to see the conflicts that the sshd.service
unit has, you can type:
- systemctl show sshd.service -p Conflicts
Output
Conflicts=shutdown.target
Masking and Unmasking Units
We saw in the service management section how to stop or disable a service, but systemd
also has the ability to mark a unit as completely unstartable, automatically or manually, by linking it to /dev/null
. This is called masking the unit, and is possible with the mask
command:
- sudo systemctl mask nginx.service
This will prevent the Nginx service from being started, automatically or manually, for as long as it is masked.
If you check the list-unit-files
, you will see the service is now listed as masked:
- systemctl list-unit-files
Output
. . .
kmod-static-nodes.service static
ldconfig.service static
mandb.service static
messagebus.service static
nginx.service masked
quotaon.service static
rc-local.service static
rdisc.service disabled
rescue.service static
. . .
If you attempt to start the service, you will see a message like this:
- sudo systemctl start nginx.service
Output
Failed to start nginx.service: Unit nginx.service is masked.
To unmask a unit, making it available for use again, use the unmask
command:
- sudo systemctl unmask nginx.service
This will return the unit to its previous state, allowing it to be started or enabled.
Editing Unit Files
While the specific format for unit files is outside of the scope of this tutorial, systemctl
provides built-in mechanisms for editing and modifying unit files if you need to make adjustments. This functionality was added in systemd
version 218.
The edit
command, by default, will open a unit file snippet for the unit in question:
- sudo systemctl edit nginx.service
This will be a blank file that can be used to override or add directives to the unit definition. A directory will be created within the /etc/systemd/system
directory which contains the name of the unit with .d
appended. For instance, for the nginx.service
, a directory called nginx.service.d
will be created.
Within this directory, a snippet will be created called override.conf
. When the unit is loaded, systemd
will, in memory, merge the override snippet with the full unit file. The snippet’s directives will take precedence over those found in the original unit file.
If you wish to edit the full unit file instead of creating a snippet, you can pass the --full
flag:
- sudo systemctl edit –full nginx.service
This will load the current unit file into the editor, where it can be modified. When the editor exits, the changed file will be written to /etc/systemd/system
, which will take precedence over the system’s unit definition (usually found somewhere in /lib/systemd/system
).
To remove any additions you have made, either delete the unit’s .d
configuration directory or the modified service file from /etc/systemd/system
. For instance, to remove a snippet, we could type:
- sudo rm -r /etc/systemd/system/nginx.service.d
To remove a full modified unit file, we would type:
- sudo rm /etc/systemd/system/nginx.service
After deleting the file or directory, you should reload the systemd
process so that it no longer attempts to reference these files and reverts back to using the system copies. You can do this by typing:
- sudo systemctl daemon-reload
Adjusting the System State (Runlevel) with Targets
Targets are special unit files that describe a system state or synchronization point. Like other units, the files that define targets can be identified by their suffix, which in this case is .target
. Targets do not do much themselves, but are instead used to group other units together.
This can be used in order to bring the system to certain states, much like other init systems use runlevels. They are used as a reference for when certain functions are available, allowing you to specify the desired state instead of the individual units needed to produce that state.
For instance, there is a swap.target
that is used to indicate that swap is ready for use. Units that are part of this process can sync with this target by indicating in their configuration that they are WantedBy=
or RequiredBy=
the swap.target
. Units that require swap to be available can specify this condition using the Wants=
, Requires=
, and After=
specifications to indicate the nature of their relationship.
Getting and Setting the Default Target
The systemd
process has a default target that it uses when booting the system. Satisfying the cascade of dependencies from that single target will bring the system into the desired state. To find the default target for your system, type:
- systemctl get-default
Output
multi-user.target
If you wish to set a different default target, you can use the set-default
. For instance, if you have a graphical desktop installed and you wish for the system to boot into that by default, you can change your default target accordingly:
- sudo systemctl set-default graphical.target
Listing Available Targets
You can get a list of the available targets on your system by typing:
- systemctl list-unit-files –type=target
Unlike runlevels, multiple targets can be active at one time. An active target indicates that systemd
has attempted to start all of the units tied to the target and has not tried to tear them down again. To see all of the active targets, type:
- systemctl list-units –type=target
Isolating Targets
It is possible to start all of the units associated with a target and stop all units that are not part of the dependency tree. The command that we need to do this is called, appropriately, isolate
. This is similar to changing the runlevel in other init systems.
For instance, if you are operating in a graphical environment with graphical.target
active, you can shut down the graphical system and put the system into a multi-user command line state by isolating the multi-user.target
. Since graphical.target
depends on multi-user.target
but not the other way around, all of the graphical units will be stopped.
You may wish to take a look at the dependencies of the target you are isolating before performing this procedure to ensure that you are not stopping vital services:
- systemctl list-dependencies multi-user.target
When you are satisfied with the units that will be kept alive, you can isolate the target by typing:
- sudo systemctl isolate multi-user.target
Using Shortcuts for Important Events
There are targets defined for important events like powering off or rebooting. However, systemctl
also has some shortcuts that add a bit of additional functionality.
For instance, to put the system into rescue (single-user) mode, you can just use the rescue
command instead of isolate rescue.target
:
- sudo systemctl rescue
This will provide the additional functionality of alerting all logged in users about the event.
To halt the system, you can use the halt
command:
- sudo systemctl halt
To initiate a full shutdown, you can use the poweroff
command:
- sudo systemctl poweroff
A restart can be started with the reboot
command:
- sudo systemctl reboot
These all alert logged in users that the event is occurring, something that only running or isolating the target will not do. Note that most machines will link the shorter, more conventional commands for these operations so that they work properly with systemd
.
For example, to reboot the system, you can usually type:
- sudo reboot
Tmux Tips
Creating Tmux sessions
To create a new Tmux session and attach to it, run the following command from the Terminal:
$ tmux
Or,
$ tmux new
Detaching from Tmux sessions
To detach from a current Tmux session, just press Ctrl+b
and d
. You don’t need to press this both Keyboard shortcut at a time. First press "Ctrl+b"
and then press "d"
.
Once you’re detached from a session, you will see an output something like below.
[detached (from session 0)]
Creating named sessions
If you use multiple sessions, you might get confused which programs are running on which sessions. In such cases, you can just create named sessions.
For example if you wanted to perform some activities related to web server in a session, just create the Tmux session with a custom name, for example “webserver” (or any name of your choice).
$ tmux new -s webserver
List Tmux sessions
To view the list of open Tmux sessions, run:
$ tmux ls
Attaching to Tmux sessions
You can attach to the last created session by running this command:
$ tmux attach
Or,
$ tmux a
If you want to attach to any specific named session, for example “myoffice”, run:
$ tmux attach -t myoffice
Or, shortly:
$ tmux a -t myoffice
Kill Tmux sessions
When you’re done and no longer required a Tmux session, you can kill it at any time with command:
$ tmux kill-session -t myoffice
To kill when attached, press Ctrl+b
and x
. Hit "y"
to kill the session.
You can verify if the session is closed with tmux ls
command.
To Kill Tmux server along with all Tmux sessions, run:
$ tmux kill-server
Be careful! This will terminate all Tmux sessions even if there are any running jobs inside the sessions without any warning.
When there were no running Tmux sessions, you will see the following output:
$ tmux ls no server running on /tmp/tmux-1000/default
Split Tmux Session Windows
Tmux has an option to split a single Tmux session window into multiple smaller windows called Tmux panes.
This way we can run different programs on each pane and interact with all of them simultaneously.
Each pane can be resized, moved and closed without affecting the other panes. We can split a Tmux window either horizontally or vertically or both at once.
Split panes horizontally
To split a pane horizontally, press Ctrl+b
and "
(single quotation mark).
Split panes vertically
To split a pane vertically, press Ctrl+b
and %
.
Split panes horizontally and vertically
We can also split a pane horizontally and vertically at the same time. Take a look at the following screenshot.
First, I did a horizontal split by pressing Ctrl+b
"
and then split the lower pane vertically by pressing Ctrl+b %
.
As you see in the above screenshot, I am running three different programs on each pane.
Switch between panes
To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).
Send commands to all panes
In the previous example, we run three different commands on each pane. However, it is also possible to run send the same commands to all panes at once.
To do so, press Ctrl+b
and type the following command and hit ENTER
:
:setw synchronize-panes
Now type any command on any pane. You will see that the same command is reflected on all panes.
Swap panes
To swap panes, press Ctrl+b
and o
.
Show pane numbers
Press Ctrl+b
and q
to show pane numbers.
Kill panes
To kill a pane, simply type exit
and ENTER
key. Alternatively, press Ctrl+b
and x
. You will see a confirmation message. Just press "y"
to close the pane.
Zoom in and Zoom out Tmux panes
Now, I am going to zoom the left pane which has top command running. To do so, switch to left pane and press and z. The tmux prefix command is Ctrl+b, so I type Ctrl+b followed by z to zoom the pane. It is that simple!
Note: To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).
Autostart Tmux Session On Remote System When Logging In Via SSH
To autostart Tmux session when connecting via SSH, edit your remote system’s ~/.bash_profile file:
$ nano ~/.bash_profile
If the file is not available, just create it.
And add the following lines in it:
if [ -z "$TMUX" ]; then tmux attach -t default || tmux new -s default fi
Save and close the file. Log out and log back into the remote systems. You will be landed into a new Tmux session named ‘default’.
Now, start any process inside the Tmux session and detach it from by pressing Ctrl+b followed by d. The process will keep running inside the Tmux session even after the network connection is dropped. Once the network connect is back, SSH into the remote system and you will see that the job is still running!!
Toggling Status Line
Turn it off:
$ tmux set-option status off
And, turn it on:
$ tmux set-option status on
You can also bind a key (t in this example) to toggle the status line on and off, as follows:
$ tmux bind-key t set-option status
Open a terminal and use the following command:
# tmux
There’s a difference between Session vs Window vs Pane (split) in tmux
Windows:
Create windows using:
[ctrl + b] + c
Switching windows in a tmux session
Switch using window list
Another option uses [ctrl+b] + w
Name your current window using:
[ctrl+b] + ,
Name your tmux session using:
tmux new -s sessionname
If you already created a session, rename it using:
[ctrl+b] + $
Detach a session (come out of tmux session but keep it running in background)
use:
[ctrl + b] + d
List tmux sessions:
tmux ls
Reenter a session in Tmux (also known as attach)
tmux attach -t session_name_or_number
Creating multiple panes in tmux
Horizontal (Left/Right) | Vertical (Up/Down) |
[ctrl + b] + % | [ctrl + b] + “ |
Switching between panes
[ctrl+b] + arrow keys
Zoom into or out of a pane
[ctrl+b] + z
Killing or closing panes
[ctrl + b] + x
Resize panes
This assumes that you’ve hit ctrl + b and : to get to the command prompt
- :resize-pane -L
Here are some additional tmux pane resizing examples:
:resize-pane -D (Resizes the current pane down)
:resize-pane -U (Resizes the current pane upward)
:resize-pane -L (Resizes the current pane left)
:resize-pane -R (Resizes the current pane right)
:resize-pane -D 10 (Resizes the current pane down by 10 cells)
:resize-pane -U 10 (Resizes the current pane upward by 10 cells)
:resize-pane -L 10 (Resizes the current pane left by 10 cells)
:resize-pane -R 10 (Resizes the current pane right by 10 cells)
Killing sessions in tmux
tmux kill-session -t session_name_or_number
Kill all tmux sessions
tmux kill-server
Command | Explanation |
tmux kill-session -t name | Kill the mentioned session |
tmux kill-session -a | Kill all sessions except the last used session |
tmux kill-server | Kill all tmux sessions |
tmux attach -t name | Attach/enter the mentioned session |
[Ctrl +b] + d | Detach/exit the current session |
[Ctrl +b] + $ | Name or rename current session |
[Ctrl +b] + c | Create new window |
[Ctrl +b] + , | Name or rename current window |
[Ctrl +b] + w | List all windows |
[Ctrl +b] + & | Kill the current window |
[Ctrl +b] + n/p/N | Move to next/previous/Nth window |
[Ctrl +b] + % | Create horizontal pane |
[Ctrl +b] + “ | Create vertical pane |
[Ctrl +b] + space key | Toggle the layout of current pane |
[Ctrl +b] + z | Zoom into the current pane |
[Ctrl +b] + x | Kill the current pane |
Ansible: fix python3 targets
If you’re using Ansible >2.2.0, you can set the ansible_python_interpreter configuration option to /usr/bin/python3:
ansible my_ubuntu_host -m ping -e ‘ansible_python_interpreter=/usr/bin/python3’
or in your inventory file:
[ubuntu_hosts]
hostname1
hostname2
[ubuntu_hosts:vars]
ansible_python_interpreter=/usr/bin/python3
using the ‘find’ command
Using the find command
find ~/bin -iname filename
or:
find ~/bin -iname “filen*”
Moving large number of files
find /source/directory -mindepth 1 -maxdepth 1 -name ‘*’ -print0 | xargs -0 mv -t /target/directory;
using find with rename recursively
find . -depth -exec rename -v ‘s!\texttoremove/newtext/’ {} +
you may have to run it twice if the subfolders get renamed in the process
or:
find . -type f -iname “Ghost*” -exec rename -v ‘s/S02\ E/S02E/g’ {} \;
move files from subfolders to parent folder
Move to target folder and execute:
find . -mindepth 2 -type f -print -exec mv {} . \;
Delete empty directories
find . -empty -type d -delete
Delete empty files
find . -empty -type f -delete
Command
find /tmp -name “foo.txt” Find a file a called foo.txt in /tmp
find /tmp -iname “foo.txt” Find a file (case insensitive) called foo.txt in /tmp
find /tmp -name “foo*” Find a file starting with the substring foo
find /tmp -regex “.*f.*t” Find regex pattern (regex must include the full path)
Time
-mtime -7 Modified within the last 7 days
-mtime +1 -mtime -7Modified more than 1 day ago, but no more than 7
-daystart Start from today rather than from 24 hours ago
Recursion
-maxdepth 2 Go no more than 2 subdirectories deep during search
-mindepth 4Ignore results that are less than 4 subdirectories deep
-mount -xdev Don’t search directories contained on another filesystem
File type
find ~ -type d -iname “foo” Find a directory in ~ called foo
d directory l symlink p named pipe (FIFO)
f file s socket b block (buffered) special
c character (unbuffered) special D Door (GNU find on Solaris)
Other attributes
-uid 1000User ID is 1000
-user tux User name is tux
-writable -readable File is writable, readable
-perm u=rwx -perm 700Permissions are exactly 700
-perm -u+w,g+w -perm -220User or group has write permission –
-perm /a+w -perm /222 At least one permission is set to write /
-size +5M File is larger than 5 MB
-true Always true
Actions
-exec grep foo {} \; Execute grep on each file found
-ok sed ‘s/foo/bar/g’ {} \; Prompt user to execute sed on each file found
-execdir chmod 700 {} \; Run chmod (in subdirectory of result) on each file found
-fprint Add a newline to output -fprint0Do not add a newline
-ls Print results in ls -dils format
-fls output.txt Write results, in ls -dils frormat, to output.txt
-fprint -fprint0Write output to out.txt …with no newline
-prune Don’t descend into subdirectories
-quit Quit (usually used after other actions)
Find multiple files in Linux
The find
command is used in various ways. One thing you don’t want to do as a system administrator is work harder than you need to. Instead of running the same command to search for one file over and over, you can use the find
command to locate multiple files at the same time.
sudo find /home -type f -name file.txt -exec {} \;
This one-liner can be broken down. I find it best almost to read it as a sentence:
- searching the
/home
directory - searching for a file (
-type f
) or a directory (-type d
) - filename is file.txt (
-name file.txt
) - executing another command from the previous output
Find large files in Linux
You can also use find
to discover large files in Linux. Finding large files has proven helpful to me in the long run. find
can help to locate large files quickly, such as backups and ISO files.
sudo find / -type f -size +500000k -exec ls -lh {} \;
This one-liner can be broken down:
- searching the
/
directory - searching for a file (
type -f
) - searching for a file larger than
500000k
- executing the command
ls -lh
on the files found in the previous output
Find specific file types in Linux
Another good method is to locate file extensions using the find
command. I find this helpful, as it has shown me ways of finding specific files with only a specific keyword. In this case, the example below is looking for files that only contain a specific extension:
sudo find / -type f \( -name "*.sh" -o -name "*.txt" )
To dissect this:
- searching in the
/
directory - searching for a file (
-type f
) or a directory (-type d
) - searching for a file name that is a wildcard but ends with the extension
.sh
or.txt
Find modified files in Linux
The last example shows how to find
a file modified in the last 50 days. This can be helpful when you need to locate recently modified files due to a security reason or if there are unwanted users on the network accessing other files.
sudo find / -type f -ctime +50 -exec rm -f {} \;
The command above shows:
- searching in the
/
directory - searching for a file (
-type f
) or a directory (-type d
) - searching for files older than
50
days - executing the command
rm -f
on the files found in the previous output
This can help remove those malicious files all in one go. You just have to make sure that the files you select are the files you want to remove. One way to check is to run the command without the exec
section to see the files that come up in the output. If there are a large number of files, redirect the output into a file:
find / -type f -ctime +50 > files.txt
The content can be reviewed and verified before you run a one-liner that removes the /etc
folder. Not ideal.
check out: find . -criteria etc -print0 | xargs -0 command (might be safer than -exec stuff)
List Symlinks
sudo find / -type l
Finding files by size
find -size +1G -ls 2>/dev/nullthe +1G means “larger than a gigabyte”
Finding files by inode number
find -inum 919674 -ls 2>/dev/null919674 is the inode number
Finding files with a specific file owner or group
find /home -user bob -name “*.png”-lsfind /tmp -group admins -ls
Finding files with no owners or groups
find /tmp -nouser -ls
Finding files by last update time
find /home/bob -mtime -1
Finding files by when permissions were last changed
find . -ctime -1 -ls
Finding files based on last access times
find -name “*.pdf” -atime -2
Finding files based on their age relative to another file
find . -newer dig1 -ls
Finding files by type
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
find . -type l -ls
Limiting how deeply find should look
The -mindepth and -maxdepth options control how deeply into the file system the search will look (from the current location or starting point).
find -maxdepth 3 -name “*loop”
Finding files only if empty
find . -maxdepth 2 -empty -type f -ls
Finding files by permissions
find -perm 777 -type f -ls
Using find to help you get rid of files
find . -name filename -exec rm {} \;
The {} represents the name of each of the files located by the search criteria.
replace -exec with -ok if you want it to ask for a confirmation before it removes any file.
find . -name runme -ok rm -rf {} \;
disable HDMI audio on Radeon video cards
sudo vim edit /etc/pulse/default.pa
comment out “load-module module-switch-on-port-available”
extract subtitles from mkv
Download mkvtoolnix from:
https://mkvtoolnix.download/downloads.html
You can use mkvmerge (from mkvtoolnix) to inspect the file to see what the ID of the subtitles data will be:
mkvmerge -i input.mkv
This will list the tracks, for example like this:
File 'input.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AAC)
Track ID 3: subtitles (S_TEXT/UTF8)
Based on the ID of the track, call the following command, where is the one you identified above. You can use any srt filename that you want.
mkvextract tracks input.mkv :.srt
So, in our case, that would have been:
mkvextract tracks input.mkv 3:subs.srt
Using ‘dd’ to write to an SD card
Use ‘lsblk’ to determine which drive is the SD card.
To copy an iso or img file to the SD card, use:
sudo dd bs=4M if=2021-10-30-raspios-bullseye-armhf-full.img of=/dev/sdd status=progress oflag=sync
To backup an SD card to a file, use:
sudo dd bs=4M if=/dev/sdd of=sd_backup.iso status=progress oflag=sync
We’re back again…
After a long stretch of not cleaning up the posts, I really cleaned them up.. Be ready for all new stuff!