The basic Linux networking commands

September 5, 2021

In this topic, I’m going to be going through some common networking commands that you should know as a programmer when using a Linux machine. Now I’m not going to be talking about these commands in a ton of depth. I’m just going to kind of show them to you, make you aware that they exist, give you an idea of what their output really is. And then you can kind of look them up further if there’s some more specific stuff that you want to know about them.

Tham khao San xuat phim quang cao TVC.

So anyways, let’s jump into this topic after a quick word from Linode, which is the sponsor of this topic and this series. So Linode is one of the best companies to use when hosting a personal project, a website, a domain, whatever it may be. I’ve been using Linode for over two years now, they have great support. They just make it super simple to do everything. And that’s one of the reasons that I love them.

So if you do want to get a free, a hundred dollars credit towards Linode and sign up with a new account by hitting the link in the description, and you’ll be given a hundred dollars in free credits towards your Linode machines.

Using ifconfig

First command that I want to show you here is con I F config. This is a very common command. Most of you probably know this or have heard of it before. And what this command does is give you information about your network interface devices or your network interfaces. So I’m not going to explain what all of this means, because there’s a lot. And quite frankly, I’m not qualified to explain all of this detailed networking stuff. Uh, but I’ll just quickly go through what this actually means.

So, first of all, you can see the different devices that are connected to your machine on the left-hand side here. Now, some of these devices could be hardware devices, so physical devices connected on your machine, or they could be virtual. Now in our case, we have one hardware device, which is represented by F zero and one software device or one virtual device, uh, which is low now, L O actually stands for loop back. I’m not going to really talk about this. Uh, but if you know what a loop back is, you know, what a loop back virtual, um, network interfaces then, I mean, you know what this means anyways. Now let’s talk about F zero.

So at the zero is your physical ethernet connection. Um, this is typically just what represents it. You see in F zero for most of your machines, you’re only going to see two devices here, but if you had wifi or you had a second land port or whatever, it may be, you may see some other devices connected, but anyways, this is our hardware device. And if we want to see the IP address related to this, while we can see the IPV four address right here in the IPV, six addresses down here.

So again, I’m not really going to go any further than that. Uh, you can, this says ether, net, and there’s some more information that if you understand well you’ll know, and you can read through it’s is that is I F config?

Getting IP information

Now that’s helpful. But sometimes we don’t. I actually need to see all that information, or we don’t want to see all that information. Instead. We just want to find our IPV four or IPV six address. So very commonly, especially when you’re running kind of a hobby project or something you want to know the IPV four IPV, six address of your machine to find this out in a little bit of a nicer interface, you can type the command IP hyphen four, and then add R.

Now this is going to show you your IPV four address. You can also type out IP hyphen six. This will show you your IPB six address. So let’s start with, for when we go here, we can see our IP V4 address showing up here and other information related to that. Now, when you’re looking for the address or when you’re trying to find the address, sorry, you’re going to be looking for I net and then a number followed after it.

Now, typically when you just see I net, that means IPV four. When you see I net six, that means IPV six. If you don’t know the difference between them, don’t worry. I’m not a huge deal. Again, I’m not going to get into all the networking stuff. This is really just, if you needed to know this, this is how you find this information. All right. So now IP, hyphen six, you can see now that we see our IP V6 address represented by net six.

  The pros and cons of Machine Learning

All right. So that’s those two commands there. That’s all I really want to talk about related to IP addresses. Uh, again, this is just meant to give you an idea of the commands and not talk about them really in depth.

Using netstat

Next, what I’m going to show you is another command. Now this command is called netstat. What netstat is going to do is show you detailed information about the network connectivity or network activity on your Linux machine.

So if you type in netstat and press enter, you’re going to see what to me at least just looks like a bunch of cryptic texts. It doesn’t really mean much, but if you’re someone who does need to look at all of the active ports, all of the listening ports, you want to see what protocols different connections are on, and which connections are active on your machine. You’re going to use netstat to do that.

