• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

KaliTut

Kali Linux tutorial and Linux Tips

  • Home
  • Raspberry Pi
  • Privacy Policy
  • About us
  • Affiliate disclaimer

Linux

Hostname Command in Linux

August 8, 2024 by Walid Salame

The world of Linux is densely populated with a variety of commands and tools that drive both the simplicity and the powerful capabilities of the system. In our journey across this vibrant landscape, we encounter the hostname command – a simple yet integral part of system identification in a network.

Hostname Command in Linux

Introduction to the Hostname Command

In essence, a hostname is a readable, human-friendly label assigned to a device connected to a computer network. On a Linux machine, the hostname command is the default tool for interacting with this label. Its primary function is twofold: to display the system’s DNS (Domain Name System) name, and to set its DNS name .

A typical hostname command syntax looks like this: hostname [options] [new_host_name]. The new_host_name part is optional and is only used when you want to change the system’s hostname . Additional parameters, called options, modify the behavior of the hostname command.

Performing Tasks with the Hostname Command

If you run the hostname command without any options or parameters, it simply returns the system’s current hostname, which can be highly beneficial for quick network orientation. System administrators would typically use this to identify individual systems within a vast network .

The hostname command can also be employed for modifying the DNS name temporarily. When run as root or with sudo privileges, and followed by a new hostname, the hostname command alters the system’s hostname until the system reboots .

To make the DNS name change more permanent, different Linux distributions may offer various methods. For many distributions, the hostname is stored in the ‘/etc/hostname’ file. Therefore, to enforce a sustainable change, modify the value within this file and reboot the system .

On more comprehensive tasks, the hostname command can display the DNS domain name (-d or –domain option), the fully qualified domain name or FQDN (-f or –fqdn option) comprising of the hostname and the domain name, and even network addresses (-i or –ip-address option) .

Why is the Hostname Command Important?

The simplicity of the hostname command’s usage underscore its prominence within system administration and network configuration. Network administrators frequently employ the hostname command within scripts to troubleshoot network issues or configure new setups. The system’s hostname plays a crucial role in establishing network protocols, serving as a communicative link between devices .

Despite its simplicity, the hostname command can deliver a wealth of vital networking information, from basic system identification to more intricate networking details. This foundational command is another testament to the flexibility and power of Linux commands.

Hostname Command in Linux: A Comprehensive KeyPoint List

  • Brief Explanation:
    • The hostname command in Linux is a tool used primarily to display the system’s DNS (Domain Name System) name, and to set its DNS name. It’s a way for systems to identify and communicate with each other on a network .
  • Command Syntax:
    • As with most Linux commands, hostname is utilized from the command line with the syntax: ‘hostname [options] [new_host_name]’. Options are additional parameters to modify the behavior of the command. The ‘new_host_name’ is optional and is used when changing the system’s hostname .
  • Display Hostname:
    • Executing the hostname command without any options or parameters simply returns the system’s current hostname. This is useful for system administrators to quickly identify a system within a network .
  • Setting Hostname:
    • As root or with sudo privileges, the hostname command, followed by a new hostname, temporarily changes the system’s hostname for the current session. This change does not persist after a system reboot .
  • Permanent Hostname Change:
    • A permanent change of the hostname on a Linux machine can be handled in different ways depending on the distribution. Usually, the hostname is stored in a file (‘/etc/hostname’ for many distributions). Changing the value inside this file and rebooting the system will make the hostname change permanent .
  • Display Domain Name:
    • The -d or –domain option with the hostname command provides the DNS domain name of the system. Not all systems are part of a DNS domain, so this will not always return useful information .
  • Display Fully Qualified Domain Name (FQDN):
    • For a complete domain name, the -f or –fqdn option can be used. This displays the fully qualified domain name of the system. It comprises the hostname and the domain name of the system .
  • Display IP Address(es):
    • By using the -i or –ip-address argument with the hostname command, it will return the network address(es) of the host .
  • Common Usage:
    • The hostname command is typically used in scripts by system administrators for network configuration or to troubleshoot network issues, as the hostname is essential to establish network connections .

Examples of how the hostname command can be used in Linux:

Display Current Hostname: Simplest usage without any options will display the current hostname.

hostname

This could return something like:

user@localhost:~$ hostname
localhost

This means your current hostname is “localhost”.

