hostname and host configuration in linux
hostname and host configuration in linux
In Linux, managing the hostname and host For systems with hostnamectl (like those
configuration involves setting up the running systemd), you can set the
system’s hostname and defining host hostname permanently with:
entries in the /etc/hosts file. Here’s a
guide to configuring both: bash
Copy code
sudo hostnamectl set-hostname new-
1. Setting the Hostname hostname
Built-in Variables in awk This prints each line from the file.
By default, sed will print every
NR: Current record (line) number. line, so the -n option suppresses
NF: Number of fields in the current this behavior, allowing only
record. specified lines to be printed.
$0: Entire line.
$1, $2, etc.: Individual fields. 2. Substitute (Find and Replace)
ChatGPT bash
Copy code
sed 's/old-text/new-text/g'
filename
Replace in Place (Modify File 7. Change Text on a Specific Line
Directly):
bash
bash Copy code
Copy code sed '3c\This line has been
sed -i 's/old-text/new- changed' filename
text/g' filename
Replaces line 3 with "This line has
3. Replacing Only on Specific Lines been changed".
bash
Deletes line 5 in the file. Copy code
sed '/pattern/s/old-
Delete a Range of Lines: text/new-text/' filename
bash bash
Copy code Copy code
tar -cvjf archive- tar -xvJf archive-
name.tar.bz2 directory/ name.tar.xz
ChatGPT said:
3. 2> (Redirect Standard Error)
o Syntax: command 2>
ChatGPT filename
o Redirects the standard error
In Linux, output redirection is a way to (error messages) of a
direct the output of a command to a file or command to a file.
another command, rather than displaying it o Example:
on the screen. Here are the basics and
bash
some common uses for output redirection. Copy code
ls
Basic Redirection Operators /nonexistent_directory
2> error.log
1. > (Redirect Output)
o Syntax: command >
If ls fails because the
filename directory doesn’t exist, the
o Redirects the standard error message will be saved
output of a command to a to error.log.
file. If the file already
exists, it will be 4. 2>> (Append Standard Error)
overwritten. o Syntax: command 2>>
o Example: filename
o Redirects standard error and
bash appends it to the file if it
Copy code exists.
ls > filelist.txt o Example:
bash
Copy code
command > /dev/null bash
Copy code
env
Suppress Errors:
bash bash
Copy code Copy code
echo $PATH export
PATH=$PATH:~/my_scripts
Adding a Directory to PATH
Temporarily
Now you can run any executable Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
script from ~/my_scripts directly Release: 20.04
from the command line. Codename: focal
ChatGPT said:
bash
Copy code
cat /etc/*release
ChatGPT
c. Using hostnamectl (if available)
You can find your Linux distribution and
kernel version information using a few
The hostnamectl command shows OS
simple commands:
and kernel information, along with
hostname details.
1. Find Distribution Information
bash
a. Using lsb_release (if available) Copy code
hostnamectl
The lsb_release command provides
information about the distribution and Output example:
version.
yaml
Copy code
bash
Operating System: Ubuntu
Copy code
20.04.3 LTS
lsb_release -a
Kernel: Linux 5.4.0-
89-generic
Output might look like this: Architecture: x86-64
The fdisk command can display partition The lsusb command lists all USB devices
tables for specified devices. You may need connected to the system.
root permissions.
bash
Copy code
bash lsusb
Copy code
sudo fdisk -l
d. Using dmidecode
d. Using du for Directory Disk Usage
The dmidecode command displays
The du command shows disk usage of hardware information retrieved from the
directories and files. system’s DMI (Desktop Management
Interface) table, including system
bash manufacturer, model, and BIOS version.
Copy code
du -sh /path/to/directory bash
Copy code
sudo dmidecode
bash
Copy code
inxi -Fxz
bash
Copy code
sudo apt install inxi # For
Debian/Ubuntu
sudo yum install inxi # For Red
Hat/CentOS
sudo dnf install inxi # For
Fedora
Summary of Commands
Information Command
lscpu, cat
CPU /proc/cpuinfo
free -h, cat
Memory /proc/meminfo
df -h, lsblk, sudo fdisk
Disk Space -l
Directory du -sh
Usage /path/to/directory
sudo lshw, lspci, lsusb,
Hardware sudo dmidecode
System
inxi -Fxz (if available)
Summary