So I’m not going to go through all of the output here. Uh, if you don’t understand this chances are, you probably don’t need to know most of this. Uh, but if you want to find detailed information related to the network activity, net stats, the command you’re going to use. Now, I’ll quickly show you a few more net stack commands that might show you some more useful things. So if you want to look at, first of all, all of the listening ports for TCP and UDP connections, what you’re going to print out or what you’re going to type here is netstat hyphen a. So netstat a will show you all of the listening ports for TCP and UDP connections.

So when I print this out, you see, we get a very similar thing, except now we can see all of the listening ports as well, or all of the listening connections. All right. So that’s, netstat hyphen a. Now there’s a few more nuts set commands to show you as well. The next one is netstat hyphen 80. Now 80 is going to show you all of the TCP port connection. So only TCP. So 18 is only TCP. When I type that out, we can see all of the TCP connections right here. All right, the next command to show you this one is called netstat hyphen L. So netstat hyphen L. This is simply going to show you all of the active ports.

So when I do this, we get all of our active ports printing. And then lastly, if you want to see all of just the UDP connections or UDP ports that are listening, you can say netstat hyphen U, and this will show you all of the UDP ports. Now you can see, we don’t have any that are listening right now. So nothing’s showing up again. If you don’t understand this, don’t worry. Uh, these are just for people that need to find this information.

Well, these are the commands you can use to do so. So we’ll continue in one second, but I need to quickly thank the other sponsor of this video, which is algo expert algo expert is the best platform to use when preparing for your software engineering, coding interviews. And I actually happened to be an instructor on the platform. That’s right.

Using Curl

All right, the next thing I’m going to show you is a much more useful command that you probably will find yourself using. And this is called curl. Now curl is kind of a way to test out an end point or send a request to where you are L so I’m just going to show you what curl does by default. So if I just type, curl, and then I follow that with an address. You’ll see, by default, this sends a get request to this website, and then we get all of this HTML showing up right here.

So it will actually return to you, whatever, I guess the get request for the end point that you’re sending it to gives you. So of course, we’re not really getting anything meaningful because my website is able to detect if robots are trying to scrape it. So we say, you know, you can see in the meta name equals robots, which just means we’re not going to get really a valid response back.

  The pros and cons of Machine Learning

Anyways, I’m going to clear this, but that’s kind of the basics of curl. If you don’t type anything else and you just type, curl, and then end point it will by default, send a get request to that end point and then print out the response.

Now curl is much more useful than that though. You can also send post requests. You can send data and parameters. If I wanted to send a post request, what I could do is curl hyphen X. This is the method, and then type posts. Now, when I type posts that simply says, send a post request, then the URL that I want to send this to. So HTTPS colon tech with tim.net. Then what you’ll see is again, we get some HTML here pretty much saying, Hey, this is a robot sending this.

We’re not going to give you a valid response. So that is how you send a post request. You just do the hyphen X, the hyphen X stands for the methods. So when you type hyphen X after it, it’s expecting a method. So you could do posts, you could do puts, you could do delete.

You could do get whatever method you’re trying to send. That’s what you do with hyphen acts. Now, oftentimes when you’re sending a post request or I put request or whatever it is, you want to send some data with that. Now there’s multiple ways to send data with a post request. One of the easiest ways to do it is to add the data flags.

You can say hyphen data. And then instead of double quotations, what you can do is you can type all of the parameters followed by their value. So we’d say P one, standing for parameter one equals value one. Then if we want it to send multiple parameters, we would put the and sign. Then we could say equals value too. And that means we would send this data alongside with our post requests or put request or whatever it is. So if I do this, you could see, could not resolve host.

So it seems as though we got an error. The reason we got an error is because I messed up the flag here, a hyphen data, sorry. It needs to be hyphen hyphen data. So two, not one. And now you can see this actually works. We don’t get this, uh, could not resolve host, uh, issue popping up anyways. That’s one way to send data. Uh, let me clear the screen here. Just so this is a little bit easier. Okay. Another way to send data is to actually use the hyphen D flying.