Set Temporary Hostname: You can change the hostname temporarily by using the hostname command followed by the new hostname. Remember, you need to have superuser privileges, so you have to use sudo.

sudo hostname newHostname

Make sure to replace newHostname with your desired hostname. This change will not survive a reboot.

Display Domain Name: The -d or –domain options can be used to display the system’s domain name.

hostname -d
OR
hostname --domain

Display Fully Qualified Domain Name: The -f or –fqdn options will display the system’s Fully Qualified Domain Name (FQDN).

hostname -f
OR
hostname --fqdn

Display IP Address: The -i or –ip-address options are used to display the IP address (es) associated with the hostname.

hostname -i
OR
hostname --ip-address

Remember, these examples are mainly useful for Unix-like operating systems, including Linux distributions and MacOS. Always cross-check with your specific system’s documentation if you’re unsure.

Notes on the hostname command

We will briefly explain three points to note when using the hostname command.

  • Temporary vs. Permanent Hostname Changes
  • Restrictions on characters that can be used in host names
  • Impact of changing hostname

Temporary vs. Permanent Hostname Changes

If you change the hostname using the hostname command, the change is temporary: the hostname reverts to the original when you reboot the system.

To change the hostname permanently, you need to edit the / etc / hostname file, which will automatically set the hostname when the system boots.

You also need to write the correspondence between host names and IP addresses in the / etc / hosts file. By setting these files appropriately, you can change the host name permanently.

Restrictions on characters that can be used in host names

There are restrictions on the characters that can be used in a host name. Only alphanumeric characters and hyphens (-) can be used.

In addition, the host name cannot start or end with a hyphen, and cannot contain a period (.).

If you try to set a hostname that violates these restrictions, an error will occur. You should take these restrictions into consideration when choosing a hostname.

Impact of changing hostname

Changing the hostname can affect various configurations, for example SSH configuration files, Apache configuration files, etc.

These configuration files may use hostnames, so if you change your hostname you will likely need to update these configuration files accordingly.

In addition, changing the hostname may affect other computers on the network, so you should give it careful consideration and preparation beforehand.

Troubleshooting the hostname command

We will briefly explain the following three points regarding troubleshooting the hostname command.

  • What to do if the hostname is not set correctly
  • What to do if the hostname change is not reflected
  • What to do if the hostname command fails to execute

What to do if the hostname is not set correctly

If the hostname is not set correctly, various problems may occur, such as failure to send emails or network services not working properly.

You can check whether the hostname is set correctly by using the hostname command. If it is not correct, check the / etc / hostname file and the / etc / hosts file, and set the appropriate hostname.

It is also important to ensure that the hostname is consistent with what is registered in the DNS server . Incorrect DNS settings can also cause problems with hostnames.

What to do if the hostname change is not reflected

If you change the hostname using the hostname command, the change may not be reflected immediately because the old hostname may still be in use due to system caching.

The surest way to resolve this issue is to reboot your system once, which will clear the cache and ensure the new hostname is used.

Alternatively, you can restart any services that still have the old hostname, for example Apache, so it will start using the new hostname.

What to do if the hostname command fails to execute

If the hostname command fails to execute, there are several possible causes. First, if you are not the root user, you cannot change the hostname. You must use sudo to execute the command with root user privileges.

Additionally, if you specify invalid characters in the host name, an error will occur. Make sure that the host name contains only alphanumeric characters and hyphens.

In addition, if there is a problem with the network settings, the hostname command may fail to execute. You may need to review your network settings and make sure they are configured correctly.

Summary

The hostname command in Linux is a simple yet versatile tool for identifying and managing system identification in a network environment. It displays or sets the system’s Domain Name System (DNS) name, commonly known as the hostname, which is a label assigned to a machine on a network for easier identification.

The basic hostname command, when run without any additional options or parameters, displays the current hostname of the system. This is especially useful in aiding system administrators in swiftly identifying a machine within a network.

When run with sudo privileges followed by a new hostname, the hostname command temporarily changes the hostname for the current session. However, to change the hostname permanently, it’s typically done by editing the hostname file located at ‘/etc/hostname’ and rebooting the system.

For further detailed information about the system’s network configuration, the hostname command can also display the DNS domain name, the Fully Qualified Domain Name (FQDN), which combines the hostname and domain name, and the network address(es) associated with the host.

