Bash
GitBook docs section concerning Bash context.
Boot Script Linux
Using init.d
In the script you put in /etc/init.d
you have to set it executable and create a symlink to /etc/rc.d/
:
Please note that this wil not work as the script have to be LSB compliant (provide, at least, the following actions: start, stop restart, force-reload and status).
Using systemd
File: /etc/systemd/system/my-startup.service
Public IP
Using dig command (Unix only)
Open the Terminal application.
Type the following dig (domain information groper) command on a Linux, OS X, or Unix-like operating systems to see public IP address assigned by the ISP:
Or:
You should see your IP address on screen. This is the fastest way to find out your IP address without using 3rd party site.
Using 3rd party web-sites
Open the Terminal application.
Type the following curl command to see public IP address assigned by the ISP:
Or:
You should see your IP address on screen.
Static IP Linux
Using NETPLAN
Here’s the replacement for editing /etc/networking/*
in the old system. The whole system now uses YAML configuration files under /etc/netplan
, and then the netplan command applies those configurations to the system.
So now (whether you’re running an older system or a new one) you should now have a static IP address!
Using Network Interfaces
Assign Static IP Address to eth0
interface editing configuration file /etc/network/interfaces
to make permanent changes as shown below:
Next, restart network services after entering all the details using the following command:
Using dhcpcd.conf file
Open /etc/dhcpcd.conf
with your favorite text editor. scroll down until you see the following commented out lines of code:
Uncomment the lines above or copy the following snippet and put it right under the code shown above:
You will need to set up the static ip_address and static routers tag to fit your network setup. Don’t forget to save the file.
Postfix Linux & Gmail SMTP
Configuring POSTFIX on Linux and Gmail SMTP for sending mail from command line.
Let's start with installing Postfix, Mailutils and a series of libraries:
as General Type of Mail Configuration we'll choose SATELLITE
as System Mail Name we can accept the proposed one or choose one not your domain
as SMTP RELAY HOST we'll put smtp.gmail.com:587
After the installation we must go to configure the SASL authentication part. With a text editor we will edit the
/etc/postfix/main.cf
Postfix file. Specifically at the bottom of the file we will add the following lines:Now we will have to create the file where to put the Gmail SMTP access password. We create the
/etc/postfix/sasl/sasl_password
file inside which we will put a single row:We create the lookup table db from postfix itself:
As they are sensitive data, although they will be used encrypted, the file could be read, so let's make sure that only the users root and postfix can handle it:
We restart the POSTFIX service to apply all the changes:
At this point we can send an email from our system to verify the proper functioning:
Any problems can be analyzed in
/var/log/mail.log
and/var/log/mail.err
.
WGET Download Site
Web site full download with WGET command.
How to find website's DNS address
To check the current nameservers (DNS) for any domain name from a Linux or Unix/macOS command line:
Open the Terminal application
Type
host -t ns domain-name-here
to print the current DNS servers of a domain OR Typedig ns domain-name-here
commandto find the delegation path from the root name servers type
dig +trace domain-name-here
Above Linux or Unix command will show you detailed information about the domain name, including its nameservers (DNS).
Open file with specific app from terminal (macOS)
To open file with specific application from macOS terminal, execute the command below:
Note: pay attention to the application name that has to be the same as appear in the Application folder (respecting case and spaces format).
Tip: to launch only the application, type the command above without filename.
Uninstall commands on Linux
As the man apt-get
page says:
remove - remove is identical to install execpt that packages are removed instead of installed. Note that removing a package leave its configuration files on the system. If a plus sign is appended to the package name (with no intervening space), the identified package will be installed instead of removed.
purge - purge is identical to remove except that packages are removed and purged (any configuration files are deleted too).
This of course, does not apply to packages that hold configuration files inside the user's home folder, these files will not be touched.
So for example, if you were to purge
Firefox or any other that holds some configuration files inside your /home
folder, these configuration files will stay there.
On the other hand if you were to install Apache, MySQL or any other services similar that save their configuration files in /etc
, these configuration files will be deleted in you use purge
.
Crontab Linux
Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
Crontab Commands
export EDITOR=vi;
to specify an editor to open crontab file.
crontab -e Edit crontab file, or create one if it doesn’t already exist.
crontab -l Crontab list of cronjobs , display crontab file contents.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
Crontab file syntax
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
Example
GitHub Repository Quick Setup
First it is possible to create and setup all from the GitHub website, in the user area, Your repositories
section.
...or create a new repository on the command line
...or push an existing repository from the command line
GitHub set remote origin
Commands to set the GitHub remote origin for a local git repository
How to Securely Copy Files in Linux | scp Command
scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol
. SCP is known for its simplicity, security, and pre-installed availability.
Syntax of scp
command
scp
commandIn this syntax:
options: These are various options that modify the behavior of the SCP command, such as -i for specifying an identity file, -l for limiting bandwidth, -o for specifying SSH options, -P for specifying a custom SSH port, and -S for specifying a program to use for the encrypted connection.
[[user@]host1:]source_file_or_directory: This represents the source file or directory. It can be local or on a remote machine specified by user@host1:.
...: This indicates that you can specify multiple source files or directories.
[[user@]host2:]destination: This is the destination where the files or directories will be copied. It can be local or on a remote machine specified by user@host2:.
Options in scp
command
scp
commandMost commonly used Options in scp
command
-P
port: Specifies the port to connect on the remote host.
-p
Preserves modification times, access times, and modes from the original file.
-q
Disables the progress meter.
-r
Recursively copy entire directories.
-s
Name of program to use for the encrypted connection. The program must understand ssh(1) options.
Wifi Networks Scan in Linux | nmcli Command
nmcli is a command-line tool for controlling NetworkManager and reporting network status. It can be utilized as a replacement for nm-applet or other graphical clients. nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status. To perform a wifi networks scan use the command below:
Example result:
How to check used ports on Linux
To check the listening ports and applications on Linux:
Open a terminal shell
Run any one of the following command on Linux to see open ports:
For the latest verion of Linux use the
ss
command. For exampless -tulw
.
Manage Git file tracking
.gitignore
will prevent untracked files from being added (without an add -f
) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.
To stop tracking a file, you need to remove it from the index. This can be achieved with this command.
If you want to remove a whole folder, you need to remove all files in it recursively.
The removal of the file from head revision will happen in the next commit.
WARNING: while this will not remove the physical file from your local machine, it will remove the files from other developers' machines on their next git pull
.
KEYMAP Linux Keyboard Layout
A persistent keymap can be set in /etc/vconsole.conf
, which is read by systemd on start-up. The KEYMAP
varible is used for specifying the keymap. If the variable is empty or not set, the us
keymap is used as default value. For example:
For convenience, localectl may be used to set the console keymap. It will change the KEYMAP
variable in /etc/vconsole.conf
and also se the keymap for the current session:
The --no-convert
option can be used to prevent localectl
from automatically changing the Xorg keymap to the nearest match.
If required, the keymap from /etc/vconsole.conf
can be loaded during early userspace by the keymap
mkinitcpio hook.
Download locally entire website | wget Command
GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies. Wget is non-interactive, meaning that it can work in the background, while the user is not logged on. This allows you to start a retrieval and disconnect from the system, letting Wget finish the work. By contrast, most of the Web browsers require constant user's presence, which can be a great hindrance when transferring a lot of data. Wget can follow links in HTML, XHTML, and CSS pages, to create local versions of remote web sites, fully recreating the directory structure of the original site. This is sometimes referred to as "recursive downloading." While doing that, Wget respects the Robot Exclusion Standard (/robots.txt). Wget can be instructed to convert the links in downloaded files to point at the local files, for offline viewing. Wget has been designed for robustness over slow or unstable network connections; if a download fails due to a network problem, it will keep retrying until the whole file has been retrieved. If the server supports regetting, it will instruct the server to continue the download from where it left off. Code example:
Create ZIP achive with password protected on Linux | zip Command
Let’s use the zip
command to create a password protected and compressed archive by using the examples below.
Example 1
The -p
(password) option allows us to specify a password in our zip
command. The following command will create a password protected zip archive of file1.txt
and file2.txt
, using a password of “mypassword” and the zip archive will be called my_archive.zip
.
WARNING: The problem with the
-p
option is that our password can easily be seen by anyone looking at the terminal over our shoulder or perusing the terminal history e.g. with thehistory
command. This is generally not a good method to use, but could be employed safely in some scenarios, such as inside of a Bash script, assuming that the user sets the password and the value is stored as a variable.
Example 2
A much better method to use is -e
(encrypt). This will prompt you for a password to use, and it will be hidden in the terminal so that onlookers and those browsing the terminal history will have no idea what you typed for a password. The syntax is as follows:
As you can see above, we are prompted in the terminal to enter and confirm our password selection, but the input keystrokes are invisible.
Last updated