
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,334 Views
I am a new learner of Linux.
can you help me to solve a problem?
I want to check the hostname using the for loop command: what will be the
command to check the hostname using the for loop command:
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,300 Views
Hello @KRISHNA_GOPAUL !
I am not sure the purpose of this but if you are just asking about the structure/format for this , then you can use something like this :
Suppose you have a list of all servers or their IPs in servers.txt file as :
servera.<domain> or 192.X.X.1
serverb.<domain> or 192.X.X.2
serverc.<domain> or 192.X.X.3
then use for loop like this : This explains the working of "for loop" in a basic manner :
If you just have the server list or the list of the IP address of those servers and want to know the hostname for all those servers remotely - then you can use ssh 'command' with the "for loop"
This command uses a for loop to iterate over the list of servers ( or their IPs ) in the server_list.txt file. For each server, the ssh command is used to connect to the server and the echo $(hostname -f) command will print the hostname of the remote server to the console.
The -t option with the ssh command tells the ssh command to execute the command on the remote server.
The 2>/dev/null part of the command redirects the standard error output of the ssh command to the /dev/null device. This prevents the connection to server closed message from being printed to the console.
Let me know if this works for you or you are looking for something else.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,268 Views
Dear Chetan_Tiwary_,
Thank you So much.
The solution is totally understandable and also learned in depth.
Best Regards
Krishna

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,301 Views
Hello @KRISHNA_GOPAUL !
I am not sure the purpose of this but if you are just asking about the structure/format for this , then you can use something like this :
Suppose you have a list of all servers or their IPs in servers.txt file as :
servera.<domain> or 192.X.X.1
serverb.<domain> or 192.X.X.2
serverc.<domain> or 192.X.X.3
then use for loop like this : This explains the working of "for loop" in a basic manner :
If you just have the server list or the list of the IP address of those servers and want to know the hostname for all those servers remotely - then you can use ssh 'command' with the "for loop"
This command uses a for loop to iterate over the list of servers ( or their IPs ) in the server_list.txt file. For each server, the ssh command is used to connect to the server and the echo $(hostname -f) command will print the hostname of the remote server to the console.
The -t option with the ssh command tells the ssh command to execute the command on the remote server.
The 2>/dev/null part of the command redirects the standard error output of the ssh command to the /dev/null device. This prevents the connection to server closed message from being printed to the console.
Let me know if this works for you or you are looking for something else.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,269 Views
Dear Chetan_Tiwary_,
Thank you So much.
The solution is totally understandable and also learned in depth.
Best Regards
Krishna