Overall, the hostname command is an essential tool in Linux, extensively utilized by system administrators in tasks like scripting, network configuration, and troubleshooting.

Filed Under: Linux Commands

uname command in Linux

August 8, 2024 by Walid Salame

The uname command in Linux is used to display system information. It provides details about the operating system, kernel version, and other system-specific data. Let’s break down its functionality and provide some examples.

uname command in Linux
uname [OPTION]...

Options
Here are some commonly used options with uname:

Short optionsLong optionsmeaning
-a–allDisplays the kernel name (-s), hostname (-n), kernel release number (-r), kernel version (-v), hardware name (-m), CPU (-p, omitted if unknown), hardware platform (-i, omitted if unknown), and OS name (-o), in that order.
-s–kernel-nameShow kernel name (default)
-n–nodenameShow hostname (network node)
-r–kernel-releasePrint the kernel release number
-v–kernel-versionShow kernel version
-m–machineShow hardware name (e.g. x86_64)
-p–processorShow CPU type (or “unknown” if unknown)
-i–hardware-platformShow the hardware platform (or “unknown” if unknown)
-o–operating-systemShow the name of the OS

Examples

Print the Kernel Name:

uname -s
uname -s

Print the Network Node Hostname:

uname -n
Output
localhost

Print the Kernel Release:

uname -r
Output:
6.6.9-amd64

Print the Kernel Version:

uname -v
Output:
#1 SMP PREEMPT_DYNAMIC Kali 6.6.9-1kali1 (2024-01-08)

Print the Machine Hardware Name:

uname -m
Output:
x86_64

Print the Processor Type (might be unknown on some systems):

uname -p
Output:
x86_64

Print the Hardware Platform (might be unknown on some systems):

uname -i
Output:
x86_64

Print the Operating System:

uname -o
Output:
GNU/Linux

Print All System Information:

uname -a
Output:
Linux localhost 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Explanation of Output

  • Linux: The kernel name.
  • localhost: The network node hostname.
  • 5.4.0-42-generic: The kernel release version.
  • #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020: The kernel version, including build and compiler info.
  • x86_64: The machine hardware name, processor type, and hardware platform (all are the same in this example).
  • GNU/Linux: The operating system name.

Summary

The uname command is a versatile tool to quickly gather system information, particularly useful in scripting and system administration tasks. By using the appropriate options, you can specify exactly what information you need, or use -a to get a comprehensive overview of the system.

Filed Under: Linux Commands

Search files by content in Linux

March 16, 2024 by Walid Salame

Sometimes you may need to find a file that contains a certain string or find a line in a file that contains a specific word. This may be needed to search for logs, search for configuration files if you do not know where they are, or to search for files with program code.

Search files by content in Linux

Previously, only the grep utility was used for this purpose, but now there are a huge number of other programs that can do this faster and provide a more convenient interface. In this article we will look at the most interesting of them and several examples of how to use them.

Search files by content in Linux
All utilities in this article work only in the terminal. Most of the commands described below have approximately the same syntax:

$ command options pattern /path/to/folder

The search pattern can be a regular word, but most commands expect it to be a regular expression, so if your query has a special one. symbols, the result may not be what you expect. As a rule, it is not necessary to specify the path to the folder; then the commands use the current folder for searching. Now let’s look at each command in more detail.

Grep Linux command

Before moving on to modern utilities, let’s take a look at the grep command. It is part of the GNU suite of programs and has been used to search for strings in files and files by their contents for a very long time.

By default, grep filters one file or standard input. To search multiple files in a folder, you need to enable recursive search using the -r option . For example, let’s find all the files in the /etc/ folder that contain the line root :

sudo grep -r "root" /etc/
Grep Linux command

The grep command does not highlight the occurrences of characters that you searched for in color; for this, you can use the –color=always option . But in most distributions this option is already specified in the alias for this command, so the output will look like this:

sudo grep --color=always -r "root" /etc/
grep command highlight

Using the -C or –context option , you can enable display of not only the current line in which the occurrence was found, but also several lines before and after it. In this parameter you need to specify the number of lines to display:

sudo grep -r -С2 "root" /etc/
sudo grep -r -С2

By default, grep expects the search query to be a regular expression, but only the basic syntax is supported. To enable extended syntax, you must use the -E option . For example, to find all files containing variables starting with the letter A in the /etc/ folder , run:

sudo grep -r -E "^A[A-Z_]+\=" /etc/
fgrep command

And in order to search for a fixed string rather than a regular expression, use the -F option or the fgrep command . For example, this is how you can find all files containing the [Install] section in the /usr/ folder :

sudo grep -r -F "[Install]" /usr/
search-fixed-string-grep

Ripgrep Linux command

This is a popular alternative to grep written in Rust. It can do everything that grep does, but it’s faster, and it’s also much more convenient to use. Recursive search is enabled by default, and highlighting occurrences and filenames in different colors also works without additional options, and it also skips hidden files, binaries, and files listed in .gitignore . To install ripgrep on Ubuntu use this command:

sudo apt install ripgrep

If you run the command with a file path, it will find and display all occurrences of the search word in that file. For example:

sudo rg root /etc/passwd
sudo apt install ripgrep

If you pass a folder, the command will search in all the files that are in this folder. The default is the current folder. For example, to find all files containing the word “root” in the /etc/ directory , use the following command:

sudo rg root /etc/
sudo rg root /etc/

Just like when using grep, you can display not only the line with the occurrence, but also several lines before and after. For example, two each:

sudo rg -C2 root /etc/
sudo rg -C2 root /etc/

But the full regular expression syntax works here by default. For example:

sudo rg "^A[A-Z_]+=" /etc/
full regular expression syntax

If you want to specify that you want to search for a string rather than a regular expression, use the -F option :

sudo rg -F "[Install]" /usr/
 search for a string

Ack Linux command

If you need to find a file with source code knowing the line that is in it, then there are more suitable utilities for this than grep. For example, ack. It appeared quite a long time ago and is designed specifically for working with source code. In addition to all the features of grep, it allows you to skip backup files, internal .git and .svn repository files , and memory dumps. In addition, you can select the file types to be searched and even specify a specific part of the file. To install the program on Ubuntu, use the following command:

sudo apt install ack

The simplest example is to search for all files containing the word root in the /etc/ folder :

sudo ack "root" /etc/

Or a regular expression, as in the previous section, to search for files with variables starting with the letter A:

sudo ack "^A[A-Z_]+=" /etc/
ack Linux command

To print not only the line with the occurrence, but also the lines before and after it, use the -C option. By default, two lines are displayed:

sudo ack -C "root" /etc/
Ark -C

The ack command allows you to specify the type of files to search. This is very convenient for searching by source. You can only select C, JavaScript or PHP source files and so on. All available file types can be viewed using the command:

ack --help-types
ack --help-types

For example, to search only XML files, use the –type option with the value xml :

sudo ack --type=xml "root" /etc/
sudo ack --type

Another interesting feature of the ack utility is setting up parts of the file that will be searched using a regular expression. The –range-start and –range-end options are intended for this , and it will work both within one line and within the entire file. For example, to search only the comments of XML files, you can use the following command:

sudo ack --type=xml --range-start="\<\!\-\-" --range-end="\-\-\>" "root" /etc/
ack utility

Silver Searcher

This is currently one of the most popular programs for searching text from files in Linux. It was designed as an alternative to ack, as a code search tool. In addition to the core features of ack, it is significantly faster and takes into account exclusion settings from .gitignore and .hgignore files. To install the program on Ubuntu, use the following command:

sudo apt install silversearcher-ag

Let’s look at the same example as in the previous sections. To find all files containing the word root in the /etc/ folder , run:

sudo ag "root" /etc/
sudo apt install silversearcher-ag

Similar to grep and ripgrep , ag expects a regular expression as a search pattern, so you can use it without any additional options. For example:

sudo ag "^A[A-Z_]+=" /etc/
Silver Searcher linux

But if you want the search query to be treated as a string, use the -Q option . For example, to search for all files containing the [Install] section in the /usr/ folder , run:

sudo ag -Q "[Install]" /usr/
sudo ag -Q

Here, too, you can display several lines before and after the line with the occurrence using the -C option .

Unlike grep and ripgrep , the ag command allows you to specify the type of files you want to search. You can view all available file types using the following command:

ag --list-file-types
ag --list-file-types

For example, to search only by ini files, use the following command:

sudo ag --ini "root" /etc/
sudo ag --ini

