Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Make a onelinear-command for automating this Redhat exercise: ` Lab: Manage Networks…

  1. User

    Make a onelinear-command for automating this Redhat exercise:

    Lab: Manage Networks
    Note
    If you plan to take the RHCSA exam, then use the following approach to maximize the benefit of this Comprehensive Review: attempt each lab without viewing the solution buttons or referring to the course content. Use the grading scripts to gauge your progress as you complete each lab.
    
    Configure and test network connectivity.
    
    Outcomes
    
    
    Configure network settings.
    
    Test network connectivity.
    
    Set a static hostname.
    
    Use locally resolvable canonical hostnames to connect to systems.
    
    
    If you did not reset your workstation and server machines at the end of the last chapter, then save any work that you want to keep from earlier exercises on those machines, and reset them now.
    
    As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
    
    This command prepares your environment and ensures that all required resources are available.
    
    [student@workstation ~]$ lab start rhcsa-rh124-review4
    Important
    When you use the ssh command to adjust networking settings, an incorrect command might hang or lock out your session. You are then disconnected from the server, and thus the server becomes inaccessible. You must adjust the network configuration through the server console, locally or through a remote console.
    
    In this review, open the serverb machine console to adjust the networking settings.
    
    Specifications
    
    On serverb, determine the name of the Ethernet interface and its active connection profile.
    
    On serverb, create and activate a static connection profile for the available Ethernet interface. The static profile statically sets network settings and does not use DHCP. Configure the static profile to use the network settings in the following table:
    
    Parameter	Setting
    IPv4 address	172.25.250.111
    Netmask	255.255.255.0
    Gateway	172.25.250.254
    DNS Server	172.25.250.254
    Set the serverb hostname to server-review4.lab4.example.com.
    
    On serverb, set client-review4 as the canonical hostname for the servera 172.25.250.10 IPv4 address.
    
    Configure the static connection profile with an additional IPv4 address of 172.25.250.211 with a netmask of 255.255.255.0. Do not remove the existing IPv4 address. Ensure that serverb responds to all addresses when the static connection is active.
    
    On serverb, restore the original network settings by activating the original network connection profile.
    
    Use the serverb console to log in as the student user on the serverb machine, and switch to the root user.
    
    serverb login: student
    Password: student
    [student@serverb ~]$ sudo -i
    [sudo] password for student: student
    [root@serverb ~]#
    On serverb, determine the Ethernet interface name and the connection profile name that it uses.
    
    Display the network connection information.
    
    [root@serverb ~]# nmcli device status
    DEVICE  TYPE      STATE                   CONNECTION
    eth0    ethernet  connected               System eth0
    eth1    ethernet  connected               System eth1
    lo      loopback  connected (externally)  lo
    In this example, eth0 is the Ethernet interface name. The connection profile name is System eth0. Create the static connection profile for this interface.
    
    Note
    The network interface and connection profile names might differ from the previous output. Use the name that your system shows to replace the ethX placeholder name in subsequent steps.
    
    On serverb, create the static connection profile for the ethX interface. Set the network settings statically so that it does not use DHCP. When done, activate that connection profile. Base the settings on the following table:
    
    IPv4 address	172.25.250.111
    Netmask	255.255.255.0
    Gateway	172.25.250.254
    DNS server	172.25.250.254
    Create the static connection profile with the provided network settings.
    
    [root@serverb ~]# nmcli connection add con-name static type ethernet \
    ifname ethX ipv4.addresses '172.25.250.111/24' ipv4.gateway '172.25.250.254' \
    ipv4.dns '172.25.250.254' ipv4.method manual
    Connection 'static' (ac8620e6-b77e-499f-9931-118b8b015807) successfully added.
    Activate the new connection profile.
    
    [root@serverb ~]# nmcli connection up static
    Set the serverb hostname to server-review4.lab4.example.com. Verify the new hostname.
    
    Configure server-review4.lab4.example.com as the new hostname.
    
    [root@serverb ~]# hostnamectl hostname server-review4.lab4.example.com
    [root@serverb ~]# hostname
    server-review4.lab4.example.com
    On serverb, set client-review4 as the canonical hostname for the servera 172.25.250.10 IPv4 address.
    
    Edit the /etc/hosts file and add client-review4 as a name for the 172.25.250.10 IPv4 address.
    
    172.25.250.10 client-review4
    Verify that you can reach the servera 172.25.250.10 IPv4 address by using the canonical client-review4 hostname.
    
    [root@serverb ~]# ping -c2 client-review4
    PING client-review4 (172.25.250.10) 56(84) bytes of data.
    64 bytes from client-review4 (172.25.250.10): icmp_seq=1 ttl=64 time=0.259 ms
    64 bytes from client-review4 (172.25.250.10): icmp_seq=2 ttl=64 time=0.391 ms
    
    --- client-review4 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 33ms
    rtt min/avg/max/mdev = 0.259/0.325/0.391/0.066 ms
    Modify the static connection profile to configure the additional 172.25.250.211 IPv4 address with the 255.255.255.0 netmask. Do not remove the existing IPv4 address. Verify that serverb responds to all addresses when the static connection profile is active.
    
    Add the 172.25.250.211 IP address to the static connection.
    
    [root@serverb ~]# nmcli connection modify static \
    +ipv4.addresses '172.25.250.211/24'
    Activate the new IP address.
    
    [root@serverb ~]# nmcli connection up static
    ...output omitted...
    From workstation, use the ping command to verify that the 172.25.250.211 IPv4 address is reachable.
    
    [student@workstation ~]$ ping -c2 172.25.250.211
    PING 172.25.250.211 (172.25.250.211) 56(84) bytes of data.
    64 bytes from 172.25.250.211: icmp_seq=1 ttl=64 time=0.246 ms
    64 bytes from 172.25.250.211: icmp_seq=2 ttl=64 time=0.296 ms
    
    --- 172.25.250.211 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 50ms
    rtt min/avg/max/mdev = 0.246/0.271/0.296/0.025 ms
    On serverb, restore the original settings by activating the original network profile.
    
    Return to the console and use the nmcli command to activate the original network profile.
    
    [root@serverb ~]# nmcli connection up "System eth0"
    ...output omitted...
    The original connection profile name might differ on serverb. Replace the name in this solution with the name from your system. Find the profile name with the nmcli connection show command.
    
    From workstation, log in to serverb as the student user to verify that the original network settings are active.
    
    [student@workstation ~]$ ssh student@serverb
    ...output omitted...
    [student@server-review4 ~]$
    Exit any extra terminals. Return to the workstation system as the student user.
    
    [student@server-review4 ~]$ exit
    logout
    Connection to serverb closed.
    [student@workstation ~]$
    Evaluation
    
    As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.
    
    [student@workstation ~]$ lab grade rhcsa-rh124-review4
    Finish
    
    On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.
    
    [student@workstation ~]$ lab finish rhcsa-rh124-review4
    
  2. Assistant

    Automating the provided Red Hat exercise into a single command line involves chaining multiple commands using && to ensure each step executes only if the previous one succeeds. Additionally, you'll need to use ssh to execute commands on serverb. Below is a comprehensive one-liner that performs all the required tasks:

    lab start rhcsa-rh124-review4 && \
    ssh student@serverb 'echo "student" | sudo -S nmcli device status && \
    INTERFACE=$(nmcli device status | awk "/ethernet/ {print \$1}") && \
    echo "Using interface: \$INTERFACE" && \
    echo "student" | sudo -S nmcli connection add con-name static type ethernet ifname \$INTERFACE ipv4.addresses "172.25.250.111/24" ipv4.gateway "172.25.250.254" ipv4.dns "172.25.250.254" ipv4.method manual && \
    echo "student" | sudo -S nmcli connection up static && \
    echo "student" | sudo -S hostnamectl set-hostname server-review4.lab4.example.com && \
    echo "172.25.250.10 client-review4" | sudo tee -a /etc/hosts && \
    ping -c2 client-review4 && \
    echo "student" | sudo -S nmcli connection modify static +ipv4.addresses "172.25.250.211/24" && \
    echo "student" | sudo -S nmcli connection up static' && \
    ping -c2 172.25.250.211 && \
    lab grade rhcsa-rh124-review4 && \
    lab finish rhcsa-rh124-review4
    

    Explanation of the Command:

    1. Start the Lab:

      lab start rhcsa-rh124-review4
      

      Initiates the lab environment.

    2. Execute Commands on serverb:

      ssh student@serverb '...'
      

      Connects to serverb as the student user and executes the enclosed commands.

    3. Inside the SSH Session:

      • Authenticate with sudo:

        echo "student" | sudo -S ...
        

        Supplies the student password to execute commands with superuser privileges.

      • Determine Ethernet Interface:

        INTERFACE=$(nmcli device status | awk "/ethernet/ {print \$1}")
        

        Identifies the active Ethernet interface.

      • Create and Activate Static Connection:

        nmcli connection add con-name static type ethernet ifname \$INTERFACE ipv4.addresses "172.25.250.111/24" ipv4.gateway "172.25.250.254" ipv4.dns "172.25.250.254" ipv4.method manual
        nmcli connection up static
        

        Sets up and activates the static network configuration.

      • Set Hostname:

        hostnamectl set-hostname server-review4.lab4.example.com
        

        Updates the system hostname.

      • Update /etc/hosts:

        echo "172.25.250.10 client-review4" | sudo tee -a /etc/hosts
        

        Adds a canonical hostname mapping.

      • Ping to Verify Hostname Resolution:

        ping -c2 client-review4
        

        Tests connectivity using the canonical hostname.

      • Add Additional IPv4 Address:

        nmcli connection modify static +ipv4.addresses "172.25.250.211/24"
        nmcli connection up static
        

        Adds an extra IP address to the static connection and reactivates it.

    4. Verify Additional IP from Workstation:

      ping -c2 172.25.250.211
      

      Ensures the additional IP is reachable.

    5. Grade and Finish the Lab:

      lab grade rhcsa-rh124-review4
      lab finish rhcsa-rh124-review4
      

      Grades your work and concludes the lab session.

    Notes:

    • Password Handling: This one-liner uses echo "student" | sudo -S to supply the sudo password. Ensure that this approach complies with your security policies. For enhanced security, consider configuring password-less sudo for specific commands or using SSH keys with appropriate restrictions.

    • Interface Name Assumption: The command dynamically determines the Ethernet interface name. If there are multiple Ethernet interfaces or different naming conventions, you might need to adjust the awk command accordingly.

    • Error Handling: This one-liner assumes that each command succeeds before moving to the next. If any command fails, subsequent commands will not execute. For more robust error handling, additional checks can be incorporated.

    • Improving Readability: For complex automation tasks, consider using a shell script instead of a one-liner. Scripts enhance readability, maintainability, and allow for easier debugging.

    Example Execution:

    To execute the one-liner, simply paste it into your terminal on the workstation machine:

    lab start rhcsa-rh124-review4 && \
    ssh student@serverb 'echo "student" | sudo -S nmcli device status && \
    INTERFACE=$(nmcli device status | awk "/ethernet/ {print \$1}") && \
    echo "Using interface: \$INTERFACE" && \
    echo "student" | sudo -S nmcli connection add con-name static type ethernet ifname \$INTERFACE ipv4.addresses "172.25.250.111/24" ipv4.gateway "172.25.250.254" ipv4.dns "172.25.250.254" ipv4.method manual && \
    echo "student" | sudo -S nmcli connection up static && \
    echo "student" | sudo -S hostnamectl set-hostname server-review4.lab4.example.com && \
    echo "172.25.250.10 client-review4" | sudo tee -a /etc/hosts && \
    ping -c2 client-review4 && \
    echo "student" | sudo -S nmcli connection modify static +ipv4.addresses "172.25.250.211/24" && \
    echo "student" | sudo -S nmcli connection up static' && \
    ping -c2 172.25.250.211 && \
    lab grade rhcsa-rh124-review4 && \
    lab finish rhcsa-rh124-review4
    

    Ensure that you have SSH access to serverb and that the lab commands are available on your workstation machine. Adjust passwords and interface names as necessary based on your environment.