So if I say hyphen D what’s expected after this is the name of my parameter and then equal sign and then the Valley. So you’d say hyphen D a parameter one equals and then value. And now I’m actually able to send this. If I add 18 to the end of my URL here. So now you can see this actually works. And if I use hyphen D, if I want to send more than one parameter, then I would say, hyphen D and then repeat the process. And you can keep adding hyphened DS to send multiple parameters.

So whatever method you like, you can use two do that. So the next thing I’m going to show you how to do with curl, save the content or the output or whatever you want to call it. The response, essentially from the server to a file to save the response from the server to a file, you type, curl hyphen lowercase. Oh, the name of the file. So we could say, uh, you know, response like this, and then the URL that you want to send this request to. So HTTP, and then, sorry, this is S colon slash slash tech with tim.net. Now, when we do this, you’re going to see that we actually get some output here showing us that this was downloaded.

Now, if I type LS, you can see that we should have a file named response. And if I look in this file response, so I say cat, and then response, uh, you can see that this is what is inside of it. So that’s how you easily save the contents or the response from a request to a file on your machine. Now, another, the thing you can do with curl is you can look at the header information from a response. So sometimes you don’t, I really care about the response itself. You just want to see the header that comes back with it. So to do that, you can say curl hyphen eye, and then again, the URL you want to send this to. So HTTPS colon slash slash tech with tim.net.

  The pros and cons of Machine Learning

And then you can see that this is the header that comes back with our response. And I believe, no, this didn’t show the responser. I was looking above, but anyways, yeah, you can see the HTTP header. That’s what hyphen capital I shows you. And it was a few more things you can do with curl. You’d say, curl hyphen help. And you can see all of the different options. Obviously, I can’t go through all of these with you, but it’s a really useful tool, especially if you just want to quickly test out an endpoint, uh, cause you don’t need to write a script to do it. You can just, you know, curl the endpoint and quickly see if everything is working as you expect it.

Now, the last thing we’ll show you with curl is how you can send a header with your request. So sometimes you need a custom header, maybe for an authorization token or something, uh, to do that. You would say curl hyphen, hyphen header. And then after that, you would simply type out the header you want. So something like content, hyphened type colon, and then application slash Jason. And then maybe if you wanted to send a post request, you could say, you know, hyped and ex post, you could pass your data here, you know, PRM one equals X and then to your URL and point. So let’s say it’s with tim.net and you’re going to see that.

I mean, this doesn’t do anything. We don’t have an end point here. That’s accepting this information, but that’s how you would do that. So that’s how you send a header. You specify it’s post request and you send a parameter equal to X. All right. So that is the basics of curl. Uh, now I’ll just show you one real example of using curl, because I feel like showing the fake ones, isn’t that useful. I’ll show you how we can actually just curl this end point.

That’s free on the internet. So this is just an API that requires no authorization and just gives you jokes when you hit its endpoint. So when I hit this one right here, you can see that we actually get a bunch of jokes popping up. So this does indeed work. Uh, if you use a correct end point.

Using Ping

So the last command I’m going to show you here is actually called ping. Now, what ping will do is simply send packets to an address or to the domain that you specify here. Uh, and then it will tell you if you’re getting a response from it. So typically you’re pinging something. If you want to see if it’s online, you want to make sure that it’s active and it’s working, or if you want to see if it’s blocking your requests, for example.

So if I say ping and then I put in, let’s just say, google.com here. Uh, you’ll notice that we were pinging this and we’re actually getting a response from google.com. It’s telling you how many packets or how many bites you send, uh, what you’re receiving and all of that. Now ping will just continually run. You can just keep pinging something for as long as you want and to stop this command, you simply type control seat, and that will stop the ping.

It will then tell you how many packets were transmitted, how many received the packet loss and all of that. So again, you can look up ping, if you want more information on it, super simple. You’re just really testing. If something’s giving you some kind of response, you don’t care what the response is. You just want to see if you’re getting something and we’ll, that’s why you use pink. So there’s not really much more to go through with networking or ping in general. I mean, if you wanted some more information on ping, you could type ping hyphen hyphen help, and then it’s going to show you all the things that you can do.