The utility also allows you to use a regular expression to filter files by name before searching by content. To do this you need to use the -G option . For example, to search only for files whose name ends with conf, run:

sudo ag -G .*\.conf$ root /etc/
sudo ag -G .*\.conf$ root /etc/

Skim fuzzy search utility

This is another interesting real-time fuzzy search utility written in Rust. By default, it searches for files by name, but it can be used in conjunction with one of the utilities listed above to search for files by content in real time. The package with the program is not yet in the official repositories, but you can install it using cargo:

cargo install skim

The command searches only in the current folder and you cannot specify another folder, so you must first go to the desired folder using the cd command . For example:

cd /etc/

Then use this command to combine sk with Silver Searcher to filter files by content:

sk --ansi -i -c 'ag --color "{}"'

Here the –ansi option turns on displaying colors, -i turns on interactive mode, and the -c option specifies a command that will be executed when you enter any query, the string {} will be replaced with whatever you enter when searching. Just run the command and start typing the word you want to search for:

Skim fuzzy search utility

Then you can use all the fuzzy search filtering functions to process the results.

Performance of grep, rg, ack and ag

Before finishing the article, I want to give a small performance test of the above listed utilities for searching text in files in Linux. I downloaded and extracted the Linux kernel 6.2.10 source and then used each of the utilities to find all files that contained the word ext4 and btrfs. Between different utilities, the system was rebooted in order to eliminate the influence of the cache. Here are the results:

Team Time, sTeamTime , s
grep -r ext4 ./23.167grep -r btrfs ./3.860
rg ext4 ./27.164rg btrfs ./1.387
ack ext4 ./36.141ack btrfs ./7.206
ag ext4 ./24.594ag btrfs ./3.158

As you can see, immediately after the system starts, all commands work approximately the same. However, after that, utilities written in Rust use the cache more efficiently, because when launched repeatedly for the same set of files, they work faster than their C counterparts.

conclusions
This article covered the most commonly used utilities for searching files by content and searching for text in files in Linux. As you can see, there are enough tools to choose from. All teams are quite similar, but each of them has its own characteristics. What do you use? Write in the comments!

Filed Under: Linux Commands

Linux commands for beginners: A Short Course

August 17, 2020 by Walid Salame

This short course will be your first step into Linux and we will cover much more then just Linux commands for beginners and Linux fundamentals

linux commands for beginners
[Read more…] about Linux commands for beginners: A Short Course

Filed Under: Linux Commands Tagged With: Linux commands

Find out Linux version and kernel version from console

August 16, 2020 by Walid Salame

There are several different commands that can help you determine which Linux distribution and version is running on your system. this article will teach you how to check Linux version

find os version linux
[Read more…] about Find out Linux version and kernel version from console

Filed Under: Linux Tagged With: find os version linux

  • Page 1
  • Page 2
  • Page 3
  • Interim pages omitted …
  • Page 5
  • Go to Next Page »

Primary Sidebar

Follow us

  • Facebook
  • Twitter
  • YouTube

Categories

  • algorithm
  • Android Ai coding
  • Android pentesting tools
  • Arduino
  • Artificial Intelligence
  • Books
  • ChatGPT Prompt
  • Darknet
  • database
  • General
  • Github Tools
  • Hacking
  • Kali Linux
  • Linux
  • Linux Commands
  • Network Administrator
  • Penetration Testing
  • Penetration Testing Tools
  • PowerShell
  • Raspberry Pi
  • resources
  • Review
  • Termux
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Video Tutorials
  • vmware
  • WiFi Adapter
  • WiFi Pentesting
  • Wireless Router
  • Wireshark

Recent Posts

  • Transforming Photos with ChatGPT Prompt : A Cinematic Double Exposure in a Post-Apocalyptic World
  • Stryker Android App: Your Mobile Pentesting Powerhouse
  • Alfa awus036ach review
  • Alfa AWUS1900 for Wireless Penetration Testing
  • How to Run DeepSeek AI Locally on Kali Linux – Step-by-Step Guide

Footer

Kalitut

Kalitut.com goal is to share the knowledge for free, help you find the best tools on the web and provides tutorials

Find us on social media

  • Facebook
  • Pinterest
  • Reddit
  • Twitter
  • YouTube

Copyright © 2025

  • Home
  • About us
  • Privacy Policy
  • Affiliate disclaimer