Linux test TCP/UDP port connectivity with nc command

TCP

# nc -z -v [hostname or IP address] [port number]
nc -z -v 192.168.100.1 443
Connection to  192.168.100.1 443 port [tcp/https] succeeded!

UDP

# nc -z -v -u [hostname or IP address] [port number]
# nc -z -v -u 192.168.100.1 123 
Connection to  192.168.100.1 123 port [udp/ntp] succeeded!

Bouns

We can also test TCP port with telnet command

telnet [hostname or IP address] [port number]
# telnet 192.168.100.1 443
Trying 192.168.100.1…
Connected to 192.168.100.1.
Escape character is '^]'.

If return “Connected to xxx.xxx.xxx.xxx”, it means the port is open and functional

If return “Trying xxx.xxx.xxx.xxx…”, it means the port is closed or not accessible.

Check NTP server working or not, Check NTP server Date and Time – Windows, Linux

Windows

1 Use Win + R to open run Window

Microsoft Windows - Run window
Microsoft Windows – Run window

2 Type cmd, then hit Enter key

Microsoft Windows - Run window - cmd.exe
Microsoft Windows – Run window – cmd.exe

The command we use is in following format:

w32tm /stripchart /computer:domainnameORipaddress /dataonly /samples:5

3 Enter following command to test “time.windows.com”

w32tm /stripchart /computer:time.windows.com /dataonly /samples:5
Microsoft Windows - CMD -   w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output
Microsoft Windows – CMD – w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output

Bonus: Other commands

1 Show the current time zone settings:

w32tm /tz

2 List external NTP servers (peers)

w32tm /query /peers
Microsoft Windows - CMD -  w32tm /query /peers output
Microsoft Windows – CMD – w32tm /query /peers output

Note! If you have encountered this error: “The following error occurred: The service has not been started. (0x80070426)”, you need to start the “Windows Time” service, you can start a elevated cmd or PowerShell by using Win + X key combination then select “Command Prompt (Admin)” or “Windows PowerShell (Admin)” then type “sc start W32Time” to start the Windows Time service or using Run window and type “services.msc” then find “Windows Time” service and start it.

3 Restore Windows Time service settings to default

C:\>net stop w32time
C:\>w32tm /unregister
C:\>w32tm /register
C:\>net start w32time

4 Re-sync Time

Resynchronize the clock as soon as possible, disregarding all accumulated error statistics. If no computer is specified, the local computer will resynchronize. The command will wait for resynchronization unless the /nowait switch is used.

w32tm /resync

5 Redirect the network configuration and Re-sync time

Force redetection of network resource before resynchronization.

w32tm /resync /rediscover
Microsoft Windows - PowerShell - w32tm /resync /rediscover output
Microsoft Windows – PowerShell – w32tm /resync /rediscover output

6 Check current NTP configuration

Show Information:

w32tm /query /status
Microsoft Windows - PowerShell -  w32tm /query /status  output
Microsoft Windows – PowerShell – w32tm /query /status output

Show Configuration:

w32tm /query /configuration
Microsoft Windows - PowerShell -  w32tm /query /configuration  output
Microsoft Windows – PowerShell – w32tm /query /configuration output

Linux

1 ntpdate -q domainnameORipaddress

ntpdate -q time.windows.com
Linux - Terminal - ntpdate -q time.windows.com output
Linux – Terminal – ntpdate -q time.windows.com output

2 ntpq -p domainnameORipaddress

ntpq -p 192.168.1.1

Linux clear intrusion footprint

1 Clear login Log

echo >/var/log/wtmp

2 Clear failed login Log

echo >/var/log/btmp

3 Remove last login echo (Comment out PrintLastLog yes and rstart ssdh service)

vi /etc/ssh/sshd_config

#PrintLastLog yes

service sshd restart OR /etc/init.d/sshd restart

4 Delete bash history

history -c

5 Clear bash history file

rm -rf ~/.bash_history

Linux Server Tips

Keywords: Linux Server Tips, Linux commands

1 Check last login details, who, when

“w” to check last start up time, currently logged in users

“last” to check logins

“who” Currently logged in users’ details

“lastlog” Users’ last login time

“whoami” Show current username

“finger” Find user information

“id” Current user uid, gid and group information

2 Find ports

netstat -anp | grep 443

lsof -i:443

3 Compile source code

“ar” extract archive, archived installation files

“./configure” configure to fit current systems

“make” compile the source code into binary files e.g. executables

“make install” install the compiled program

4 Linux Directories

“/boot”: Linux kernel, boot configuration etc.

“/bin”: Basic user commands, can be run by users

“/dev”: HDD, Keyboard, Mouse, DVD/CD-ROM device files

“/etc”: Configuration files

“/home”: Users’ working directories (home directory etc.)

“/sbin”: Basic management commands, normally only accessable by administrators

“/usr”: Application, source code files, documents etc.

“/var”: Logs, user mail, process data

“/tmp”: Temporary files

5 Common log files

“/var/log/messages”: Kernel and public messages

“/var/log/cron”: Scheduled Tasks/Crontab

“/var/log/dmesg”: System boot log

“/var/log/maillog”: System mail log

“/var/log/secure”: Access control log

6 Common Linux boot files

“/etc/fstab”: Auto mount on boot

“/etc/initab”: Define default boot level

“/etc/rc.local”: Define custom boot tasks

7 suid, sgid, sticky bit

suid: Run as the owner of the file/binary

sgid: Files created within the folder inherits the folder owner’s permission

sticky bit: Users can only modify, delete wheir own files

8 Check process: “ps”, “top”

9 Check process scheduling: “at”, “crontab”, “batch”, “kill”

10 Extract files

1.*.tarExtract using tar -xvf
2.*.gz Extract using gzip -d OR gunzip
3.*.tar.gz Extract using tar -xzf
4.*.bz2 Extract using bzip2 -d OR bunzip2
5.*.tar.bz2 Extract using tar -xjf
6.*.Z Extract using uncompress
7.*.tar.Z Extract using tar -xZf
8.*.rar Extract using unrar e
9.*.zip Extract using unzip

11 Mounting Windows share

mount -t cifs -o username=windowsusername, password="windowspassword" //ipaddress/folder mnt/linuxtargetfolder

OR

mount.cifs //ipaddress/folder /mnt.linuxtargetfolder -o username="windowsusername", password="windowspassword"

Change hostname in Linux operating systems

Usually hostname is configured during system installating.

Here is how to change the hostame on up to date Linux Linux distributions with systemd installed by default.

To understand what is hostname: https://en.wikipedia.org/wiki/Hostname

Show Current hostname

To show current hostname, we can use this command: hostnamectl

(It will show information including: Static hostname, Icon name, Chassis, Machine ID, Boot ID, Virtualization, Operating System, Kernel, Architecture)

[email protected]:~$ hostnamectl
    Static hostname: ubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl
Output of hostnamectl

Change hostname

Command: hostnamectl set-hostname newhostname

[email protected]:~$ hostnamectl set-hostname newubuntu
 [email protected]:~$ hostnamectl
    Static hostname: newubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl set-hostname newhostname
Output of hostnamectl set-hostname newhostname

hostnamectl set-hostname will return 0 if the command was successfully executed, if the command failed, it will return non 0 results.

To check if the hostname is changed or not, just execute hostnamectl command again to verify.

Depend on your platform or Linux distro, you may need to modify the command to achieve same results.