Network Utilities and File Transfer between computers
hostname and whoami commands
A hostname is a unique name that identifies a device on a network. It is used to identify and distinguish devices on a network, and it can be used to access network services on the device.
On a Linux system, the hostname is stored in the file /etc/hostname
and can be viewed and changed using the hostname command.
Here is an example of using the hostname command:
To view the current hostname of the system:
$ hostname
The whoami command is a simple command that displays the username of the current user. It can be useful for determining the current user’s identity, particularly when running scripts or commands with different permissions.
Here is an example of using the whoami command:
$ whoami
This will display the username of the current user.
ssh
command to connect to remote computer
The ssh command stands for “secure shell” and is used to establish a secure connection to a remote computer over a network. It uses the Secure Shell (SSH) protocol to encrypt the data being transferred, making it secure and suitable for transferring sensitive data.
Here is the basic syntax for using the ssh command:
$ ssh username@remote_server
This establishes an SSH connection to the remote server as the specified username.
You can also specify a port number if the remote server is using a non-standard SSH port. For example:
$ ssh -p 2222 username@remote_server
This establishes an SSH connection to the remote server using port 2222.
Once connected, you can execute commands on the remote server as if you were logged in directly.
Here are some other useful options for the ssh command:
- -v: Enables verbose mode, which can be helpful for debugging connection issues.
- -i path/to/private_key: Specifies the path to an SSH private key to use for authentication.
- -o “option”: Allows you to pass options to the SSH client. For example, -o “StrictHostKeyChecking no” disables host key checking, which can be useful if you are connecting to a host for the first time.
To run a command on a remote computer using ssh, you can use the following syntax:
$ ssh username@remote_server command
This establishes an SSH connection to the remote server as the specified username and runs the specified command. The command will be executed on the remote server and the output will be displayed on your local machine.
For example, to run the ls command on a remote server and display the directory listing on your local machine, you can use the following command:
$ ssh username@remote_server ls
scp
command for secure file copy to remote servers
The scp command stands for “secure copy” and is used to transfer files between computers over a network. It uses the Secure Shell (SSH) protocol to encrypt the data being transferred, making it secure and suitable for transferring sensitive files.
Here is the basic syntax for using the scp command:
scp source destination
The source argument specifies the file or directory you want to copy, and the destination argument specifies where you want to copy it to.
Here are some examples of using the scp command:
- To copy a file from your local computer to a remote server:
$ scp local_file.txt username@remote_server:/path/to/destination
- To copy a file from a remote server to your local computer:
$ scp username@remote_server:/path/to/remote/file.txt /path/to/local/destination
- To copy a directory and all its contents from your local computer to a remote server:
$ scp -r local_directory username@remote_server:/path/to/destination
- To copy a directory and all its contents from a remote server to your local computer:
$ scp -r username@remote_server:/path/to/remote/directory /path/to/local/destination
Note: The -r option tells scp to copy the directory and all its contents, including any subdirectories and files, recursively.
You can also specify a port number if the remote server is using a non-standard SSH port. For example:
scp -P 2222 local_file.txt username@remote_server:/path/to/destination
This copies local_file.txt from your local computer to the remote server using port 2222 for the SSH connection.
Host
Command to translate names to IP addresses
The host command is a tool for performing DNS lookups. It can be used to translate domain names to IP addresses and vice versa, as well as to retrieve other DNS records for a domain.
Here is the basic syntax for using the host command:
host [-aCdlnrtTvW] [-c class] [-N ndots] [-R number] [-t type] [-w wait] [-4] [-6] name [server]
The name argument specifies the domain name or IP address that you want to look up. The server argument is optional and specifies the DNS server that you want to use for the lookup.
Here are some examples of using the host command:
- To translate a domain name to an IP address:
$ host www.example.com
- To translate an IP address to a domain name:
$ host 123.45.67.89
- To retrieve the MX (mail exchange) records for a domain:
$ host -t MX example.com
- To retrieve all DNS records for a domain:
$ host -a example.com
The host command has many options that allow you to customize the type of DNS lookup that you want to perform. You can see a complete list of options by running man host or by consulting the host command’s documentation.
The nslookup command is a tool for performing DNS lookups. It can be used to translate domain names to IP addresses and vice versa, as well as to retrieve other DNS records for a domain.
nslookup
command to translate Domain name to IP address
Here is the basic syntax for using the nslookup command:
nslookup [-option] [name] [server]
The name argument specifies the domain name or IP address that you want to look up. The server argument is optional and specifies the DNS server that you want to use for the lookup.
Here are some examples of using the nslookup command:
To translate a domain name to an IP address:
$ nslookup www.example.com
To translate an IP address to a domain name:
$ nslookup 123.45.67.89
To retrieve the MX (mail exchange) records for a domain:
$ nslookup -type=mx example.com
To use a specific DNS server for the lookup:
$ nslookup www.example.com 8.8.8.8
The nslookup command has many options that allow you to customize the type of DNS lookup that you want to perform. You can see a complete list of options by running man nslookup or by consulting the nslookup command’s documentation.
curl command to transfer data from/to server
The curl
command in Linux is a utility for transferring data from or to a server using one of the supported protocols such as HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE. It is a powerful tool that allows you to send HTTP requests and receive HTTP responses from a web server.
To use curl
, you need to specify a URL that specifies the location of the server and the resource you want to access. You can also specify additional options to modify the request, such as the HTTP method, the data to be sent, and the format of the response.
Here are a few examples of using the curl
command:
- To send an HTTP GET request to retrieve a web page:
$ curl https://www.example.com
- To send an HTTP POST request to submit a form:
$ curl -X POST -d "name=John&email=john@example.com" https://www.example.com/form
- To save the response to a file:
$ curl https://www.example.com -o output.html
- To set the user agent in the request:
$ curl -A "Mozilla/5.0" https://www.example.com
- To follow redirects:
$ curl -L https://www.example.com
For more information on the curl
command and its options, you can consult the curl
man page or use the man curl
command in a terminal.
To make a GET request using curl in Linux, you can use the following syntax:
$ curl -X GET <URL>
For example, to make a GET request to the JSONPlaceholder API to retrieve a list of users, you can use the following command:
$ curl -X GET https://jsonplaceholder.typicode.com/users
This will return a JSON array with a list of users.
You can also pass additional options to customize the request. For example, to include the response headers in the output, you can use the “-i” option:
$ curl -i -X GET https://jsonplaceholder.typicode.com/users
mail
command to send emails in CLI
There are several ways to send email from the command line in Linux. One common way is to use the mail command.
Here is the basic syntax for using the mail command to send an email:
$ echo "message" | mail -s "subject" recipient@example.com
This command sends an email with the specified subject and message to the specified recipient. The echo command is used to pipe the message text to the mail command, which sends the email.
You can also specify multiple recipients by separating their email addresses with a comma:
$ echo "message" | mail -s "subject" recipient1@example.com,recipient2@example.com
If you want to include a file as an attachment, you can use the -a option:
$ echo "message" | mail -s "subject" -a /path/to/attachment.txt recipient@example.com
Summary:
The hostname
command is used to view or set the hostname of a device on a network. It is stored in the file /etc/hostname
and can be changed by running the hostname
command followed by the desired hostname.
The whoami
command is used to display the username of the current user. It can be useful for determining the identity of the user running a script or command.
The ssh
command is used to establish a secure connection to a remote computer over a network. It uses the Secure Shell (SSH) protocol to encrypt the data being transferred. The basic syntax for using ssh
is ssh username@remote_server
, which establishes an SSH connection to the remote server as the specified username. The ssh
command has several options, such as -v
for verbose mode and -i
to specify an SSH private key for authentication. It can also be used to run a command on a remote computer by using the syntax ssh username@remote_server command
, which will execute the command on the remote server and display the output on the local machine.
The scp
command is used to transfer files between computers over a network. It uses the Secure Shell (SSH) protocol to encrypt the data being transferred. The basic syntax for using scp
is scp source destination
, where the source
argument specifies the file or directory to be copied, and the destination
argument specifies the location to copy it to. The scp
command has the -r
option to copy directories and their contents recursively, and the -P
option to specify a non-standard SSH port.
Questions to review:
- How can the hostname of a Linux system be viewed and changed?
- What does the whoami command do and when might it be useful?
- What is the ssh command used for and how does it establish a secure connection to a remote server?
- What are some options available with the ssh command and how can they be used?
- How can a command be run on a remote server using ssh?
- What is the scp command used for and how does it transfer files between computers over a network?
- What are some examples of using the scp command to transfer files between a local computer and a remote server?
- How can the -r option be used with the scp command to copy directories and their contents recursively?
- How can the -P option be used with the scp command to specify a non-standard SSH port?